slogr.go raw

   1  //go:build go1.21
   2  // +build go1.21
   3  
   4  /*
   5  Copyright 2023 The logr Authors.
   6  
   7  Licensed under the Apache License, Version 2.0 (the "License");
   8  you may not use this file except in compliance with the License.
   9  You may obtain a copy of the License at
  10  
  11      http://www.apache.org/licenses/LICENSE-2.0
  12  
  13  Unless required by applicable law or agreed to in writing, software
  14  distributed under the License is distributed on an "AS IS" BASIS,
  15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16  See the License for the specific language governing permissions and
  17  limitations under the License.
  18  */
  19  
  20  package logr
  21  
  22  import (
  23  	"context"
  24  	"log/slog"
  25  )
  26  
  27  // FromSlogHandler returns a Logger which writes to the slog.Handler.
  28  //
  29  // The logr verbosity level is mapped to slog levels such that V(0) becomes
  30  // slog.LevelInfo and V(4) becomes slog.LevelDebug.
  31  func FromSlogHandler(handler slog.Handler) Logger {
  32  	if handler, ok := handler.(*slogHandler); ok {
  33  		if handler.sink == nil {
  34  			return Discard()
  35  		}
  36  		return New(handler.sink).V(int(handler.levelBias))
  37  	}
  38  	return New(&slogSink{handler: handler})
  39  }
  40  
  41  // ToSlogHandler returns a slog.Handler which writes to the same sink as the Logger.
  42  //
  43  // The returned logger writes all records with level >= slog.LevelError as
  44  // error log entries with LogSink.Error, regardless of the verbosity level of
  45  // the Logger:
  46  //
  47  //	logger := <some Logger with 0 as verbosity level>
  48  //	slog.New(ToSlogHandler(logger.V(10))).Error(...) -> logSink.Error(...)
  49  //
  50  // The level of all other records gets reduced by the verbosity
  51  // level of the Logger and the result is negated. If it happens
  52  // to be negative, then it gets replaced by zero because a LogSink
  53  // is not expected to handled negative levels:
  54  //
  55  //	slog.New(ToSlogHandler(logger)).Debug(...) -> logger.GetSink().Info(level=4, ...)
  56  //	slog.New(ToSlogHandler(logger)).Warning(...) -> logger.GetSink().Info(level=0, ...)
  57  //	slog.New(ToSlogHandler(logger)).Info(...) -> logger.GetSink().Info(level=0, ...)
  58  //	slog.New(ToSlogHandler(logger.V(4))).Info(...) -> logger.GetSink().Info(level=4, ...)
  59  func ToSlogHandler(logger Logger) slog.Handler {
  60  	if sink, ok := logger.GetSink().(*slogSink); ok && logger.GetV() == 0 {
  61  		return sink.handler
  62  	}
  63  
  64  	handler := &slogHandler{sink: logger.GetSink(), levelBias: slog.Level(logger.GetV())}
  65  	if slogSink, ok := handler.sink.(SlogSink); ok {
  66  		handler.slogSink = slogSink
  67  	}
  68  	return handler
  69  }
  70  
  71  // SlogSink is an optional interface that a LogSink can implement to support
  72  // logging through the slog.Logger or slog.Handler APIs better. It then should
  73  // also support special slog values like slog.Group. When used as a
  74  // slog.Handler, the advantages are:
  75  //
  76  //   - stack unwinding gets avoided in favor of logging the pre-recorded PC,
  77  //     as intended by slog
  78  //   - proper grouping of key/value pairs via WithGroup
  79  //   - verbosity levels > slog.LevelInfo can be recorded
  80  //   - less overhead
  81  //
  82  // Both APIs (Logger and slog.Logger/Handler) then are supported equally
  83  // well. Developers can pick whatever API suits them better and/or mix
  84  // packages which use either API in the same binary with a common logging
  85  // implementation.
  86  //
  87  // This interface is necessary because the type implementing the LogSink
  88  // interface cannot also implement the slog.Handler interface due to the
  89  // different prototype of the common Enabled method.
  90  //
  91  // An implementation could support both interfaces in two different types, but then
  92  // additional interfaces would be needed to convert between those types in FromSlogHandler
  93  // and ToSlogHandler.
  94  type SlogSink interface {
  95  	LogSink
  96  
  97  	Handle(ctx context.Context, record slog.Record) error
  98  	WithAttrs(attrs []slog.Attr) SlogSink
  99  	WithGroup(name string) SlogSink
 100  }
 101