https://github.com/goapt/logger
golang logger
https://github.com/goapt/logger
go-lang logrus sentry
Last synced: about 1 month ago
JSON representation
golang logger
- Host: GitHub
- URL: https://github.com/goapt/logger
- Owner: goapt
- License: mit
- Created: 2018-06-26T14:15:42.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2026-01-14T02:57:47.000Z (5 months ago)
- Last Synced: 2026-01-14T05:39:15.607Z (5 months ago)
- Topics: go-lang, logrus, sentry
- Language: Go
- Size: 95.7 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Logger
Golang logger,use log/slog, support logroate
## Default logger
default log out to os.Stderr
```go
import "github.com/goapt/logger"
func main(){
logger.New(&logger.Config{
Mode: logger.ModeStd,
Level: slog.LevelInfo,
})
}
```
## Setting default logger
```
logger.SetDefault(logger.New(&logger.Config{
Mode: logger.ModeFile,
Level: slog.LevelInfo,
FileName: "app.log",
Detail: true,
}))
```
## Usage logger
```
// global logger
logger.Default().Info("this is new log",slog.String("key","value"))
// or
func Foo(log *slog.Logger) {
log.Info("this is new log",slog.String("key","value"))
}
Foo(logger.Default())
```
## Print filename and line no
if `Detail` is true,the log data add filename and line no
```
{"file":"/Users/fifsky/wwwroot/go/library/src/github.com/fifsky/goblog/handler/index.go","func":"handler.IndexGet","level":"debug","line":16,"msg":"[test]","time":"2018-08-02 22:37:02"}
```
# Record the logs of the HTTP Handler and HTTP Client
Use sloghttp to record HTTP server and client request/response data as structured logs for observability and search. Supports request ID correlation, request/response body capture with truncation, sensitive header redaction, flexible filters, and OpenTelemetry Trace/Span extraction. Code forked from https://github.com/samber/slog-http, with additional support for HTTP client logging.
## Server Handler Logging
```go
package main
import (
"log/slog"
"net/http"
"github.com/goapt/logger"
"github.com/goapt/logger/sloghttp"
)
func main() {
// Initialize default logger (JSON to stdout)
logger.SetDefault(logger.New(&logger.Config{
Mode: logger.ModeStd,
Level: slog.LevelInfo,
Detail: false,
}))
// Configure HTTP logging middleware (enable fields as needed)
cfg := sloghttp.DefaultConfig
cfg.Level = slog.LevelInfo // log level for HTTP logs
cfg.WithRequestBody = false // capture request body (on by default; truncated)
cfg.WithResponseBody = false // capture response body (on by default; truncated)
cfg.WithRequestHeader = false // include request headers (sensitive headers redacted)
cfg.WithResponseHeader = false // include response headers (sensitive headers redacted)
// Optional: filter routes/methods/status codes you don’t want to log
// cfg.Filters = []sloghttp.Filter{
// sloghttp.IgnorePathPrefix("/health", "/metrics"),
// sloghttp.IgnoreMethod(http.MethodOptions),
// }
mw := sloghttp.NewMiddleware(logger.Default(), cfg)
mux := http.NewServeMux()
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
// Enrich with business attributes via Context; included in logs
ctx := sloghttp.NewContextAttributes(r.Context(), slog.String("user_id", "123"))
r = r.WithContext(ctx)
// Retrieve request ID for business log correlation
reqID := sloghttp.GetRequestID(r)
logger.Default().Info("handle hello", slog.String("request_id", reqID))
w.Write([]byte("ok"))
})
// Start server with middleware
_ = http.ListenAndServe(":8080", mw(mux))
}
```
## HTTP Client Logging
```go
package main
import (
"context"
"log/slog"
"net/http"
"strings"
"github.com/goapt/logger"
"github.com/goapt/logger/sloghttp"
)
func main() {
logger.SetDefault(logger.New(&logger.Config{
Mode: logger.ModeStd,
Level: slog.LevelInfo,
}))
cfg := sloghttp.DefaultConfig
cfg.Level = slog.LevelInfo // log level for HTTP logs
cfg.WithRequestBody = true // capture client request body
cfg.WithResponseBody = true // capture client response body
// Wrap Transport with sloghttp RoundTripper
rt := sloghttp.NewRoundTripper(logger.Default(), http.DefaultTransport, cfg)
client := &http.Client{Transport: rt}
ctx := sloghttp.NewContextAttributes(context.Background(), slog.String("order_id", "A1001"))
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "http://example.com/api", strings.NewReader("payload"))
res, err := client.Do(req)
if err != nil {
logger.Default().Error("http request failed", slog.String("error", err.Error()))
return
}
defer res.Body.Close()
}
```
## Advanced
- Sensitive headers are redacted by default: authorization/cookie/x-auth-token, etc.
- Customize capture size: adjust global max bytes for request/response body.
```go
// Max bytes captured for request/response bodies (default 64KB)
sloghttp.RequestBodyMaxSize = 128 * 1024
sloghttp.ResponseBodyMaxSize = 128 * 1024
```
- OpenTelemetry integration: when `span.IsRecording()` is true, `trace_id` and `span_id` are recorded automatically.
## Config Fields
- Level: control log level
- WithUserAgent / WithRequestBody / WithRequestHeader: request switches
- WithResponseBody / WithResponseHeader: response switches
- request_id: always generated/propagated via `X-Request-Id` header
- trace_id/span_id: auto extracted when `span.IsRecording()` is true
- Filters: plug Accept*/Ignore* filters (method, path, status, host, etc.)
Filter example:
```go
cfg.Filters = []sloghttp.Filter{
sloghttp.IgnorePathPrefix("/health", "/metrics"),
sloghttp.IgnoreMethod(http.MethodOptions),
sloghttp.AcceptStatusGreaterThanOrEqual(http.StatusBadRequest),
}
```