-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: added mode and mode named Logger interfaces and implementat…
…ions
- Loading branch information
1 parent
806dba3
commit 9679753
Showing
5 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |