Introduction
Log module, implements structured log output based on zap + lumberjack. Supports JSON and Console formats, automatic log file rotation (by size and days), and simultaneous output to file and console. Provides two global log instances: Logger (business log) and CoreLogger (framework core log). The default log directory is the current working directory, which can be dynamically changed via SetLogDir().
Import
import "github.com/wallace5303/ee-go/elog"Global Variables
Logger
*zap.SugaredLogger — Business log instance, initialized by InitLog(). Used to record logs in business code.
elog.Logger.Info("hello")
elog.Logger.Infof("user id: %d", userId)
elog.Logger.Warn("warning message")
elog.Logger.Errorf("error: %s", err)CoreLogger
*zap.SugaredLogger — Framework core log instance, initialized by InitCoreLog(). Used to record ee-go framework internal logs.
elog.CoreLogger.Infof("[ee-go] load http service")
elog.CoreLogger.Errorf("[ee-go] The port:%s already in use", port)LogDir
string — Log file storage directory, defaults to the current working directory (os.Getwd()), modified by SetLogDir().
LogName
string — Default log file name, value is "ee-go.log".
Structs
LogConfig
Log configuration struct, parameters used when generating zap Logger.
| Field | Type | Description |
|---|---|---|
| OutputJSON | bool | Output format: true for JSON format, false for Console format (default) |
| Level | string | Minimum log level, e.g. "debug", "info", "warn", "error" (default "info") |
| FileName | string | Log file absolute path, default is {LogDir}/ee-go.log |
| MaxSize | int | Maximum size of a single log file (MB), auto-rotates after exceeding, default 1024 |
| MaxAge | int | Maximum days to retain old log files, default 30 |
API
InitLog(cfg)
Description: Initialize the business log instance Logger. Creates zap Logger based on configuration and converts it to SugaredLogger, writing to the global Logger variable.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| cfg | map[string]any | Yes | Log configuration, supported fields: output_json (bool), level (string), filename (string), max_size (float64→int), max_age (float64→int) |
Returns: None
Example:
logCfg := econfig.GetLogger()
elog.InitLog(logCfg)
// Use business log
elog.Logger.Info("application started")
elog.Logger.Infof("request: method=%s, path=%s", method, path)See also: econfig.GetLogger()
InitCoreLog(cfg)
Description: Initialize the framework core log instance CoreLogger. Same logic as InitLog(), creates an independent SugaredLogger and writes to the global CoreLogger variable, used to record ee-go framework internal logs.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| cfg | map[string]any | Yes | Core log configuration, same fields as InitLog() |
Returns: None
Example:
coreLogCfg := econfig.GetCoreLogger()
elog.InitCoreLog(coreLogCfg)
// Use core log
elog.CoreLogger.Infof("[ee-go] http server %s, pid:%d", url, pid)See also: econfig.GetCoreLogger()
GetZLog(cfg)
Description: Create and return a *zap.Logger instance based on configuration. This is the core function for log initialization, both InitLog() and InitCoreLog() call this function internally. Merges default configuration with the passed configuration, generates zap Core (encoder + writer + level filter), and returns a zap Logger with caller information.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| cfg | map[string]any | Yes | Log configuration, same fields as InitLog(); uses default values when passing an empty map |
Returns: *zap.Logger — zap Logger instance, can further call .Sugar() to get SugaredLogger
Example:
cfg := map[string]any{
"output_json": false,
"level": "info",
"filename": "app.log",
"max_size": 512,
"max_age": 7,
}
zlog := elog.GetZLog(cfg)
logger := zlog.Sugar()
logger.Info("custom logger message")SetLogDir(path)
Description: Set the log file storage directory. Modifies the global LogDir variable, affecting the absolute path construction of log files in subsequent GetZLog() calls. Typically called during initialization by eboot, pointing the log directory to {WorkDir}/logs.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| path | string | Yes | Log directory absolute path |
Returns: None
Example:
logDir := filepath.Join(eruntime.WorkDir, "logs")
elog.SetLogDir(logDir)See also: eruntime.WorkDir
SugaredLogger Methods
Both Logger and CoreLogger are *zap.SugaredLogger, supporting the following commonly used methods:
| Method | Description |
|---|---|
Info(args ...any) | Log at INFO level |
Infof(template string, args ...any) | Log formatted message at INFO level |
Warn(args ...any) | Log at WARN level |
Warnf(template string, args ...any) | Log formatted message at WARN level |
Error(args ...any) | Log at ERROR level |
Errorf(template string, args ...any) | Log formatted message at ERROR level |
DPanic(args ...any) | Log at DPANIC level (panics in development) |
DPanicf(template string, args ...any) | Log formatted message at DPANIC level |
Panic(args ...any) | Log at PANIC level and panic |
Panicf(template string, args ...any) | Log formatted message at PANIC level and panic |
Fatal(args ...any) | Log at FATAL level and terminate process |
Fatalf(template string, args ...any) | Log formatted message at FATAL level and terminate process |
