cherryLogger

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 12 Imported by: 72

Documentation

Index

Constants

View Source
const (
	// KEY_NODE_TYPE is the common-field key for the node type (e.g. "game", "gate").
	KEY_NODE_TYPE = "nodetype"
	// KEY_NODE_ID is the common-field key for the node's unique ID.
	KEY_NODE_ID = "nodeid"
	// ENCODER_JSON_Type selects the JSON encoder when passed to a Config.
	ENCODER_JSON_Type = "json"
)

Variables

View Source
var DefaultLogger = defaultManager.DefaultLogger()

DefaultLogger is the package-level default logger backed by defaultManager. It is always a valid console-logging CherryLogger, never nil.

Functions

func CommonFields added in v1.5.3

func CommonFields() map[string]string

CommonFields returns a copy of the default manager's current common fields.

func DPanic

func DPanic(args ...interface{})

DPanic logs a message at DPanicLevel. In development mode the logger then panics.

func DPanicf

func DPanicf(template string, args ...interface{})

DPanicf formats and logs a message at DPanicLevel. In development mode the logger then panics.

func DPanicw

func DPanicw(msg string, keysAndValues ...interface{})

DPanicw logs a message at DPanicLevel with key-value pairs. In development mode the logger then panics.

func Debug

func Debug(args ...interface{})

Debug logs a message at DebugLevel.

func Debugf

func Debugf(template string, args ...interface{})

Debugf formats and logs a message at DebugLevel.

func Debugw

func Debugw(msg string, keysAndValues ...interface{})

Debugw logs a message at DebugLevel with key-value pairs.

func Enable added in v1.1.29

func Enable(level zapcore.Level) bool

Enable reports whether the given log level is enabled on DefaultLogger.

func Error

func Error(args ...interface{})

Error logs a message at ErrorLevel.

func Errorf

func Errorf(template string, args ...interface{})

Errorf formats and logs a message at ErrorLevel.

func Errorw

func Errorw(msg string, keysAndValues ...interface{})

Errorw logs a message at ErrorLevel with key-value pairs.

func Fatal

func Fatal(args ...interface{})

Fatal logs a message at FatalLevel, then calls os.Exit(1).

func Fatalf

func Fatalf(template string, args ...interface{})

Fatalf formats and logs a message at FatalLevel, then calls os.Exit(1).

func Fatalw

func Fatalw(msg string, keysAndValues ...interface{})

Fatalw logs a message at FatalLevel with key-value pairs, then calls os.Exit(1).

func Flush added in v1.1.0

func Flush()

Flush syncs all loggers in the default manager. Call before shutdown to ensure buffered log entries are written.

func GetLevel added in v1.1.0

func GetLevel(level string) zapcore.Level

GetLevel converts a level name string to a zapcore.Level. Supported values (case-insensitive): "debug", "info", "warn", "error", "panic", "fatal". Unknown values default to DebugLevel.

func Info

func Info(args ...interface{})

Info logs a message at InfoLevel.

func Infof

func Infof(template string, args ...interface{})

Infof formats and logs a message at InfoLevel.

func Infow

func Infow(msg string, keysAndValues ...interface{})

Infow logs a message at InfoLevel with key-value pairs.

func Panic

func Panic(args ...interface{})

Panic logs a message at PanicLevel, then panics.

func Panicf

func Panicf(template string, args ...interface{})

Panicf formats and logs a message at PanicLevel, then panics.

func Panicw

func Panicw(msg string, keysAndValues ...interface{})

Panicw logs a message at PanicLevel with key-value pairs, then panics.

func PrintLevel added in v1.1.29

func PrintLevel(level zapcore.Level) bool

PrintLevel returns true if the given level meets the minimum print threshold set on the default manager.

func RegisterWrapper added in v1.4.22

func RegisterWrapper(w Wrapper)

RegisterWrapper adds a core wrapper to the default manager.

func SetCommonField added in v1.4.21

func SetCommonField(key, value string)

SetCommonField sets a single common field on the default manager. Common fields appear on every log line produced by the manager's loggers.

func SetCommonFields added in v1.5.3

func SetCommonFields(fields map[string]string)

SetCommonFields sets multiple common fields at once on the default manager.

func SetFileNameVar added in v1.3.13

func SetFileNameVar(key, value string)

SetFileNameVar sets a template variable on the default manager. Keys such as "nodetype" or "nodeid" can be used in log file paths via %key placeholders.

func SetNodeLogger

func SetNodeLogger(node cfacade.INode)

SetNodeLogger reads the node's profile, determines the referenced logger name, and replaces DefaultLogger with a fully configured instance (file writer, common fields, fileName vars) from the profile config.

func SetPrintLevel added in v1.5.3

func SetPrintLevel(level zapcore.Level)

SetPrintLevel updates the minimum print threshold on the default manager.

func Warn

func Warn(args ...interface{})

Warn logs a message at WarnLevel.

func Warnf

func Warnf(template string, args ...interface{})

Warnf formats and logs a message at WarnLevel.

func Warnw

func Warnw(msg string, keysAndValues ...interface{})

Warnw logs a message at WarnLevel with key-value pairs.

Types

type CherryLogger added in v1.1.0

type CherryLogger struct {
	Config
	*zap.SugaredLogger
}

CherryLogger wraps zap.SugaredLogger with framework-level Config awareness. It embeds *zap.SugaredLogger (all logging methods) and a value copy of Config that reflects the logger's construction-time settings.

func NewConfigLogger added in v1.1.0

func NewConfigLogger(config Config, commonFields map[string]string, wrappers []Wrapper, opts ...zap.Option) *CherryLogger

NewConfigLogger builds a CherryLogger from the given Config. Pass nil for commonFields and wrappers when neither is needed; the Manager handles them automatically via GetOrCreateLogger.

This function references no package-level global state — it is safe to call during package initialization (e.g. from NewManager).

func NewLogger

func NewLogger(refLoggerName string, opts ...zap.Option) *CherryLogger

NewLogger creates or retrieves a named logger from profile config via the default manager. Loggers are cached by name; subsequent calls with the same name return the existing instance.

type Config added in v1.1.0

type Config struct {
	// LogLevel is the minimum enabled log level ("debug", "info", etc.).
	LogLevel string `json:"level"`
	// StackLevel is the minimum level at which stack traces are captured.
	StackLevel string `json:"stack_level"`
	// EncoderType selects the encoder: "console" (key=value) or "json".
	EncoderType string `json:"encoder_type"`
	// EnableConsole enables synchronous writes to os.Stderr.
	EnableConsole bool `json:"enable_console"`
	// EnableWriteFile enables file output with rotation via rotatelogs.
	EnableWriteFile bool `json:"enable_write_file"`
	// MaxAge is the maximum number of days a rotated log file is kept.
	MaxAge int `json:"max_age"`
	// TimeFormat is the layout passed to time.Format for timestamps.
	TimeFormat string `json:"time_format"`
	// PrintCaller enables the caller file:line annotation on every log line.
	PrintCaller bool `json:"print_caller"`
	// RotationTime is the file rotation interval in seconds.
	RotationTime int `json:"rotation_time"`
	// FileLinkPath is the fixed symlink / shortcut name pointing to the current log file.
	FileLinkPath string `json:"file_link_path"`
	// FilePathFormat is the rotated file path pattern (strftime syntax).
	FilePathFormat string `json:"file_path_format"`
	// IncludeStdout adds os.Stdout as an extra writer.
	IncludeStdout bool `json:"include_stdout"`
	// IncludeStderr adds os.Stderr as an extra writer (skipped when EnableConsole is on).
	IncludeStderr bool `json:"include_stderr"`
}

Config describes the construction parameters for a CherryLogger. It is typically populated from a JSON profile (via NewConfig / NewConfigWithName).

After the logger is built, the Config is stored on CherryLogger as a read-only snapshot — mutating it has no effect on the running logger.

func NewConfig added in v1.1.0

func NewConfig(jsonConfig cfacade.ProfileJSON) (*Config, error)

NewConfig creates a Config from a JSON profile node. Every field falls back to the value in defaultConfig when the profile key is absent.

func NewConfigWithName added in v1.3.13

func NewConfigWithName(refLoggerName string) (*Config, error)

NewConfigWithName looks up a named logger definition under the "logger" key in the global profile and returns its Config.

func (*Config) TimeEncoder added in v1.1.0

func (c *Config) TimeEncoder() zapcore.TimeEncoder

TimeEncoder returns a zapcore.TimeEncoder that formats timestamps using the Config's TimeFormat layout.

type Logger added in v1.5.3

type Logger interface {
	Debug(args ...interface{})
	Debugf(template string, args ...interface{})
	Debugw(msg string, keysAndValues ...interface{})
	Info(args ...interface{})
	Infof(template string, args ...interface{})
	Infow(msg string, keysAndValues ...interface{})
	Warn(args ...interface{})
	Warnf(template string, args ...interface{})
	Warnw(msg string, keysAndValues ...interface{})
	Error(args ...interface{})
	Errorf(template string, args ...interface{})
	Errorw(msg string, keysAndValues ...interface{})
	DPanic(args ...interface{})
	DPanicf(template string, args ...interface{})
	DPanicw(msg string, keysAndValues ...interface{})
	Panic(args ...interface{})
	Panicf(template string, args ...interface{})
	Panicw(msg string, keysAndValues ...interface{})
	Fatal(args ...interface{})
	Fatalf(template string, args ...interface{})
	Fatalw(msg string, keysAndValues ...interface{})

	Sync() error
}

Logger is the common logging interface used throughout the framework. CherryLogger implements this interface by wrapping zap.SugaredLogger.

type Manager added in v1.5.3

type Manager struct {
	// contains filtered or unexported fields
}

Manager holds all logger state that was previously package-level globals. A default Manager (defaultManager) owns the DefaultLogger used by the package-level convenience functions.

For isolated contexts (e.g. tests, multiple Application instances), create a separate Manager with NewManager and pass it around explicitly.

func NewManager added in v1.5.3

func NewManager(opts ...ManagerOption) *Manager

NewManager creates a Manager with an initial console-only default logger.

func (*Manager) CommonFields added in v1.5.3

func (m *Manager) CommonFields() map[string]string

CommonFields returns a copy of the current common fields.

func (*Manager) DefaultLogger added in v1.5.3

func (m *Manager) DefaultLogger() *CherryLogger

DefaultLogger returns the manager's default logger (console fallback).

func (*Manager) GetOrCreateLogger added in v1.5.3

func (m *Manager) GetOrCreateLogger(refLoggerName string, opts ...zap.Option) *CherryLogger

GetOrCreateLogger returns an existing named logger or creates one from profile config.

func (*Manager) Loggers added in v1.5.3

func (m *Manager) Loggers() map[string]*CherryLogger

Loggers returns a snapshot of all named loggers in the registry.

func (*Manager) PrintLevel added in v1.5.3

func (m *Manager) PrintLevel(level zapcore.Level) bool

PrintLevel returns true if the given level meets the minimum print threshold.

func (*Manager) RegisterWrapper added in v1.5.3

func (m *Manager) RegisterWrapper(w Wrapper)

RegisterWrapper adds a core wrapper that is applied to every logger built by this manager.

func (*Manager) SetCommonField added in v1.5.3

func (m *Manager) SetCommonField(key, value string)

SetCommonField sets a single key-value pair applied to every log line.

func (*Manager) SetCommonFields added in v1.5.3

func (m *Manager) SetCommonFields(fields map[string]string)

SetCommonFields sets multiple key-value pairs at once.

func (*Manager) SetFileNameVar added in v1.5.3

func (m *Manager) SetFileNameVar(key, value string)

SetFileNameVar sets a template variable for file path substitution.

func (*Manager) SetPrintLevel added in v1.5.3

func (m *Manager) SetPrintLevel(level zapcore.Level)

SetPrintLevel updates the minimum print level.

func (*Manager) Sync added in v1.5.3

func (m *Manager) Sync()

Sync flushes all loggers managed by this Manager.

type ManagerOption added in v1.5.3

type ManagerOption func(m *Manager)

ManagerOption is a functional option for NewManager.

func WithCommonFields added in v1.5.3

func WithCommonFields(fields map[string]string) ManagerOption

WithCommonFields sets the initial common fields for a new Manager.

type Wrapper added in v1.4.22

type Wrapper interface {
	Wrap(core zapcore.Core) zapcore.Core
}

Wrapper allows intercepting and modifying the zapcore.Core during logger construction. Useful for adding sampling, filtering, or metrics to every logger built by a Manager without modifying the builder itself.

Directories

Path Synopsis
Package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
Package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.

Jump to

Keyboard shortcuts

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