A logging package for golang.
Here are the main features and these are what I normally need for the development.
- Provides ILogger interface to avoide use a concrete logger definition
- Allows configure the message layout by logging configuration
- Allows configure log message output destination by logging configuration
- Allows configure log level to output destination
- Supports load logging configuration from a file (JSON format)
- Supports log messages to console window
- Supports colorize level message to console window
- Supports log messages to plain text file
- Supports log messages to json file
- Support Windows, MacOS and Linux
- Good performance
Intall the logging package by run go get github.com/andrewlang/matrix-go-logging
, then you will be able to create loggers to log messages.
package main
import (
"github.com/andrewlang/matrix-go-logging"
)
Configure logging to use Console logger with the message layout: [Datetime] [Level] [Logger Name] [Indent] [Message]
The output message you can see in console window
[2020-02-13 15:28:08.716] [Info] [StartClientAction] Start Actionn to response user's request
[2020-02-13 15:28:08.723] [Debug] [Test Station] Test station is started with XXX
import (
logging "github.com/andrewlang/matrix-go-logging"
)
factory := logging.NewLoggerFactory()
config := logging.NewLogTargetConfigurations()
consoleTarget := logging.NewLogTargetConfiguration("Console", logging.ConsoleLoggerName, []string{logging.Time, logging.Level, logging.Name, logging.Indent, logging.Message})
config.AddTarget(consoleTarget)
factory.Configure(config)
Logging factory use a json format file to store the configuration for layouts and loggers
configFile := NewFile("logging.config.json")
factory := logging.NewLoggerFactory().ConfigureFromFile(LoggingConfigFile)
After logging factory configured, you can use factory create an instance of ILogger.
// need a name for the logger
logger, _ := factory.Create("Application")
Here is an example log all level messages to plain text file and only log error level messages to json file.
config := NewLogTargetConfigurations()
// log all messages to txt file
fileConfig := NewLogTargetConfiguration("File", FileLoggerName, []string{Time, Level, Name, Indent, Message})
fileConfig.Configuration.FileName = "./test/compose_logger.txt"
config.AddTarget(fileConfig)
// log Error level messages to json file
jsonConfig := NewLogTargetConfiguration("JsonFile", JSONFileLoggerName, []string{Message})
jsonConfig.Configuration.FileName = "./test/compose_logger.json"
jsonConfig.Configuration.MinLevel = LevelError.Value
config.AddTarget(jsonConfig)
By default, the max log file size is 2M, after reach or over this, a new log file will be generated to keep write messages. You can set the size with following code.
config := NewLogTargetConfigurations()
// log all messages to txt file
fileConfig := NewLogTargetConfiguration("File", FileLoggerName, []string{Time, Level, Name, Indent, Message})
fileConfig.Configuration.FileName = "./test/logger.txt"
fileConfig.Configuration.FileSize = 5 * 1024 * 1024
config.AddTarget(fileConfig)
You can beuild the package by
go build
You can run unit test by
go test -v