Documentation ¶
Index ¶
- Constants
- Variables
- func ParseLevel(s string) slog.Leveler
- type L
- func (l *L) Debug(msg any, keyvals ...any)
- func (l *L) Err(msg any, keyvals ...any)
- func (l *L) Fatal(msg any, keyvals ...any)
- func (l *L) Info(msg any, keyvals ...any)
- func (l *L) LogError(msg string, err error)
- func (l *L) New(name string) *L
- func (l *L) Warn(msg any, keyvals ...any)
- func (l *L) With(keyvals ...any) *L
- type Option
- type TimeFormatterFunc
Examples ¶
Constants ¶
const ( FormatLogFmt = "logfmt" FormatJSON = "json" )
Constants defining various output formats.
const ( LevelAll = slog.Level(-10) LevelFatal = slog.Level(12) )
Variables ¶
var AvailableFormats = []string{ FormatLogFmt, FormatJSON, }
AvailableFormats lists the available format types.
Functions ¶
Types ¶
type L ¶
type L struct {
// contains filtered or unexported fields
}
L is the logger implementation
func Default ¶
func Default() *L
Default returns a default logger implementation
Example ¶
l := logger.Default() l.Info("some message") // Output would resemble: // ts=2023-04-21T16:09:28.653472Z caller=github.com/jasonhancock/go-logger_test/example_test.go:51 src=default level=info msg="some message"
Output:
func New ¶
New initializes a new logger. If w is nil, logs will be sent to stdout.
Example (Customized) ¶
l := logger.New( logger.WithDestination(os.Stdout), logger.WithName("myapp"), logger.WithLevel("info"), logger.WithFormat(logger.FormatLogFmt), logger.With("somekey", "someval"), ) l.Info("some message", "anotherkey", "another value") // Output would resemble: // ts=2023-04-13T17:38:13.516398Z caller=github.com/jasonhancock/go-logger_test/example_test.go:11 somekey=someval src=myapp level=info msg="some message" anotherkey="another value"
Output:
Example (Json) ¶
l := logger.New( logger.WithFormat(logger.FormatJSON), logger.WithName("myapp"), ) l.Info("some message", "anotherkey", "another value") // Output would resemble: // {"anotherkey":"another value","caller":"github.com/jasonhancock/go-logger_test/example_test.go:32","level":"info","msg":"some message","src":"myapp","ts":"2023-04-21T16:08:24.224597Z"}
Output:
Example (Minimum) ¶
l := logger.New() l.Info("some message", "anotherkey", "another value") // Output would resemble: // ts=2023-04-21T16:09:00.026753Z caller=github.com/jasonhancock/go-logger_test/example_test.go:43 src=go-logger.test level=info msg="some message" anotherkey="another value"
Output:
func Silence ¶
func Silence() *L
Silence returns a logger that writes everything to /dev/null. Useful for silencing log output from tests
func (*L) Fatal ¶
Fatal logs a message at the fatal level and also exits the program by calling os.Exit
func (*L) LogError ¶
LogError logs an error. It automatically unwinds multi-errors (not recursively...yet).
type Option ¶ added in v0.0.2
type Option func(*options)
Option is used to customize the logger.
func With ¶ added in v0.0.2
func With(keyvals ...interface{}) Option
With adds key value pairs to the logger.
func WithCaller ¶ added in v0.0.3
WithCaller sets whether or not to include the source file and line number of where the message originated.
func WithDestination ¶ added in v0.0.2
WithDestination sets the target for where the output of the logger should be written.
func WithFormat ¶ added in v0.0.2
WithFormat sets the format to log in.
func WithName ¶ added in v0.0.2
WithName specifies the name of the application. If not specified, the current processes name will be used.
func WithTimeLocation ¶ added in v0.0.3
WithTimeLocation specifies the locale to log the time in.