logger_120.go raw
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build !go1.21
6
7 package slog
8
9 import (
10 "context"
11 "log"
12
13 "golang.org/x/exp/slog"
14 )
15
16 // Default returns the default Logger.
17 func Default() *Logger { return slog.Default() }
18
19 // SetDefault makes l the default Logger.
20 // After this call, output from the log package's default Logger
21 // (as with [log.Print], etc.) will be logged at LevelInfo using l's Handler.
22 func SetDefault(l *Logger) {
23 slog.SetDefault(l)
24 }
25
26 // A Logger records structured information about each call to its
27 // Log, Debug, Info, Warn, and Error methods.
28 // For each call, it creates a Record and passes it to a Handler.
29 //
30 // To create a new Logger, call [New] or a Logger method
31 // that begins "With".
32 type Logger = slog.Logger
33
34 // New creates a new Logger with the given non-nil Handler.
35 func New(h Handler) *Logger {
36 return slog.New(h)
37 }
38
39 // With calls Logger.With on the default logger.
40 func With(args ...any) *Logger {
41 return slog.With(args...)
42 }
43
44 // NewLogLogger returns a new log.Logger such that each call to its Output method
45 // dispatches a Record to the specified handler. The logger acts as a bridge from
46 // the older log API to newer structured logging handlers.
47 func NewLogLogger(h Handler, level Level) *log.Logger {
48 return slog.NewLogLogger(h, level)
49 }
50
51 // Debug calls Logger.Debug on the default logger.
52 func Debug(msg string, args ...any) {
53 slog.Debug(msg, args...)
54 }
55
56 // DebugContext calls Logger.DebugContext on the default logger.
57 func DebugContext(ctx context.Context, msg string, args ...any) {
58 slog.DebugContext(ctx, msg, args...)
59 }
60
61 // Info calls Logger.Info on the default logger.
62 func Info(msg string, args ...any) {
63 slog.Info(msg, args...)
64 }
65
66 // InfoContext calls Logger.InfoContext on the default logger.
67 func InfoContext(ctx context.Context, msg string, args ...any) {
68 slog.InfoContext(ctx, msg, args...)
69 }
70
71 // Warn calls Logger.Warn on the default logger.
72 func Warn(msg string, args ...any) {
73 slog.Warn(msg, args...)
74 }
75
76 // WarnContext calls Logger.WarnContext on the default logger.
77 func WarnContext(ctx context.Context, msg string, args ...any) {
78 slog.WarnContext(ctx, msg, args...)
79 }
80
81 // Error calls Logger.Error on the default logger.
82 func Error(msg string, args ...any) {
83 slog.Error(msg, args...)
84 }
85
86 // ErrorContext calls Logger.ErrorContext on the default logger.
87 func ErrorContext(ctx context.Context, msg string, args ...any) {
88 slog.ErrorContext(ctx, msg, args...)
89 }
90
91 // Log calls Logger.Log on the default logger.
92 func Log(ctx context.Context, level Level, msg string, args ...any) {
93 slog.Log(ctx, level, msg, args...)
94 }
95
96 // LogAttrs calls Logger.LogAttrs on the default logger.
97 func LogAttrs(ctx context.Context, level Level, msg string, attrs ...Attr) {
98 slog.LogAttrs(ctx, level, msg, attrs...)
99 }
100