Documentation
¶
Overview ¶
Package log provides a structured logging facility for the Ziwi framework.
This package is built on top of the zap logging library (go.uber.org/zap) and provides a simplified interface for common logging patterns. It supports various log levels, structured logging with key-value pairs, and different output formats.
Features:
- Multiple log levels: Debug, Info, Warn, Error, Panic, Fatal
- Structured logging with key-value pairs
- Printf-style logging with format strings
- Println-style logging
- JSON and console output formats
- Configurable time layout
- Log file rotation by date
- Separate error log files
- Optional caller information and stack traces
Basic Usage:
// Initialize with custom options
opts := log.NewOptions()
opts.Level = "debug"
opts.Format = "json"
log.Init(opts)
// Simple logging
log.Debug("Debug message")
log.Info("Info message")
log.Warn("Warning message")
log.Error("Error message")
// Structured logging with key-value pairs
log.Infow("User logged in", "user_id", 123, "ip", "192.168.1.1")
log.Errorw("Database connection failed", "error", err, "retry", true)
// Format string logging
log.Debugf("Processing item %d of %d", i, total)
log.Errorf("Failed to connect to %s: %v", host, err)
Configuration: The logger can be configured through the Options struct:
type Options struct {
Prefix string // Log prefix, e.g., "ZIWI"
Directory string // Log file directory, e.g., "logs"
Level string // "debug", "info", "warn", "error", "dpanic", "panic", "fatal"
TimeLayout string // Time format, default: "2006-01-02 15:04:05.000"
Format string // "console" or "json"
DisableCaller bool // Disable caller information
DisableStacktrace bool // Disable stack traces
DisableSplitError bool // Disable separate error log files
}
Example configuration in ziwi.yaml:
log: disable-caller: false disable-stacktrace: false level: debug format: console output-paths: [/tmp/ziwi.log, stdout]
Custom Logger: You can create a custom logger instance for specific components:
customOpts := log.NewOptions()
customOpts.Prefix = "MYCOMPONENT"
customLogger := log.NewLogger(customOpts)
customLogger.Info("Component initialized")
Package log is a log package.
Index ¶
- Constants
- Variables
- func Debug(args ...any)
- func Debugf(template string, args ...any)
- func Debugln(args ...any)
- func Debugw(msg string, ksAndvs ...any)
- func Error(args ...any)
- func Errorf(template string, args ...any)
- func Errorln(args ...any)
- func Errorw(msg string, keysAndValues ...any)
- func Fatal(args ...any)
- func Fatalf(template string, args ...any)
- func Fatalln(args ...any)
- func Fatalw(msg string, ksAndvs ...any)
- func Info(args ...any)
- func Infof(template string, args ...any)
- func Infoln(args ...any)
- func Infow(msg string, ksAndvs ...any)
- func Panic(args ...any)
- func Panicf(template string, args ...any)
- func Panicln(args ...any)
- func Panicw(msg string, keysAndValues ...any)
- func Sync()
- func Warn(args ...any)
- func Warnf(template string, args ...any)
- func Warnln(args ...any)
- func Warnw(msg string, keysAndValues ...any)
- type Logger
- type Options
- func (opt *Options) Validate() error
- func (opt *Options) WithCompress(compress bool) *Options
- func (opt *Options) WithDirectory(dir string) *Options
- func (opt *Options) WithDisableCaller(disableCaller bool) *Options
- func (opt *Options) WithDisableSplitError(disableSplitError bool) *Options
- func (opt *Options) WithDisableStacktrace(disableStacktrace bool) *Options
- func (opt *Options) WithFormat(format string) *Options
- func (opt *Options) WithLevel(level string) *Options
- func (opt *Options) WithMaxBackups(maxBackups int) *Options
- func (opt *Options) WithMaxSize(maxSize int) *Options
- func (opt *Options) WithPrefix(prefix string) *Options
- func (opt *Options) WithTimeLayout(timeLayout string) *Options
- type ZiwiLog
- func (l *ZiwiLog) Debug(args ...any)
- func (l *ZiwiLog) Debugf(template string, args ...any)
- func (l *ZiwiLog) Debugln(args ...any)
- func (l *ZiwiLog) Debugw(msg string, ksAndvs ...any)
- func (l *ZiwiLog) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error)
- func (l *ZiwiLog) Error(args ...any)
- func (l *ZiwiLog) Errorf(template string, args ...any)
- func (l *ZiwiLog) Errorln(args ...any)
- func (l *ZiwiLog) Errorw(msg string, ksAndVs ...any)
- func (l *ZiwiLog) Fatal(args ...any)
- func (l *ZiwiLog) Fatalf(template string, args ...any)
- func (l *ZiwiLog) Fatalln(args ...any)
- func (l *ZiwiLog) Fatalw(msg string, ksAndvs ...any)
- func (l *ZiwiLog) Info(args ...any)
- func (l *ZiwiLog) Infof(template string, args ...any)
- func (l *ZiwiLog) Infoln(args ...any)
- func (l *ZiwiLog) Infow(msg string, keysAndValues ...any)
- func (l *ZiwiLog) Panic(args ...any)
- func (l *ZiwiLog) Panicf(template string, args ...any)
- func (l *ZiwiLog) Panicln(args ...any)
- func (l *ZiwiLog) Panicw(msg string, ksAndvs ...any)
- func (l *ZiwiLog) Sync()
- func (l *ZiwiLog) Warn(args ...any)
- func (l *ZiwiLog) Warnf(template string, args ...any)
- func (l *ZiwiLog) Warnln(args ...any)
- func (l *ZiwiLog) Warnw(msg string, keysAndValues ...any)
Constants ¶
const ( DefaultPrefix = "ZIWI_" DefaultLevel = zapcore.InfoLevel DefaultTimeLayout = "2006-01-02 15:04:05.000" DefaultFormat = "console" // console style DefaultDisableCaller = false DefaultDisableStacktrace = false DefaultDisableSplitError = true DefaultMaxSize = 100 // 100MB DefaultMaxBackups = 3 // Keep 3 old log files DefaultCompress = false // Not compress rotated log files )
Variables ¶
var DefaultDirectory = func() string { home, err := os.UserHomeDir() if err != nil { panic(fmt.Sprintf("Failed to get user home directory: %v", err)) } return filepath.Join(home, "logs") }()
DefaultDirectory returns the default log directory, which is typically the user's home directory joined with "logs".
Functions ¶
func Debugw ¶
Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Errorw ¶
Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Fatalw ¶
Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.
func Infow ¶
Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func Panicw ¶
Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.
func Sync ¶
func Sync()
Sync flushs any buffered log entries. Applications should take care to call Sync before exiting.
Types ¶
type Logger ¶
type Logger interface {
Sync()
Debug(args ...any)
Debugf(template string, args ...any)
Debugw(msg string, keysAndValues ...any)
Debugln(args ...any)
Info(args ...any)
Infof(template string, args ...any)
Infow(msg string, keysAndValues ...any)
Infoln(args ...any)
Warn(args ...any)
Warnf(template string, args ...any)
Warnw(msg string, keysAndValues ...any)
Warnln(args ...any)
Error(args ...any)
Errorf(template string, args ...any)
Errorw(msg string, keysAndValues ...any)
Errorln(args ...any)
Panic(args ...any)
Panicf(template string, args ...any)
Panicw(msg string, keysAndValues ...any)
Panicln(args ...any)
Fatal(args ...any)
Fatalf(template string, args ...any)
Fatalw(msg string, keysAndValues ...any)
Fatalln(args ...any)
}
Logger defines the interface, includes the supported logging methods. For each log level, it exposes four methods:
- methods named after the log level for log.Print-style logging
- methods ending in "w" for loosely-typed structured logging (read as "info with")
- methods ending in "f" for log.Printf-style logging
- methods ending in "ln" for log.Println-style logging
type Options ¶
type Options struct {
Prefix string // Log Prefix, e.g ZIWI
Directory string // Log File Directory, e.g logs
Level string // Log Level, "debug", "info", "warn", "error"
TimeLayout string // Time Layout, e.g "2006-01-02 15:04:05.000"
Format string // Log Format, "console", "json"
DisableCaller bool // Disable caller information
DisableStacktrace bool // Disable stack traces
DisableSplitError bool // Disable separate error log files
// Log rotation settings
MaxSize int // Maximum size of log files in megabytes before rotation
MaxBackups int // Maximum number of old log files to retain
Compress bool // Whether to compress rotated log files
}
Options for logger
func NewOptions ¶
func NewOptions() *Options
NewOptions return the default Options.
Default:
Prefix: "ZIWI_", Directory: "$HOME/logs", Level: "info", TimeLayout: "2006-01-02 15:04:05.000", Format: "console", DisableCaller: false, DisableStacktrace: false, DisableSplitError: false, // Default log rotation settings MaxSize: 100, // 100MB MaxBackups: 3, // Keep 3 old log files Compress: false,
func (*Options) WithCompress ¶
func (*Options) WithDirectory ¶
func (*Options) WithDisableCaller ¶
func (*Options) WithDisableSplitError ¶
func (*Options) WithDisableStacktrace ¶
func (*Options) WithFormat ¶
func (*Options) WithMaxBackups ¶
func (*Options) WithMaxSize ¶
func (*Options) WithPrefix ¶
func (*Options) WithTimeLayout ¶
type ZiwiLog ¶
ZiwiLog is the implement of Logger interface. It wraps zap.Logger.
func NewLogger ¶
NewLogger creates a new logger instance. It will initialize the global logger instance with the specified options.
Returns:
- *ZiwiLog: The new logger instance.
func (*ZiwiLog) Debugw ¶
Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*ZiwiLog) EncodeEntry ¶
EncodeEntry encodes the entry and fields into a buffer.
func (*ZiwiLog) Errorw ¶
Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*ZiwiLog) Fatalf ¶
Fatalf formats the message according to the format specifier and calls os.Exit.
func (*ZiwiLog) Fatalw ¶
Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.
func (*ZiwiLog) Infow ¶
Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*ZiwiLog) Panicw ¶
Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.
func (*ZiwiLog) Sync ¶
func (l *ZiwiLog) Sync()
Sync flushs any buffered log entries. Applications should take care to call Sync before exiting.