# README
Hertz zerolog
This is a logger library that uses zerolog to implement the Hertz logger interface
Usage
Download and install it:
go get github.com/hertz-contrib/logger/zerolog
Import it in your code:
import hertzZerolog "github.com/hertz-contrib/logger/zerolog"
Simple example:
import (
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
hertzZerolog "github.com/hertz-contrib/logger/zerolog"
)
func main () {
h := server.Default()
hlog.SetLogger(hertzZerolog.New())
h.GET("/ping", func(ctx context.Context, c *app.RequestContext) {
hlog.Warn("test log")
c.JSON(consts.StatusOK, utils.H{"ping": "pong"})
})
h.Spin()
}
Options
Example:
import (
"os"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
hertzZerolog "github.com/hertz-contrib/logger/zerolog"
)
func main () {
h := server.Default()
hlog.SetLogger(hertzZerolog.New(
hertzZerolog.WithOutput(os.Stdout), // allows to specify output
hertzZerolog.WithLevel(hlog.LevelInfo), // option with log level
hertzZerolog.WithTimestamp(), // option with timestamp
hertzZerolog.WithCaller())) // option with caller
h.GET("/ping", func(ctx context.Context, c *app.RequestContext) {
hlog.Info("test log")
c.JSON(consts.StatusOK, utils.H{"ping": "pong"})
})
h.Spin()
}
Advanced usage
Implementing a request logging middleware:
import (
"context"
"time"
"github.com/cloudwego/hertz/pkg/app"
hertzZerolog "github.com/hertz-contrib/logger/zerolog"
)
// RequestIDHeaderValue value for the request id header
const RequestIDHeaderValue = "X-Request-ID"
// LoggerMiddleware middleware for logging incoming requests
func LoggerMiddleware() app.HandlerFunc {
return func(c context.Context, ctx *app.RequestContext) {
start := time.Now()
logger, err := hertzZerolog.GetLogger()
if err != nil {
hlog.Error(err)
ctx.Next(c)
return
}
reqId := c.Value(RequestIDHeaderValue).(string)
if reqId != "" {
logger = logger.WithField("request_id", reqId)
}
c = logger.WithContext(c)
defer func() {
stop := time.Now()
logUnwrap := logger.Unwrap()
logUnwrap.Info().
Str("remote_ip", ctx.ClientIP()).
Str("method", string(ctx.Method())).
Str("path", string(ctx.Path())).
Str("user_agent", string(ctx.UserAgent())).
Int("status", ctx.Response.StatusCode()).
Dur("latency", stop.Sub(start)).
Str("latency_human", stop.Sub(start).String()).
Msg("request processed")
}()
ctx.Next(c)
}
}
# Functions
From returns a new Logger instance using an existing logger.
GetLogger returns the default logger instance.
MultiLevelWriter may be used to send the log message to multiple outputs.
New returns a new Logger instance.
WithCaller adds a caller field to the logger's context.
WithCallerSkipFrameCount adds a caller field to the logger's context The specified skipFrameCount int will override the global CallerSkipFrameCount for this context's respective logger.
WithField adds a field to the logger's context.
WithFields adds fields to the logger's context.
WithFormattedTimestamp adds a formatted timestamp field to the logger's context.
WithHook adds a hook to the logger's context.
WithHookFunc adds hook function to the logger's context.
WithLevel allows to specify the level of the logger.
WithOutput allows to specify the output of the logger.
WithTimestamp adds a timestamp field to the logger's context.
# Type aliases
ConsoleWriter parses the JSON input and writes it in an (optionally) colorized, human-friendly format to Out.
No description provided by the author