slogr.go 3.6 KB

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