-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidates configuring of global loggers to a single function. This is mainly to facilitate configuring the logger for teleport scp, but will also allow us to remove the copy of logger initialization that currently exists in integrations/lib/logger.
- Loading branch information
1 parent
e0433c8
commit 19e7128
Showing
6 changed files
with
146 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"io/fs" | ||
"log/slog" | ||
"os" | ||
"strings" | ||
|
||
"github.com/gravitational/trace" | ||
|
||
"github.com/gravitational/teleport" | ||
) | ||
|
||
// Config configures teleport logging | ||
type Config struct { | ||
// Output defines where logs go. It can be one of the following: "stderr", "stdout" or | ||
// a path to a log file | ||
Output string `yaml:"output,omitempty"` | ||
// Severity defines how verbose the log will be. Possible values are "error", "info", "warn" | ||
Severity string `yaml:"severity,omitempty"` | ||
// Format defines the output format. Possible values are 'text' and 'json'. | ||
Format string `yaml:"output,omitempty"` | ||
// ExtraFields lists the output fields from KnownFormatFields. Example format: [timestamp, component, caller] | ||
ExtraFields []string `yaml:"extra_fields,omitempty"` | ||
// EnableColors dictates if output should be colored. | ||
EnableColors bool | ||
} | ||
|
||
// Initialize configures the default global logger based on the | ||
// provided configuration. The [slog.Logger] and [slog.LevelVar] | ||
func Initialize(loggerConfig Config) (*slog.Logger, *slog.LevelVar, error) { | ||
const ( | ||
// logFileDefaultMode is the preferred permissions mode for log file. | ||
logFileDefaultMode fs.FileMode = 0o644 | ||
// logFileDefaultFlag is the preferred flags set to log file. | ||
logFileDefaultFlag = os.O_WRONLY | os.O_CREATE | os.O_APPEND | ||
) | ||
|
||
var w io.Writer | ||
level := new(slog.LevelVar) | ||
switch loggerConfig.Output { | ||
case "": | ||
w = os.Stderr | ||
case "stderr", "error", "2": | ||
w = os.Stderr | ||
case "stdout", "out", "1": | ||
w = os.Stdout | ||
case teleport.Syslog: | ||
var err error | ||
w, err = NewSyslogWriter() | ||
if err != nil { | ||
slog.ErrorContext(context.Background(), "Failed to switch logging to syslog", "error", err) | ||
slog.SetDefault(slog.New(DiscardHandler{})) | ||
return slog.Default(), level, nil | ||
} | ||
default: | ||
// Assume this is a file path. | ||
sharedWriter, err := NewFileSharedWriter(loggerConfig.Output, logFileDefaultFlag, logFileDefaultMode) | ||
if err != nil { | ||
return nil, nil, trace.Wrap(err, "failed to init the log file shared writer") | ||
} | ||
w = NewWriterFinalizer(sharedWriter) | ||
if err := sharedWriter.RunWatcherReopen(context.Background()); err != nil { | ||
return nil, nil, trace.Wrap(err) | ||
} | ||
} | ||
|
||
switch strings.ToLower(loggerConfig.Severity) { | ||
case "", "info": | ||
level.Set(slog.LevelInfo) | ||
case "err", "error": | ||
level.Set(slog.LevelError) | ||
case teleport.DebugLevel: | ||
level.Set(slog.LevelDebug) | ||
case "warn", "warning": | ||
level.Set(slog.LevelWarn) | ||
case "trace": | ||
level.Set(TraceLevel) | ||
default: | ||
return nil, nil, trace.BadParameter("unsupported logger severity: %q", loggerConfig.Severity) | ||
} | ||
|
||
configuredFields, err := ValidateFields(loggerConfig.ExtraFields) | ||
if err != nil { | ||
return nil, nil, trace.Wrap(err) | ||
} | ||
|
||
var logger *slog.Logger | ||
switch strings.ToLower(loggerConfig.Format) { | ||
case "": | ||
fallthrough // not set. defaults to 'text' | ||
case "text": | ||
logger = slog.New(NewSlogTextHandler(w, SlogTextHandlerConfig{ | ||
Level: level, | ||
EnableColors: loggerConfig.EnableColors, | ||
ConfiguredFields: configuredFields, | ||
})) | ||
slog.SetDefault(logger) | ||
case "json": | ||
logger = slog.New(NewSlogJSONHandler(w, SlogJSONHandlerConfig{ | ||
Level: level, | ||
ConfiguredFields: configuredFields, | ||
})) | ||
slog.SetDefault(logger) | ||
default: | ||
return nil, nil, trace.BadParameter("unsupported log output format : %q", loggerConfig.Format) | ||
} | ||
|
||
return logger, level, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters