...

Source file src/github.com/sagikazarmark/slog-shim/logger.go

Documentation: github.com/sagikazarmark/slog-shim

     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  	"log/slog"
    13  )
    14  
    15  // Default returns the default Logger.
    16  func Default() *Logger { return slog.Default() }
    17  
    18  // SetDefault makes l the default Logger.
    19  // After this call, output from the log package's default Logger
    20  // (as with [log.Print], etc.) will be logged at LevelInfo using l's Handler.
    21  func SetDefault(l *Logger) {
    22  	slog.SetDefault(l)
    23  }
    24  
    25  // A Logger records structured information about each call to its
    26  // Log, Debug, Info, Warn, and Error methods.
    27  // For each call, it creates a Record and passes it to a Handler.
    28  //
    29  // To create a new Logger, call [New] or a Logger method
    30  // that begins "With".
    31  type Logger = slog.Logger
    32  
    33  // New creates a new Logger with the given non-nil Handler.
    34  func New(h Handler) *Logger {
    35  	return slog.New(h)
    36  }
    37  
    38  // With calls Logger.With on the default logger.
    39  func With(args ...any) *Logger {
    40  	return slog.With(args...)
    41  }
    42  
    43  // NewLogLogger returns a new log.Logger such that each call to its Output method
    44  // dispatches a Record to the specified handler. The logger acts as a bridge from
    45  // the older log API to newer structured logging handlers.
    46  func NewLogLogger(h Handler, level Level) *log.Logger {
    47  	return slog.NewLogLogger(h, level)
    48  }
    49  
    50  // Debug calls Logger.Debug on the default logger.
    51  func Debug(msg string, args ...any) {
    52  	slog.Debug(msg, args...)
    53  }
    54  
    55  // DebugContext calls Logger.DebugContext on the default logger.
    56  func DebugContext(ctx context.Context, msg string, args ...any) {
    57  	slog.DebugContext(ctx, msg, args...)
    58  }
    59  
    60  // Info calls Logger.Info on the default logger.
    61  func Info(msg string, args ...any) {
    62  	slog.Info(msg, args...)
    63  }
    64  
    65  // InfoContext calls Logger.InfoContext on the default logger.
    66  func InfoContext(ctx context.Context, msg string, args ...any) {
    67  	slog.InfoContext(ctx, msg, args...)
    68  }
    69  
    70  // Warn calls Logger.Warn on the default logger.
    71  func Warn(msg string, args ...any) {
    72  	slog.Warn(msg, args...)
    73  }
    74  
    75  // WarnContext calls Logger.WarnContext on the default logger.
    76  func WarnContext(ctx context.Context, msg string, args ...any) {
    77  	slog.WarnContext(ctx, msg, args...)
    78  }
    79  
    80  // Error calls Logger.Error on the default logger.
    81  func Error(msg string, args ...any) {
    82  	slog.Error(msg, args...)
    83  }
    84  
    85  // ErrorContext calls Logger.ErrorContext on the default logger.
    86  func ErrorContext(ctx context.Context, msg string, args ...any) {
    87  	slog.ErrorContext(ctx, msg, args...)
    88  }
    89  
    90  // Log calls Logger.Log on the default logger.
    91  func Log(ctx context.Context, level Level, msg string, args ...any) {
    92  	slog.Log(ctx, level, msg, args...)
    93  }
    94  
    95  // LogAttrs calls Logger.LogAttrs on the default logger.
    96  func LogAttrs(ctx context.Context, level Level, msg string, attrs ...Attr) {
    97  	slog.LogAttrs(ctx, level, msg, attrs...)
    98  }
    99  

View as plain text