Documentation ¶
Overview ¶
Package ylog provides a slog.Logger instance for logging. ylog also provides a default slog.Logger, the default logger is build from environment.
ylog allows to call log api directly, like:
ylog.Debug("test", "name", "yomo") ylog.Info("test", "name", "yomo") ylog.Warn("test", "name", "yomo") ylog.Error("test", "name", "yomo")
Package provides handler that supports spliting log stream to common log stream and error log stream.
Example ¶
package main import ( "io" "net" "github.com/yomorun/yomo/core/ylog" ) func main() { // text format logger logger := ylog.NewFromConfig(ylog.Config{ Level: "warn", Format: "text", ErrorOutput: "stdout", DisableTime: true, }) ylog.SetDefault(logger.With("hello", "yomo").WithGroup("syslog")) ylog.Debug("debug", "aaa", "bbb") ylog.Info("info", "ccc", "ddd") ylog.Warn("warn", "eee", "fff") ylog.Error("error", io.EOF, "eee", "fff") // json format logger sysLogger := ylog.NewFromConfig(ylog.Config{ Level: "error", Format: "json", ErrorOutput: "stdout", DisableTime: true, }) sysLogger = sysLogger.WithGroup("syslog") sysLogger.Error("sys error", net.ErrClosed, "ggg", "hhh") }
Output: level=WARN msg=warn hello=yomo syslog.eee=fff level=ERROR msg=error hello=yomo syslog.eee=fff syslog.err=EOF {"level":"ERROR","msg":"sys error","syslog":{"ggg":"hhh","err":"use of closed network connection"}}
Index ¶
- Variables
- func Debug(msg string, keyvals ...interface{})
- func Default() *slog.Logger
- func Error(msg string, err error, keyvals ...interface{})
- func Info(msg string, keyvals ...interface{})
- func NewFromConfig(conf Config) *slog.Logger
- func NewHandlerFromConfig(conf Config) slog.Handler
- func SetDefault(logger *slog.Logger)
- func Warn(msg string, keyvals ...interface{})
- type Config
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DebugFrameSize = 16
DebugFrameSize is use for log dataFrame, It means that only logs the first DebugFrameSize bytes if the data is large than DebugFrameSize bytes.
DebugFrameSize is default to 16, if env `YOMO_DEBUG_FRAME_SIZE` is setted and It's an int number, Set the env value to DebugFrameSize.
Functions ¶
func NewFromConfig ¶
NewFromConfig returns a slog.Logger according to conf.
func NewHandlerFromConfig ¶
NewHandlerFromConfig creates a slog.Handler from conf
Types ¶
type Config ¶
type Config struct { // Verbose indicates if logger log code line, use false for production. Verbose bool `env:"YOMO_LOG_VERBOSE" envDefault:"false"` // Level can be one of `debug`, `info`, `warn`, `error` Level string `env:"YOMO_LOG_LEVEL" envDefault:"info"` // Output is the filename of log file, // The default is stdout. Output string `env:"YOMO_LOG_OUTPUT"` // ErrorOutput is the filename of errlog file, // The default is stderr. ErrorOutput string `env:"YOMO_LOG_ERROR_OUTPUT"` // Format supports text and json, // The default is text. Format string `env:"YOMO_LOG_FORMAT" envDefault:"text"` // MaxSize is the maximum size in megabytes of the log file before it gets rotated. // It defaults to 100 megabytes. MaxSize int `env:"YOMO_LOG_MAX_SIZE"` // MaxBackups is the maximum number of old log files to retain. // The default is to retain all old log files (though MaxAge may still cause them to get deleted.) MaxBackups int `env:"YOMO_LOG_MAX_BACKUPS"` // MaxAge is the maximum number of days to retain old log files based on the timestamp encoded in their filename. // Note that a day is defined as 24 hours and may not exactly correspond to calendar days due to daylight savings, leap seconds, etc. // The default is not to remove old log files based on age. MaxAge int `env:"YOMO_LOG_MAX_AGE"` // LocalTime determines if the time used for formatting the timestamps in backup files is the computer's local time. // The default is to use UTC time. LocalTime bool `env:"YOMO_LOG_LOCAL_TIME"` // Compress determines if the rotated log files should be compressed using gzip. // The default is not to perform compression. Compress bool `env:"YOMO_LOG_COMPRESS"` // DisableTime disable time key, It's a pravited field, Just for testing. DisableTime bool }
Config is the config of slog, the config is from environment.