Documentation
¶
Index ¶
- Constants
- func Close() error
- func Debug(event Event, ctx ...context.Context)
- func Error(event Event, ctx ...context.Context)
- func Fatal(event Event, ctx ...context.Context)
- func Info(event Event, ctx ...context.Context)
- func LanguageContext(lang string) context.Context
- func SetDefault(l *Logger, sinkClosers ...io.Closer)
- func Warn(event Event, ctx ...context.Context)
- type Attr
- type ConsoleSink
- func (s *ConsoleSink) WithColor(on bool) *ConsoleSink
- func (s *ConsoleSink) WithLevelColors(colors map[Level]string) *ConsoleSink
- func (s *ConsoleSink) WithLevelPrefixes(prefixes map[Level]string) *ConsoleSink
- func (s *ConsoleSink) WithLocalizedLevelColors(lang string, colors map[Level]string) *ConsoleSink
- func (s *ConsoleSink) WithLocalizedLevelPrefixes(lang string, prefixes map[Level]string) *ConsoleSink
- func (s *ConsoleSink) WithServiceColor(color string) *ConsoleSink
- func (s *ConsoleSink) WithServicePrefixStyle(open, close string) *ConsoleSink
- func (s *ConsoleSink) Write(_ context.Context, rec Record) error
- type Event
- type JSONSink
- type Level
- type Logger
- func (l *Logger) Debug(event Event, ctx ...context.Context)
- func (l *Logger) Enabled(level Level) bool
- func (l *Logger) Error(event Event, ctx ...context.Context)
- func (l *Logger) Info(event Event, ctx ...context.Context)
- func (l *Logger) SlogHandler(id MessageID) slog.Handler
- func (l *Logger) Warn(event Event, ctx ...context.Context)
- func (l *Logger) With(attrs ...Attr) *Logger
- type MessageID
- type NoPlural
- type Options
- type Record
- type SimpleEvent
- type Sink
- type Translator
Constants ¶
const ( ANSIRed = "\x1b[31m" ANSIGreen = "\x1b[32m" ANSIYellow = "\x1b[33m" ANSIBrightWhite = "\x1b[97m" )
Variables ¶
This section is empty.
Functions ¶
func Close ¶ added in v1.0.1
func Close() error
Close releases the default Logger's closers; safe to call more than once.
func Fatal ¶ added in v1.1.0
Fatal logs at error level, closes the sinks and exits with status 1 without running deferred functions.
func LanguageContext ¶
LanguageContext returns a background context carrying lang.
func SetDefault ¶ added in v1.0.1
SetDefault installs l and its sinkClosers, closing the previous closers; a nil l is ignored.
Types ¶
type Attr ¶
Attr is one key/value pair on an event, emitted in the record and passed to translators as template data.
func Duration ¶
Duration returns a duration attribute, rendered as text such as "1.5s" in logs and templates.
type ConsoleSink ¶
type ConsoleSink struct {
// contains filtered or unexported fields
}
ConsoleSink writes human-readable lines, colored on a terminal, with per-language level prefixes; safe for concurrent use.
func NewConsoleSink ¶
func NewConsoleSink(w io.Writer) *ConsoleSink
NewConsoleSink returns a ConsoleSink writing to w, colored only when w is a terminal.
func (*ConsoleSink) WithColor ¶
func (s *ConsoleSink) WithColor(on bool) *ConsoleSink
WithColor forces color on or off, overriding terminal detection.
func (*ConsoleSink) WithLevelColors ¶
func (s *ConsoleSink) WithLevelColors(colors map[Level]string) *ConsoleSink
WithLevelColors sets default ANSI colors for levels; missing levels keep theirs.
func (*ConsoleSink) WithLevelPrefixes ¶
func (s *ConsoleSink) WithLevelPrefixes(prefixes map[Level]string) *ConsoleSink
WithLevelPrefixes sets default console prefixes; missing levels keep theirs.
func (*ConsoleSink) WithLocalizedLevelColors ¶
func (s *ConsoleSink) WithLocalizedLevelColors(lang string, colors map[Level]string) *ConsoleSink
WithLocalizedLevelColors sets ANSI colors for records in lang; missing levels keep theirs.
func (*ConsoleSink) WithLocalizedLevelPrefixes ¶
func (s *ConsoleSink) WithLocalizedLevelPrefixes(lang string, prefixes map[Level]string) *ConsoleSink
WithLocalizedLevelPrefixes sets console prefixes for records in lang; missing levels keep theirs.
func (*ConsoleSink) WithServiceColor ¶
func (s *ConsoleSink) WithServiceColor(color string) *ConsoleSink
WithServiceColor sets the service prefix's ANSI color; empty disables it.
func (*ConsoleSink) WithServicePrefixStyle ¶
func (s *ConsoleSink) WithServicePrefixStyle(open, close string) *ConsoleSink
WithServicePrefixStyle sets the strings around the service prefix, [service] by default.
type Event ¶
type Event interface {
// MessageID returns the stable identifier for this event.
MessageID() MessageID
// Attrs returns the structured attributes for this event.
Attrs() []Attr
// PluralCount returns the count used for plural translation selection.
// The boolean is false for events that are not pluralized.
PluralCount() (int, bool)
}
Event is a typed log event; NewEvent and NewPluralEvent cover the simple cases.
type JSONSink ¶
type JSONSink struct {
// contains filtered or unexported fields
}
JSONSink writes records as newline-delimited JSON; safe for concurrent use.
func NewJSONSink ¶
NewJSONSink returns a JSONSink writing to w.
type Level ¶
type Level int
Level is the severity of a log record.
func ParseLevel ¶ added in v1.0.1
ParseLevel reads debug, info, warn, warning or error in any case; ok is false otherwise.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger emits typed events to a Sink, optionally translated; safe for concurrent use when its Sink is.
func Default ¶ added in v1.0.1
func Default() *Logger
Default returns the process-wide Logger; before SetDefault it writes to stderr at debug level.
func (*Logger) Enabled ¶ added in v1.3.0
Enabled reports whether a record at level reaches the sink.
func (*Logger) SlogHandler ¶ added in v1.3.0
SlogHandler returns a slog.Handler that logs each record as event id, the slog message under "detail".
type MessageID ¶
type MessageID string
MessageID is a stable, language-independent message id, emitted beside the text and used as the text when translation fails.
type NoPlural ¶
type NoPlural struct{}
NoPlural lets an event struct satisfy Event without a PluralCount of its own.
func (NoPlural) PluralCount ¶
PluralCount reports that the event is not pluralized.
type Options ¶
type Options struct {
// Language is the default target language passed to the Translator. A call logged with
// LanguageContext(lang) overrides it.
Language string
// Service is the service name attached to every record.
Service string
// MinLevel is the lowest level that is logged; lower levels are dropped
// before any translation or sink work. The zero value (LevelDebug) logs
// everything.
MinLevel Level
// Translator localizes events. If nil, messages are the message id.
Translator Translator
// Clock supplies the record timestamp. If nil, time.Now is used.
Clock func() time.Time
}
Options configures a Logger; the zero value works, with time.Now and untranslated message ids.
type Record ¶
type Record struct {
Time time.Time
Level Level
Service string
Lang string
MessageID MessageID
Message string
Attrs []Attr
}
Record is a resolved entry: Message is the translated text or the id, MessageID always the id.
type SimpleEvent ¶
type SimpleEvent struct {
// contains filtered or unexported fields
}
SimpleEvent is an immutable Event for events that need no struct of their own.
func NewEvent ¶
func NewEvent(id MessageID, attrs ...Attr) SimpleEvent
NewEvent returns a non-pluralized event with id and attrs.
func NewPluralEvent ¶
func NewPluralEvent(id MessageID, count int, attrs ...Attr) SimpleEvent
NewPluralEvent returns an event whose count selects the plural form.
func (SimpleEvent) Attrs ¶
func (e SimpleEvent) Attrs() []Attr
Attrs returns the structured attributes for this event.
func (SimpleEvent) MessageID ¶
func (e SimpleEvent) MessageID() MessageID
MessageID returns the stable identifier for this event.
func (SimpleEvent) PluralCount ¶
func (e SimpleEvent) PluralCount() (int, bool)
PluralCount returns the count used for plural translation selection.
func (SimpleEvent) With ¶
func (e SimpleEvent) With(attrs ...Attr) SimpleEvent
With returns a copy of e with attrs appended.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
consoleprefixes
command
|
|
|
customsink
command
|
|
|
global
command
|
|
|
jsonstdout
command
|
|
|
localizedprefixes
command
|
|
|
minimal
command
|
|
|
mixedformats
command
|
|
|
multisink
command
|
|
|
pluralization
command
|
|
|
rotatingfile
command
|
|
|
scopedattrs
command
|
|
|
verifytranslations
command
|
|
|
Package i18n is the go-i18n adapter for s99logger, loading locale files from any fs.FS.
|
Package i18n is the go-i18n adapter for s99logger, loading locale files from any fs.FS. |
|
Package rotation is a timberjack-backed, daily-rotated JSON file sink for s99logger.
|
Package rotation is a timberjack-backed, daily-rotated JSON file sink for s99logger. |