Documentation ¶
Index ¶
- Constants
- Variables
- func Initialize(loggerConfig Config) (*slog.Logger, *slog.LevelVar, error)
- func NewPackageLogger(args ...any) *slog.Logger
- func NewSyslogWriter() (io.Writer, error)
- func StringerAttr(s fmt.Stringer) slog.LogValuer
- func TypeAttr(val any) slog.LogValuer
- func ValidateFields(formatInput []string) (result []string, err error)
- type Config
- type DiscardHandler
- type FileSharedWriter
- type SlogJSONHandler
- type SlogJSONHandlerConfig
- type SlogTextHandler
- type SlogTextHandlerConfig
- type WriterFinalizer
Constants ¶
const ( // TraceLevel is the logging level when set to Trace verbosity. TraceLevel = slog.LevelDebug - 1 // TraceLevelText is the text representation of Trace verbosity. TraceLevelText = "TRACE" // LevelField is the log field that stores the verbosity. LevelField = "level" // ComponentField is the log field that stores the calling component. ComponentField = "component" // CallerField is the log field that stores the calling file and line number. CallerField = "caller" // TimestampField is the field that stores the timestamp the log was emitted. TimestampField = "timestamp" )
Variables ¶
var ( errors.New("file shared writer is closed") )ErrFileSharedWriterClosed =
var SupportedLevelsText = []string{ TraceLevelText, slog.LevelDebug.String(), slog.LevelInfo.String(), slog.LevelWarn.String(), slog.LevelError.String(), }
SupportedLevelsText lists the supported log levels in their text representation. All strings are in uppercase.
Functions ¶
func Initialize ¶
Initialize configures the default global logger based on the provided configuration. The slog.Logger and slog.LevelVar
func NewPackageLogger ¶
NewPackageLogger creates a slog.Logger that defers setting any groups or attributes until the first use of the logger. This allows package global loggers to be created prior to any custom slog.Handler can be set via slog.Default AND still respect the formatting of the default handler set at runtime.
func NewSyslogWriter ¶
NewSyslogWriter creates a writer that outputs to the local machine syslog.
func StringerAttr ¶
StringerAttr creates a slog.LogValuer that will defer to the provided fmt.Stringer. All slog attributes are always evaluated, even if the log event is discarded due to the configured log level. A text slog.Handler will try to defer evaluation if the attribute is a fmt.Stringer, however, the JSON slog.Handler only defers to [json.Marshaler]. This means that to defer evaluation and creation of the string representation, the object must implement fmt.Stringer and [json.Marshaler], otherwise additional and unwanted values may be emitted if the logger is configured to use JSON instead of text. This wrapping mechanism allows a value that implements fmt.Stringer, to be guaranteed to be lazily constructed and always output the same content regardless of the output format.
func TypeAttr ¶
TypeAttr creates a lazily evaluated log value that presents the pretty type name of a value as a string. It is roughly equivalent to the '%T' format option, and should only perform reflection in the event that logs are actually being generated.
func ValidateFields ¶
ValidateFields ensures the provided fields map to the allowed fields. An error is returned if any of the fields are invalid.
Types ¶
type Config ¶
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 // Severity defines how verbose the log will be. Possible values are "error", "info", "warn" Severity string // Format defines the output format. Possible values are 'text' and 'json'. Format string // ExtraFields lists the output fields from KnownFormatFields. Example format: [timestamp, component, caller] ExtraFields []string // EnableColors dictates if output should be colored. EnableColors bool // Padding to use for various components. Padding int }
Config configures teleport logging
type DiscardHandler ¶
type DiscardHandler struct{}
DiscardHandler is a slog.Handler that discards all messages. It is more efficient than a slog.Handler which outputs to io.Discard since it performs zero formatting. TODO(tross): Use slog.DiscardHandler once upgraded to Go 1.24.
type FileSharedWriter ¶
type FileSharedWriter struct {
// contains filtered or unexported fields
}
FileSharedWriter is similar to SharedWriter except that it requires a `os.File` instead of a `io.Writer`. This is to allow the File reopen required by logrotate and similar tools.
func NewFileSharedWriter ¶
NewFileSharedWriter wraps the provided os.File in a writer that is thread safe, with ability to enable filesystem notification watch and reopen file on specific events.
func (*FileSharedWriter) Close ¶
func (s *FileSharedWriter) Close() error
Close stops the internal watcher and close the log file.
func (*FileSharedWriter) Reopen ¶
func (s *FileSharedWriter) Reopen() error
Reopen closes the file and opens it again using APPEND mode.
func (*FileSharedWriter) RunWatcherReopen ¶
func (s *FileSharedWriter) RunWatcherReopen(ctx context.Context) error
RunWatcherReopen runs a filesystem watcher for rename/remove events to reopen the log.
type SlogJSONHandler ¶
type SlogJSONHandler struct {
*slog.JSONHandler
}
SlogJSONHandler is a slog.Handler that outputs messages in a json format per the config file.
func NewSlogJSONHandler ¶
func NewSlogJSONHandler(w io.Writer, cfg SlogJSONHandlerConfig) *SlogJSONHandler
NewSlogJSONHandler creates a SlogJSONHandler that outputs to w.
type SlogJSONHandlerConfig ¶
type SlogJSONHandlerConfig struct { // Level is the minimum record level that will be logged. Level slog.Leveler // ConfiguredFields are fields explicitly set by users to be included in // the output message. If there are any entries configured, they will be honored. // If empty, the default fields will be populated and included in the output. ConfiguredFields []string // ReplaceAttr is called to rewrite each non-group attribute before // it is logged. ReplaceAttr func(groups []string, a slog.Attr) slog.Attr }
SlogJSONHandlerConfig allows the SlogJSONHandler functionality to be tweaked.
type SlogTextHandler ¶
type SlogTextHandler struct {
// contains filtered or unexported fields
}
SlogTextHandler is a slog.Handler that outputs messages in a textual manner as configured by the Teleport configuration.
func NewSlogTextHandler ¶
func NewSlogTextHandler(w io.Writer, cfg SlogTextHandlerConfig) *SlogTextHandler
NewSlogTextHandler creates a SlogTextHandler that writes messages to w.
func (*SlogTextHandler) Enabled ¶
Enabled returns whether the provided level will be included in output.
func (*SlogTextHandler) Handle ¶
Handle formats the provided record and writes the output to the destination.
type SlogTextHandlerConfig ¶
type SlogTextHandlerConfig struct { // Level is the minimum record level that will be logged. Level slog.Leveler // EnableColors allows the level to be printed in color. EnableColors bool // Padding to use for various components. Padding int // ConfiguredFields are fields explicitly set by users to be included in // the output message. If there are any entries configured, they will be honored. // If empty, the default fields will be populated and included in the output. ConfiguredFields []string // ReplaceAttr is called to rewrite each non-group attribute before // it is logged. ReplaceAttr func(groups []string, a slog.Attr) slog.Attr }
SlogTextHandlerConfig allow the SlogTextHandler functionality to be tweaked.
type WriterFinalizer ¶
type WriterFinalizer[T io.WriteCloser] struct { // contains filtered or unexported fields }
WriterFinalizer is a wrapper for the io.WriteCloser to automate resource cleanup.
func NewWriterFinalizer ¶
func NewWriterFinalizer[T io.WriteCloser](writer T) *WriterFinalizer[T]
NewWriterFinalizer wraps the provided writer io.WriteCloser to trigger Close function after writer is unassigned from any variable.
func (*WriterFinalizer[T]) Close ¶
func (w *WriterFinalizer[T]) Close() error
Close wraps closing function of internal writer.