Skip to content

Commit

Permalink
refactor: added mode and mode named Logger interfaces and implementat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
ralvarezdev committed Jan 5, 2025
1 parent 806dba3 commit 9679753
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/ralvarezdev/go-logger

go 1.23.4

require github.com/ralvarezdev/go-flags v0.3.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/ralvarezdev/go-flags v0.3.0 h1:JItUzutZKiVsYxQGRjF9DsGTBvRo3ScadMz5s6YUcKY=
github.com/ralvarezdev/go-flags v0.3.0/go.mod h1:R3yVBYvzwqfOp26LidaiJ/zftVAnPC3pKunVpV/vosE=
33 changes: 33 additions & 0 deletions mode/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mode

import (
goflagsmode "github.com/ralvarezdev/go-flags/mode"
gologgerstatus "github.com/ralvarezdev/go-logger/status"
)

var (
// LogModeMap is the map of the log mode
LogModeMap = map[goflagsmode.Mode]map[gologgerstatus.Status]bool{
goflagsmode.Debug: {
gologgerstatus.Info: true,
gologgerstatus.Error: true,
gologgerstatus.Warning: true,
gologgerstatus.Debug: true,
gologgerstatus.Critical: true,
},
goflagsmode.Dev: {
gologgerstatus.Info: true,
gologgerstatus.Error: true,
gologgerstatus.Warning: true,
gologgerstatus.Debug: false,
gologgerstatus.Critical: true,
},
goflagsmode.Prod: {
gologgerstatus.Info: true,
gologgerstatus.Error: true,
gologgerstatus.Warning: true,
gologgerstatus.Debug: false,
gologgerstatus.Critical: true,
},
}
)
128 changes: 128 additions & 0 deletions mode/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package mode

import (
goflagsmode "github.com/ralvarezdev/go-flags/mode"
gologger "github.com/ralvarezdev/go-logger"
gologgerstatus "github.com/ralvarezdev/go-logger/status"
)

type (
// Logger interface for the mode logger
Logger interface {
ShouldLog(status gologgerstatus.Status) bool
RunIfShouldLog(status gologgerstatus.Status, fn func())
gologger.Logger
}

// DefaultLogger is the default mode logger
DefaultLogger struct {
logger gologger.Logger
}
)

// NewDefaultLogger creates a new default mode logger
func NewDefaultLogger(logger gologger.Logger) (
*DefaultLogger,
error,
) {
// Check if the logger is nil
if logger == nil {
return nil, gologger.ErrNilLogger
}

return &DefaultLogger{logger: logger}, nil
}

// Log logs a message
func (d *DefaultLogger) Log(message *gologger.Message) {
// Check if the message is nil
if message == nil {
return
}

d.RunIfShouldLog(
message.Status(), func() {
d.logger.Log(message)
},
)
}

// ShouldLog checks if the log should be logged
func (d *DefaultLogger) ShouldLog(status gologgerstatus.Status) bool {
return LogModeMap[goflagsmode.ModeFlag.Mode()][status]
}

// RunIfShouldLog runs the function if the log should be logged
func (d *DefaultLogger) RunIfShouldLog(
status gologgerstatus.Status,
fn func(),
) {
if d.ShouldLog(status) {
fn()
}
}

// Info logs an info message
func (d *DefaultLogger) Info(header, subheader string, details *[]string) {
d.RunIfShouldLog(
gologgerstatus.Info, func() {
d.logger.Info(
header,
subheader,
details,
)
},
)
}

// Error logs an error message
func (d *DefaultLogger) Error(header, subheader string, errors *[]error) {
d.RunIfShouldLog(
gologgerstatus.Error, func() {
d.logger.Error(
header,
subheader,
errors,
)
},
)
}

// Debug logs a debug message
func (d *DefaultLogger) Debug(header, subheader string, details *[]string) {
d.RunIfShouldLog(
gologgerstatus.Debug, func() {
d.logger.Debug(
header,
subheader,
details,
)
},
)
}

// Critical logs a critical message
func (d *DefaultLogger) Critical(header, subheader string, details *[]string) {
d.RunIfShouldLog(
gologgerstatus.Critical, func() {
d.logger.Critical(
header,
subheader,
details,
)
},
)
}

// Warning logs a warning message
func (d *DefaultLogger) Warning(header, subheader string, details *[]string) {
d.RunIfShouldLog(
gologgerstatus.Warning, func() {
d.logger.Warning(
header,
subheader,
details,
)
},
)
}
61 changes: 61 additions & 0 deletions mode/named/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package named

import (
gologger "github.com/ralvarezdev/go-logger"
gologgermode "github.com/ralvarezdev/go-logger/mode"
)

type (
// Logger interface for the mode logger
Logger interface {
Info(subheader string, details ...string)
Error(subheader string, errors ...error)
Debug(subheader string, details ...string)
Critical(subheader string, details ...string)
Warning(subheader string, details ...string)
}

// DefaultLogger is the default mode logger
DefaultLogger struct {
header string
logger gologgermode.Logger
}
)

// NewDefaultLogger creates a new default mode logger
func NewDefaultLogger(header string, logger gologgermode.Logger) (
*DefaultLogger,
error,
) {
// Check if the logger is nil
if logger == nil {
return nil, gologger.ErrNilLogger
}

return &DefaultLogger{header: header, logger: logger}, nil
}

// Info logs an info message
func (d *DefaultLogger) Info(subheader string, details ...string) {
d.logger.Info(d.header, subheader, &details)
}

// Error logs an error message
func (d *DefaultLogger) Error(subheader string, errors ...error) {
d.logger.Error(d.header, subheader, &errors)
}

// Debug logs a debug message
func (d *DefaultLogger) Debug(subheader string, details ...string) {
d.logger.Debug(d.header, subheader, &details)
}

// Critical logs a critical message
func (d *DefaultLogger) Critical(subheader string, details ...string) {
d.logger.Critical(d.header, subheader, &details)
}

// Warning logs a warning message
func (d *DefaultLogger) Warning(subheader string, details ...string) {
d.logger.Warning(d.header, subheader, &details)
}

0 comments on commit 9679753

Please sign in to comment.