Customizable slog.Handler for console.
package main
import (
"github.com/gaebalai/clog"
"log/slog"
)
func main() {
handler := clog.New(
clog.WithColor(true),
clog.WithSource(true),
)
logger := slog.New(handler)
logger.Info("hello, world!", slog.String("foo", "bar"))
logger.Warn("What?", slog.Group("group1", slog.String("foo", "bar")))
logger.WithGroup("hex").Error("Ouch!", slog.Int("num", 123))
}WithWriter: Output writer. Default isos.Stdout.WithLevel: Log level. Default isslog.LevelInfo.WithTimeFmt: Time format string. Default is15:04:05.000.WithColor: Enable colorized output. Default will be changed by terminal's color support.WithColorMap: Color map for each log level. Default isclog.DefaultColorMap. See ColorMap section for more detail.WithSource: Enable source code location. Default is false.WithReplaceAttr: Replace attribute value. It's same withslog.ReplaceAttrinslog.HandlerOptions.WithTemplate: Template string. See Template section for more detail.WithAttrPrinter: Attribute printer. Default isclog.LinearPrinter. See AttrPrinter section for more detail.WithLevelFormatter: Custom function to format log level strings. Default useslevel.String().
You can customize color map for each handler with clog.ColorMap. Default is clog.DefaultColorMap. If the fields is nil or not set, default color will be used.
LogLevel: You can set color for each log level. If not set, default colorLogLevelDefaultwill be used.LogLevelDefault: Default color for log level.Time: Color for time string.Message: Color for log message string.AttrKey: Color for attribute key string. It's applied or not depends on AttrPrinter.AttrValue: Color for attribute value string. It's applied or not depends on AttrPrinter.
Template can be used to customize log format. A developer can use following variables in template string.
.Time: Time string. Format is specifiedWithTimeFmt..Elapsed: Duration from the start of the program.Level: Log level string. e.g.INFO,WARN,ERROR.Message: Log message.FileName: A file name of the source code that calls logger. It is empty if WithSource is not specified.FilePath: A full file path of the source code that calls logger. It is empty if WithSource is not specified..FileLine: A line number of the source code that calls logger. It is empty if WithSource is not specified.FuncNameA function name of the source code that calls logger. It is empty if WithSource is not specified
Default is clog.DefaultTemplate.
AttrPrinter is an interface designed for customizing the way attributes are printed. By default, clog.LinearPrinter is used.
LinearPrinter: Print attributes in a linear way.PrettyPrinter: Print attributes with pp package.IndentPrinter: Print attributes with indent like YAML format.
Full example is here.
func main() {
user := User{
Name: "mizutani",
Email: "mizutani@hey.com",
}
store := Store{
Name: "Jiro",
Address: "Tokyo",
Phone: "123-456-7890",
}
group := slog.Group("info", slog.Any("user", user), slog.Any("store", store))
linearHandler := clog.New(clog.WithPrinter(clog.LinearPrinter))
slog.New(linearHandler).Info("by LinearPrinter", group)
prettyHandler := clog.New(clog.WithPrinter(clog.PrettyPrinter))
slog.New(prettyHandler).Info("by PrettyPrinter", group)
indentHandler := clog.New(clog.WithPrinter(clog.IndentPrinter))
slog.New(indentHandler).Info("by IndentHandler", group)
}Apache License 2.0