Documentation
¶
Index ¶
- Constants
- Variables
- func AtomicLevel() zap.AtomicLevel
- func Debug(msg string, fields ...zapcore.Field)
- func DebugContext(ctx context.Context, msg string, fields ...zapcore.Field)
- func Error(msg string, fields ...zapcore.Field)
- func ErrorContext(ctx context.Context, msg string, fields ...zapcore.Field)
- func Fatal(msg string, fields ...zapcore.Field)
- func FatalContext(ctx context.Context, msg string, fields ...zapcore.Field)
- func GetDefaultLogger() *otelzap.Logger
- func GetTraceID(ctx context.Context) string
- func Info(msg string, fields ...zapcore.Field)
- func InfoContext(ctx context.Context, msg string, fields ...zapcore.Field)
- func InitLogger(opts ...Option) error
- func Level() zapcore.Level
- func Named(s string) *otelzap.Logger
- func SetLevel(level string) error
- func Sync() error
- func Warn(msg string, fields ...zapcore.Field)
- func WarnContext(ctx context.Context, msg string, fields ...zapcore.Field)
- type Config
- type Option
- func WithAutoEvents(enable bool) Option
- func WithCallerSkip(skip int) Option
- func WithCompress(compress bool) Option
- func WithFile(path string) Option
- func WithJSON() Option
- func WithLevel(level string) Option
- func WithMaxAge(days int) Option
- func WithMaxBackups(maxBackups int) Option
- func WithMaxSize(maxSize int) Option
- func WithStderr() Option
- func WithStdout() Option
- func WithTraceID(on bool) Option
Constants ¶
const LabelLevel = "level"
Variables ¶
var ( // DefaultLogger is never nil: before InitLogger it discards all logs (zap.NewNop). DefaultLogger = otelzap.New(zap.NewNop()) )
Functions ¶
func AtomicLevel ¶ added in v0.2.0
func AtomicLevel() zap.AtomicLevel
AtomicLevel returns the underlying zap.AtomicLevel for advanced use.
func DebugContext ¶ added in v0.0.2
func ErrorContext ¶ added in v0.0.2
func FatalContext ¶ added in v0.0.2
func GetDefaultLogger ¶
func GetTraceID ¶ added in v0.1.0
GetTraceID returns the 32-character lowercase hexadecimal string of the TraceID from the current context. Returns an empty string if there is no Span in the ctx.
func InfoContext ¶ added in v0.0.2
func InitLogger ¶
InitLogger initializes the default logger.
Defaults when called with no options:
- level: info
- output: stdout
Examples:
InitLogger()
InitLogger(WithFile("/var/log/app.log"))
InitLogger(WithFile(path), WithStdout(), WithLevel("debug"), WithJSON())
func Named ¶ added in v0.0.5
Named returns a child logger with the given name.
Logs written through the returned *otelzap.Logger bypass zaplog package helpers: they do NOT go through statistics / trace_id injection / auto span events. Use package-level Info/InfoContext/... when you need those features.
func SetLevel ¶ added in v0.2.0
SetLevel changes the minimum log level at runtime. Supported: debug, info, warn, error. Must be called after InitLogger.
Types ¶
type Config ¶ added in v0.1.0
type Config struct {
lumberjack.Logger
WithTraceID bool
WithAutoEvents bool
// CallerSkip is the number of additional stack frames to skip when
// reporting the caller. Default is 1 (skip zaplog's own Info/Debug/... wrappers).
// Increase by 1 for each extra wrapper layer around zaplog.
CallerSkip int
Level string
// Output sinks. If none are set, InitLogger defaults to stdout.
FilePath string
AlsoStdout bool
AlsoStderr bool
JSON bool
// contains filtered or unexported fields
}
type Option ¶ added in v0.0.7
type Option func(c *Config)
func WithAutoEvents ¶ added in v0.1.1
WithAutoEvents configures the logger to automatically add log messages as events to active spans. When enabled, each log call will add an event to the current span with the log level and message.
func WithCallerSkip ¶ added in v0.2.0
WithCallerSkip sets how many extra stack frames to skip when annotating logs with caller file:line. The default is 1 so direct zaplog.Info/Debug/... calls report the application call site. Pass 2 if you wrap zaplog in another package (e.g. applog.Info -> zaplog.Info).
func WithCompress ¶ added in v0.0.7
func WithJSON ¶ added in v0.2.0
func WithJSON() Option
WithJSON switches the encoder from console to JSON.
func WithLevel ¶ added in v0.2.0
WithLevel sets the minimum log level. Supported: debug, info, warn, error. Default is "info".
func WithMaxBackups ¶ added in v0.0.7
func WithStderr ¶ added in v0.2.0
func WithStderr() Option
WithStderr enables writing logs to os.Stderr.
func WithStdout ¶ added in v0.2.0
func WithStdout() Option
WithStdout enables writing logs to os.Stdout.
func WithTraceID ¶ added in v0.1.0
WithTraceID configures the logger to add `trace_id` field to structured log messages.
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
basic
command
Basic usage: zero-config InitLogger (default level=info, output=stdout), runtime SetLevel, and Sync on exit.
|
Basic usage: zero-config InitLogger (default level=info, output=stdout), runtime SetLevel, and Sync on exit. |
|
file
command
File-only logging with rotation options.
|
File-only logging with rotation options. |
|
noContext
command
Non-context logging + OpenTelemetry metrics (log_total counter).
|
Non-context logging + OpenTelemetry metrics (log_total counter). |
|
teeJSON
command
Container-style setup: JSON logs to stdout, optionally also to a file (tee).
|
Container-style setup: JSON logs to stdout, optionally also to a file (tee). |
|
withContext
command
Context-aware logging with trace_id injection and auto span events.
|
Context-aware logging with trace_id injection and auto span events. |