go_klogger
is a flexible logging package for Go, built on top of the popular logrus
package.
It provides easy-to-use functions for logging at different levels with support for customizable
fields and global configuration.
To use go_klogger
, first install it using go get
:
go get github.com/kmesiab/go-klogger
Here's a quick guide to get you started:
You can set up default fields which will be added to every log message. Typically, these are properties like "app_name", "app_version", etc.
log.SetDefaultFields(map[string]interface{}{
"app_name": "MyApplication",
"app_version": "1.0.0",
})
You can initialize the global logger with your preferred log level and formatter. This step is optional; if not called, the global logger will be initialized with default preferences.
log.InitializeGlobalLogger(logrus.InfoLevel, &logrus.JSONFormatter{})
Use Logf
to create a new logger instance with a formatted message, and
then chain it with one of the logging methods
(Info
, Warn
, Debug
, Error
, Fatal
, Panic
) based on the required
severity.
log.Logf("This is an %s message", "info").Info()
log.Logf("This is a warning!").Warn()
You can add more key-value pairs to your log data easily.
log.Logf("User login attempt").Add("username", "johndoe").Info()
If needed, you can replace the global logger instance with a new one.
newLogger := logrus.New()
newLogger.SetFormatter(&logrus.TextFormatter{})
log.SetLogger(newLogger)
log.Logf("Logging with a new logger instance").Info()
Here is an example of how to use go_klogger
in your application:
package main
import (
"github.com/sirupsen/logrus"
log "github.com/kmesiab/go-klogger"
)
func main() {
// Set up default fields
log.SetDefaultFields(map[string]interface{}{
"foo": "bar",
"baz": "qux",
})
// Initialize the global logger
log.InitializeGlobalLogger(logrus.WarnLevel, &logrus.TextFormatter{
DisableTimestamp: true,
})
// Log a message
log.Logf("Hello %s", "World").Warn()
// Add extra fields and log at a different level
log.Logf("Hello %s", "World").
Add("additional", "info").
Info()
// Replace the global logger and log a message
log.SetLogger(logrus.New())
log.Logf("Hello from a new logger!").Info()
}
This example demonstrates setting default fields, initializing the global logger with specific preferences, logging messages at different levels, adding additional data to logs, and replacing the global logger.
We welcome contributions! If you'd like to contribute to go_klogger
, please follow
these steps:
- Fork the repository on GitHub.
- Clone your forked repository to your local machine.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them to your branch.
- Push your changes to your GitHub fork.
- Open a Pull Request against the
main
branch ofgo_klogger
repository.
For more details, check out GitHub's guide on forking and creating a pull request.