log

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2025 License: BSD-3-Clause Imports: 10 Imported by: 0

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

View Source
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

View Source
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 Debug

func Debug(args ...any)

func Debugf

func Debugf(template string, args ...any)

Debugf formats the message according to the format specifier and logs it.

func Debugln

func Debugln(args ...any)

func Debugw

func Debugw(msg string, ksAndvs ...any)

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func Error

func Error(args ...any)

func Errorf

func Errorf(template string, args ...any)

Errorf formats the message according to the format specifier and logs it.

func Errorln

func Errorln(args ...any)

func Errorw

func Errorw(msg string, keysAndValues ...any)

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func Fatal

func Fatal(args ...any)

func Fatalf

func Fatalf(template string, args ...any)

Fatalf formats the message according to the format specifier and calls os.Exit.

func Fatalln

func Fatalln(args ...any)

func Fatalw

func Fatalw(msg string, ksAndvs ...any)

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 Info

func Info(args ...any)

func Infof

func Infof(template string, args ...any)

Infof formats the message according to the format specifier and logs it.

func Infoln

func Infoln(args ...any)

func Infow

func Infow(msg string, ksAndvs ...any)

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func Panic

func Panic(args ...any)

func Panicf

func Panicf(template string, args ...any)

Panicf formats the message according to the format specifier and panics.

func Panicln

func Panicln(args ...any)

func Panicw

func Panicw(msg string, keysAndValues ...any)

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.

func Warn

func Warn(args ...any)

func Warnf

func Warnf(template string, args ...any)

Warnf formats the message according to the format specifier and logs it.

func Warnln

func Warnln(args ...any)

func Warnw

func Warnw(msg string, keysAndValues ...any)

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

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) Validate added in v0.1.1

func (opt *Options) Validate() error

func (*Options) WithCompress added in v0.1.1

func (opt *Options) WithCompress(compress bool) *Options

func (*Options) WithDirectory added in v0.1.1

func (opt *Options) WithDirectory(dir string) *Options

func (*Options) WithDisableCaller added in v0.1.1

func (opt *Options) WithDisableCaller(disableCaller bool) *Options

func (*Options) WithDisableSplitError added in v0.1.1

func (opt *Options) WithDisableSplitError(disableSplitError bool) *Options

func (*Options) WithDisableStacktrace added in v0.1.1

func (opt *Options) WithDisableStacktrace(disableStacktrace bool) *Options

func (*Options) WithFormat added in v0.1.1

func (opt *Options) WithFormat(format string) *Options

func (*Options) WithLevel added in v0.1.1

func (opt *Options) WithLevel(level string) *Options

func (*Options) WithMaxBackups added in v0.1.1

func (opt *Options) WithMaxBackups(maxBackups int) *Options

func (*Options) WithMaxSize added in v0.1.1

func (opt *Options) WithMaxSize(maxSize int) *Options

func (*Options) WithPrefix added in v0.1.1

func (opt *Options) WithPrefix(prefix string) *Options

func (*Options) WithTimeLayout added in v0.1.1

func (opt *Options) WithTimeLayout(timeLayout string) *Options

type ZiwiLog added in v0.1.1

type ZiwiLog struct {
	zapcore.Encoder
	// contains filtered or unexported fields
}

ZiwiLog is the implement of Logger interface. It wraps zap.Logger.

func NewLogger

func NewLogger(opts *Options) *ZiwiLog

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) Debug added in v0.1.1

func (l *ZiwiLog) Debug(args ...any)

func (*ZiwiLog) Debugf added in v0.1.1

func (l *ZiwiLog) Debugf(template string, args ...any)

Debugf formats the message according to the format specifier and logs it.

func (*ZiwiLog) Debugln added in v0.1.1

func (l *ZiwiLog) Debugln(args ...any)

func (*ZiwiLog) Debugw added in v0.1.1

func (l *ZiwiLog) Debugw(msg string, ksAndvs ...any)

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*ZiwiLog) EncodeEntry added in v0.1.1

func (l *ZiwiLog) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error)

EncodeEntry encodes the entry and fields into a buffer.

func (*ZiwiLog) Error added in v0.1.1

func (l *ZiwiLog) Error(args ...any)

func (*ZiwiLog) Errorf added in v0.1.1

func (l *ZiwiLog) Errorf(template string, args ...any)

Errorf formats the message according to the format specifier and logs it.

func (*ZiwiLog) Errorln added in v0.1.1

func (l *ZiwiLog) Errorln(args ...any)

func (*ZiwiLog) Errorw added in v0.1.1

func (l *ZiwiLog) Errorw(msg string, ksAndVs ...any)

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*ZiwiLog) Fatal added in v0.1.1

func (l *ZiwiLog) Fatal(args ...any)

func (*ZiwiLog) Fatalf added in v0.1.1

func (l *ZiwiLog) Fatalf(template string, args ...any)

Fatalf formats the message according to the format specifier and calls os.Exit.

func (*ZiwiLog) Fatalln added in v0.1.1

func (l *ZiwiLog) Fatalln(args ...any)

func (*ZiwiLog) Fatalw added in v0.1.1

func (l *ZiwiLog) Fatalw(msg string, ksAndvs ...any)

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) Info added in v0.1.1

func (l *ZiwiLog) Info(args ...any)

func (*ZiwiLog) Infof added in v0.1.1

func (l *ZiwiLog) Infof(template string, args ...any)

Infof formats the message according to the format specifier and logs it.

func (*ZiwiLog) Infoln added in v0.1.1

func (l *ZiwiLog) Infoln(args ...any)

func (*ZiwiLog) Infow added in v0.1.1

func (l *ZiwiLog) Infow(msg string, keysAndValues ...any)

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

func (*ZiwiLog) Panic added in v0.1.1

func (l *ZiwiLog) Panic(args ...any)

func (*ZiwiLog) Panicf added in v0.1.1

func (l *ZiwiLog) Panicf(template string, args ...any)

Panicf formats the message according to the format specifier and panics.

func (*ZiwiLog) Panicln added in v0.1.1

func (l *ZiwiLog) Panicln(args ...any)

func (*ZiwiLog) Panicw added in v0.1.1

func (l *ZiwiLog) Panicw(msg string, ksAndvs ...any)

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 added in v0.1.1

func (l *ZiwiLog) Sync()

Sync flushs any buffered log entries. Applications should take care to call Sync before exiting.

func (*ZiwiLog) Warn added in v0.1.1

func (l *ZiwiLog) Warn(args ...any)

func (*ZiwiLog) Warnf added in v0.1.1

func (l *ZiwiLog) Warnf(template string, args ...any)

Warnf formats the message according to the format specifier and logs it.

func (*ZiwiLog) Warnln added in v0.1.1

func (l *ZiwiLog) Warnln(args ...any)

func (*ZiwiLog) Warnw added in v0.1.1

func (l *ZiwiLog) Warnw(msg string, keysAndValues ...any)

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL