Multiple Logs package "go_multi_log" is a Go package that provides logging with multiple loggers (console, file and custom loggers) with different log levels.
Register Multiple Logger Types
- Console logger (defaults)
- File logger
- Custom logger
- Default log level is
Info
, which means that onlyInfo
,Warning
,Error
andFatal
messages will be logged, butDebug
andTrace
messages will be skipped. - The function
Fatal
logs the messages and then callspanic
. It can be used for fatal errors and it will ensure storing the log into a file before closing the application.
import (
"github.com/takecontrolsoft/go_multi_log/logger"
"github.com/takecontrolsoft/go_multi_log/logger/levels"
"github.com/takecontrolsoft/go_multi_log/logger/loggers"
)
logger.Info("Test info log message")
logger.Warning("Test warning log message")
logger.Error("Test error log message")
logger.Fatal("Test log fatal message")
go get -u "github.com/takecontrolsoft/go_multi_log@{commit hash}"
err := callFunction()
if err!=nil{
logger.Error(err)
}
type person struct{ Name string }
person := person{Name: "Michael"}
logger.Info(person)
Using formatting functions DebugF
, TraceF
, InfoF
, WarningF
, ErrorF
, FatalF
type person struct{ Name string }
type car struct{ Year string }
person := person{Name: "Michael"}
car := car{Year: "2020"}
logger.InfoF("Person: %v, Car: %v", person, car)
To change the default log level use SetLevel(levels.All)
. This will cause all the messages for levels greater or equal to the new level also to be logged. The level is changed for the whole application. Avoid changing the level inside Goroutine (go lightweight thread).
currentLevel:= logger.DefaultLogger().GetLevel()
logger.DefaultLogger().SetLevel(levels.All)
logger.Debug("Test debug log message")
logger.Trace("Test trace log message")
No messages will be logged after calling Stop
function.
Logging could be resumed with calling Start
function.
In this example only "Message 2" will be logged.
logger.DefaultLogger().Stop()
logger.Info("Message 1")
logger.DefaultLogger().Start()
logger.Info("Message 2")
Use the following functions to register, unregister or get loggers by key. One default logger always exists and can not be unregistered, but can be stopped.
key:="new_logger_key"
// where the key must be unique, because more than one instances of the same logger type can be registered.
logger.RegisterLogger(key, logger)
// where `logger` implements `loggers.LoggerInterface` and can support different log levels and log destinations.
logger.GetLogger(key)
// will return an instance of the logger by key.
logger.UnregisterLogger(key)
// will delete a specific logger from the collection with registered loggers.
logger.DefaultLogger()
// will return an instance of the default logger of type `ConsoleLogger`
A simple ConsoleLogger
is set by default and it can be obtained from logger.DefaultLogger()
. This logger can not be unregistered, but it can be stopped and resumed.
logger.DefaultLogger().Stop()
Custom console log can be registered for example to log only Debug messages using new formatting string. See the example:
c := loggers.NewConsoleLogger(levels.Debug, "***debug:'%s'")
err := logger.RegisterLogger("debug_log_key", c)
FileLogger
can be added as an additional logger to prints the messages to files.
- Default LogLevel is levels.Info.
- Default FileOptions are used:
- Directory: current executable directory.
- FilePrefix: "mLog".
- FileExtension: ".log".
f := loggers.NewFileLoggerDefault()
err := logger.RegisterLogger("file_logger_key", f)
level := levels.Error
format := "***error:'%s'"
fileOptions := loggers.FileOptions{
Directory: "./",
FilePrefix: "tLog",
FileExtension: ".txt",
}
f := loggers.NewFileLogger(level, format, fileOptions)
err := logger.RegisterLogger("txt_file_key", f)
Custom loggers implementations can be easily added by implementing the interface loggers.LoggerInterface
or deriving the base class loggers.LoggerType
, which already implements most of the function.
- Create a new file
json_logger.go
. - Import
go_multi_log
package.
import (
"github.com/takecontrolsoft/go_multi_log/logger/levels"
"github.com/takecontrolsoft/go_multi_log/logger/loggers"
)
- Create
JsonLogger
type.
//json_logger.go
// Define new type
type JsonLogger struct {
loggers.LoggerType
}
// Create function for initializing
func NewJsonLogger(level levels.LogLevel) *JsonLogger {
return &JsonLogger{
LoggerType: loggers.LoggerType{Level: level},
}
}
// Implement Log function
func (logger *JsonLogger) Log(level levels.LogLevel, arg any) {
if logger.IsLogAllowed(level) {
// Define your format here
format := fmt.Sprintf("{%s: %s},", strings.ToLower(level.String()), "%v")
logger.LogF(level, format, arg)
}
}
// Implement LogF function
func (logger *JsonLogger) LogF(level levels.LogLevel, format string, args ...interface{}) {
if logger.IsLogAllowed(level) {
// You can send the messages in Json format to an external service here
fmt.Printf(format, args...)
}
}
err := logger.RegisterLogger("json_key", NewJsonLogger(levels.Info))
// ... error check...
logger.Error("Test log error message")
logger.Info("Test log info message")
- Go version 1.21 is required.
- Create and go to folder
go_multi_log
. - run the following commands:
git clone https://github.com/takecontrolsoft/go_multi_log.git
go build -v ./...
go test -v ./...
See CONTRIBUTING.md for instructions about building the source.
Multiple Logs package ("go_multi_log") is published under the Apache License 2.0.
The package is created and maintained by "Take Control - software & infrastructure"
The "Go" name and logo are trademarks owned by Google.