RainbowLog is an easy-to-use, highly configurable, structured logging library (For Golang). It is designed to provide fast and reliable logging services for other projects of Rainbow Project and is one of the foundations of Rainbow Project.
When designing RainbowLog, we referred to some of the design ideas of ZeroLog, and combined it with some of the features needed in my work to make RainbowLog faster , more efficient, Smaller, more beautiful and easier to use.
How to use
There are instructions for use (in English) in README.md.
Note: README.md may not fully describe all the functions of Rainbow Log. I will add the missing parts to README at the appropriate time.
log.Logger.Info().Msg("Hello world!").Done() log.Logger.Debug().WithLabels("MODEL1").Msg("Something debugging...").Done() log.Logger.Warn().WithLabels("MODEL2", "SERVICE1").Msg("Something warning!").Int("IntegerValue", 888).Done() log.Logger.Error().Msg("failed to do something").Err(errors.New("something wrong")).Done() log.Logger.Fatal().Msg("fatal to do something").Done() }
Output:
Use global logger by config file
RainbowLog supports setting logger options based on configuration files. If you want to use a configuration file, you need to ensure that the configuration file contains Rainbow Log configuration items. Rainbow Log supports configuration files in three formats: .yaml|.json|.toml. For specific configuration templates, see the corresponding files in the config package.
Assume that we have prepared a configuration file rainbowlog.yaml and placed it in the same directory as the execution file, then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package main
import ( "errors"
"github.com/rambollwong/rainbowlog/log" )
funcmain() { log.UseDefaultConfigFile()
log.Logger.Info().Msg("Hello world!").Done() log.Logger.Debug().WithLabels("MODEL1").Msg("Something debugging...").Done() log.Logger.Warn().WithLabels("MODEL2", "SERVICE1").Msg("Something warning!").Int("IntegerValue", 888).Done() log.Logger.Error().Msg("failed to do something").Err(errors.New("something wrong")).Done() log.Logger.Fatal().Msg("fatal to do something").Done() }
If you want to use a .json or .toml type configuration file, you only need to modify the log.DefaultConfigFileName, for example:
1
log.DefaultConfigFileName = "rainbowlog.json"
Or
1
log.DefaultConfigFileName = "rainbowlog.toml"
If you also want to specify the directory where the configuration file is located, you only need to modify log.DefaultConfigFilePath:
Note: Modifying log.DefaultConfigFileName and log.DefaultConfigFilePath needs to be executed before log.UseDefaultConfigFile(), otherwise it will not take effect.
Use global logger by custom options
If you want to use custom options for Global logger, we have reserved the log.UseCustomOptions(opts ...Option) API for implementation. Supported Option is detailed in option.go.
Custom logger
If you don’t want to use Global logger, you can initialize a Logger instance through the New method. The New method receives the Option parameters. Supported Option is detailed in option.go.
logger.Info().Msg("Hello world!").Done() logger.Debug().WithLabels("MODEL1").Msg("Something debugging...").Done() logger.Warn().WithLabels("MODEL2", "SERVICE1").Msg("Something warning!").Int("IntegerValue", 888).Done() logger.Error().Msg("failed to do something").Err(errors.New("something wrong")).Done() logger.Fatal().Msg("fatal to do something").Done() }
SubLogger
SubLogger support allows you to create a logger instance that inherits from the parent logger and can reset some Option when needed. For example, a scenario where a LABEL different from the parent Logger is used in a submodule.
The default time format of RainbowLog is 2006-01-02 15:04:05.000, you can modify this format through WithTimeFormat(timeFormat string) Option, the format can be a string that conforms to golang time formatting rules, it can also be UNIX or UNIXMS or UNIXMICRO or UNIXNANO, They respectively represent the return value of Unix() or UnixMilli() or UnixMicro() or UnixNano() which outputs time.Time.