A simple, pretty-printed JSON logging package for Go that provides structured logging with automatic trace information for errors.
- Structured JSON logging with pretty-printed output
- Three log levels: Info, Warn, and Error
- Automatic trace capture for errors (file, function, and line number)
- Context support for adding additional fields to log entries
- Color-coded output via go-prettyjson
go get github.com/xDeFc0nx/nlogpackage main
import "github.com/xDeFc0nx/nlog"
func main() {
// Info logging
nlog.Info("Application started", nil)
// Info with context
nlog.Info("User logged in", map[string]any{
"user_id": 12345,
"ip": "192.168.1.1",
})
// Warning
nlog.Warn("API rate limit approaching", map[string]any{
"remaining": 10,
"limit": 100,
})
}func processData() error {
err := someOperation()
if err != nil {
nlog.Error(err, map[string]any{
"operation": "data_processing",
"input_size": 1024,
})
return err
}
return nil
}Output:
{
"level": "ERROR",
"msg": "connection timeout",
"trace": "main.go:42 main.processData",
"operation": "data_processing",
"input_size": 1024
}Logs an informational message with optional context fields.
- msg: The log message
- context: Additional key-value pairs to include in the log entry (can be
nil)
Logs a warning message with optional context fields.
- msg: The log message
- context: Additional key-value pairs to include in the log entry (can be
nil)
Logs an error with automatic trace information (file, function, line number) and optional context fields.
- err: The error to log (if
nil, nothing is logged) - context: Additional key-value pairs to include in the log entry (can be
nil)
- github.com/hokaccha/go-prettyjson - For pretty-printed, color-coded JSON output
MIT