Skip to content

Commit

Permalink
Making it possible to return the log level and impossible to change t…
Browse files Browse the repository at this point in the history
…he level via a sublogger (#4)

Signed-off-by: Shivansh Vij <shivanshvij@loopholelabs.io>
  • Loading branch information
ShivanshVij authored Jul 13, 2024
1 parent 5264caa commit 838b0a7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
18 changes: 13 additions & 5 deletions loggers/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ import "github.com/loopholelabs/logging/types"

var _ types.Logger = (*Logger)(nil)

type Logger struct{}
type Logger struct {
level types.Level
}

func New(level types.Level) *Logger {
return &Logger{level: level}
}

func New() *Logger {
return new(Logger)
func (s *Logger) SetLevel(level types.Level) {
s.level = level
}

func (s *Logger) SetLevel(types.Level) {}
func (s *Logger) Level() types.Level {
return s.level
}

func (s *Logger) SubLogger(string) types.Logger { return s }
func (s *Logger) SubLogger(string) types.SubLogger { return s }

func (s *Logger) Fatal() types.Event {
return new(Event)
Expand Down
28 changes: 17 additions & 11 deletions loggers/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ var (
)

type Logger struct {
logger *slog.Logger
level *slog.LevelVar
output io.Writer
source string
logger *slog.Logger
level types.Level
slogLevel *slog.LevelVar
output io.Writer
source string
}

func New(source string, level types.Level, output io.Writer) *Logger {
Expand All @@ -44,14 +45,18 @@ func newSlog(source string, slogLevel *slog.LevelVar, output io.Writer) *Logger
{Key: types.SourceKey, Value: slog.StringValue(source)},
}))
s := &Logger{
logger: sl,
output: output,
level: slogLevel,
source: source,
logger: sl,
output: output,
slogLevel: slogLevel,
source: source,
}
return s
}

func (s *Logger) Level() types.Level {
return s.level
}

func (s *Logger) SetLevel(level types.Level) {
var slogLevel slog.Level
switch level {
Expand All @@ -68,12 +73,13 @@ func (s *Logger) SetLevel(level types.Level) {
case types.TraceLevel:
slogLevel = slog.LevelDebug - 1
}
s.level.Set(slogLevel)
s.level = level
s.slogLevel.Set(slogLevel)
}

func (s *Logger) SubLogger(source string) types.Logger {
func (s *Logger) SubLogger(source string) types.SubLogger {
sloglevel := new(slog.LevelVar)
sloglevel.Set(s.level.Level())
sloglevel.Set(s.slogLevel.Level())
return newSlog(fmt.Sprintf("%s:%s", s.source, source), sloglevel, s.output)
}

Expand Down
8 changes: 7 additions & 1 deletion loggers/zerolog/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
var _ types.Logger = (*Logger)(nil)

type Logger struct {
level types.Level
logger zerolog.Logger
source string
}
Expand All @@ -33,6 +34,10 @@ func New(source string, level types.Level, output io.Writer) *Logger {
return z
}

func (z *Logger) Level() types.Level {
return z.level
}

func (z *Logger) SetLevel(level types.Level) {
var zerologLevel zerolog.Level
switch level {
Expand All @@ -49,10 +54,11 @@ func (z *Logger) SetLevel(level types.Level) {
case types.TraceLevel:
zerologLevel = zerolog.TraceLevel
}
z.level = level
z.logger.Level(zerologLevel)
}

func (z *Logger) SubLogger(source string) types.Logger {
func (z *Logger) SubLogger(source string) types.SubLogger {
return &Logger{
logger: z.logger,
source: fmt.Sprintf("%s:%s", z.source, source),
Expand Down
6 changes: 3 additions & 3 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
func New(kind Kind, source string, output io.Writer) types.Logger {
switch kind {
case Noop:
return noop.New()
return noop.New(types.InfoLevel)
case Zerolog:
return zerolog.New(source, types.InfoLevel, output)
case Slog:
Expand All @@ -38,10 +38,10 @@ func New(kind Kind, source string, output io.Writer) types.Logger {
}
}

func NewTest(t testing.TB, kind Kind, source string) types.Logger {
func Test(t testing.TB, kind Kind, source string) types.Logger {
switch kind {
case Noop:
return noop.New()
return noop.New(types.InfoLevel)
case Zerolog:
return zerolog.New(source, types.InfoLevel, testingAdapter.New(t))
case Slog:
Expand Down
7 changes: 6 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ const (

type Logger interface {
SetLevel(level Level)
SubLogger(source string) Logger
SubLogger
}

type SubLogger interface {
Level() Level
SubLogger(source string) SubLogger

Fatal() Event
Error() Event
Expand Down

0 comments on commit 838b0a7

Please sign in to comment.