-
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.
- Loading branch information
1 parent
b094c08
commit ed798ed
Showing
5 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,36 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
||
log "github.com/kmesiab/go-klogger" | ||
) | ||
|
||
func main() { | ||
|
||
// Set up the default fields to be added to every log message | ||
defaultFields := map[string]interface{}{ | ||
"foo": "bar", | ||
"baz": "qux", | ||
} | ||
|
||
log.SetDefaultFields(defaultFields) | ||
|
||
// Set up the global logger with default preferences | ||
log.InitializeGlobalLogger(logrus.WarnLevel, &logrus.TextFormatter{ | ||
DisableTimestamp: true, | ||
}) | ||
|
||
// Log a message using the `Logf()` function | ||
log.Logf("Hello %s", "World").Warn() | ||
|
||
// Add extra fields to the log message | ||
log.Logf("Hello %s", "World"). | ||
Add("foo", "bar"). | ||
Info() | ||
|
||
// Replace the global logger | ||
log.SetLogger(logrus.New()) | ||
log.Logf("Hello from a new logger!").Info() | ||
|
||
} |
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,8 @@ | ||
module github.com/kmesiab/go-klogger | ||
|
||
go 1.21.5 | ||
|
||
require ( | ||
github.com/sirupsen/logrus v1.9.3 // indirect | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect | ||
) |
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,11 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= | ||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,137 @@ | ||
package go_klogger | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const DefaultLogLevel = logrus.DebugLevel | ||
|
||
var ( | ||
once sync.Once | ||
globalLogger *logrus.Logger | ||
defaultFields map[string]interface{} | ||
) | ||
|
||
type KLogger struct { | ||
Logger *logrus.Logger | ||
Message string `json:"message"` | ||
LogLevel logrus.Level `json:"log_level"` | ||
Data map[string]interface{} `json:"data"` | ||
} | ||
|
||
// InitializeGlobalLogger Optionally allows you to specify the logger level and formatter | ||
// for the global logger. If not called, the global logger will be initialized with default | ||
// preferences. If called multiple times, the global logger will not be overwritten. | ||
// | ||
// This function is thread safe. It is safe to call this function from multiple goroutines. | ||
func InitializeGlobalLogger(level logrus.Level, formatter logrus.Formatter) { | ||
|
||
once.Do(func() { | ||
globalLogger = logrus.New() | ||
globalLogger.SetLevel(level) | ||
globalLogger.SetFormatter(formatter) | ||
}) | ||
} | ||
|
||
// SetLogger let's you replace the global logger. | ||
func SetLogger(logger *logrus.Logger) { | ||
globalLogger = logger | ||
} | ||
|
||
// SetDefaultFields sets the default fields to be added to every log message. | ||
// Typically, this will be properties like "app_name", "app_version", etc. | ||
func SetDefaultFields(fields map[string]interface{}) { | ||
defaultFields = fields | ||
} | ||
|
||
// Logf creates a new logger with the given format and arguments. | ||
func Logf(format string, a ...interface{}) *KLogger { | ||
|
||
// Set up a global logger with default preferences. This is | ||
// only ever done once, whether here or by calling InitializeGlobalLogger | ||
// directly. | ||
InitializeGlobalLogger(DefaultLogLevel, &logrus.JSONFormatter{}) | ||
|
||
// Pass back an instance of a KLogger with the global logger and default properties. | ||
k := &KLogger{ | ||
Logger: globalLogger, | ||
Message: fmt.Sprintf(format, a...), | ||
Data: make(map[string]interface{}), | ||
} | ||
|
||
return k.AddData(defaultFields) | ||
} | ||
|
||
// SetLogLevel sets the log level of the logger. | ||
func (l *KLogger) SetLogLevel(level logrus.Level) *KLogger { | ||
l.LogLevel = level | ||
l.Logger.SetLevel(level) | ||
|
||
return l | ||
} | ||
|
||
// Add adds a key-value pair to the logger's data. | ||
func (l *KLogger) Add(key string, value interface{}) *KLogger { | ||
l.Data[key] = value | ||
|
||
return l | ||
} | ||
|
||
func (l *KLogger) Info() *KLogger { | ||
if l.LogLevel <= logrus.InfoLevel { | ||
l.Logger.WithFields(l.Data).Info(l.Message) | ||
} | ||
|
||
return l | ||
} | ||
|
||
func (l *KLogger) Warn() *KLogger { | ||
if l.LogLevel <= logrus.WarnLevel { | ||
l.Logger.WithFields(l.Data).Warn(l.Message) | ||
} | ||
|
||
return l | ||
} | ||
|
||
func (l *KLogger) Debug() *KLogger { | ||
if l.LogLevel <= logrus.DebugLevel { | ||
l.Logger.WithFields(l.Data).Debug(l.Message) | ||
} | ||
|
||
return l | ||
} | ||
|
||
func (l *KLogger) Error() *KLogger { | ||
if l.LogLevel <= logrus.ErrorLevel { | ||
l.Logger.WithFields(l.Data).Error(l.Message) | ||
} | ||
|
||
return l | ||
} | ||
|
||
func (l *KLogger) Fatal() *KLogger { | ||
if l.LogLevel <= logrus.FatalLevel { | ||
l.Logger.WithFields(l.Data).Fatal(l.Message) | ||
} | ||
|
||
return l | ||
} | ||
|
||
func (l *KLogger) Panic() *KLogger { | ||
if l.LogLevel <= logrus.PanicLevel { | ||
l.Logger.WithFields(l.Data).Panic(l.Message) | ||
} | ||
|
||
return l | ||
} | ||
|
||
func (l *KLogger) AddData(data map[string]interface{}) *KLogger { | ||
for k, v := range data { | ||
l.Data[k] = v | ||
} | ||
|
||
return l | ||
} |