slogs

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 13 Imported by: 0

README

slogs

中文文档

A lightweight, zero-external-dependency unified logging package built on Go's standard log/slog (Go 1.21+), providing structured logging, file rotation, multi-target output, and named logger management.

Features

  • Zero external dependencies — Built on log/slog (Go 1.21+ stdlib); file rotation is vendored internally (adapted from lumberjack, MIT License)
  • Dual-target output — Console (stderr) + file (JSON), dispatched via a built-in fan-out multiHandler
  • File rotation — Built-in Rotator with size/count/age-based rolling + gzip compression
  • Structured attributesWith() attaches key-value context across all output targets
  • Named loggersManager manages multiple isolated named logger instances
  • Global default — Lazy initialization + package-level convenience functions, ready out of the box
  • Thread-safe — All exported types and functions are safe for concurrent use

Installation

go get github.com/winezer0/slogs

Quick Start

Package-level Functions (Zero Config)
import "github.com/winezer0/slogs"

slogs.Info("server started", "port", 8080)
slogs.Errorf("connection failed: %v", err)
Explicit Initialization
cfg := slogs.NewConfig("debug", "logs/app.log", "json")
if err := slogs.Init(cfg); err != nil {
    log.Fatal(err)
}
slogs.Debug("initialized with file output")
Standalone Logger Instance
cfg := slogs.Config{
    Level:      "info",
    Format:     "text",
    FilePath:   "logs/audit.log",
    MaxSize:    50,
    MaxBackups: 5,
    MaxAge:     14,
    Compress:   true,
}
logger, err := slogs.NewLogger(cfg)
if err != nil {
    log.Fatal(err)
}
defer logger.Close()

// Structured attributes
reqLogger := logger.With("request_id", "abc-123")
reqLogger.Info("processing request", "method", "GET", "path", "/api/scan")
Named Loggers (Multi-module Isolation)
// Create
scanLogger, _ := slogs.CreateLogger("scanengine", slogs.NewConfig("info", "logs/scan.log", "json"))
auditLogger, _ := slogs.CreateLogger("auditflow", slogs.NewConfig("debug", "logs/audit.log", "json"))

// Retrieve
logger, ok := slogs.GetLogger("scanengine")

// Close all on shutdown
slogs.CloseAll()
Access Underlying slog.Logger
logger := slogs.Default()
slogLogger := logger.Slog() // *slog.Logger, can be passed to frameworks like eino

Configuration

logging:
  level: info          # debug | info | warn | error
  format: text         # text (human-readable) | json (structured)
  file_path: ""        # log file path; empty = console only
  max_size: 100        # max megabytes per file before rotation
  max_backups: 3       # max number of old files to retain
  max_age: 30          # max days to retain old files
  compress: true       # gzip compress rotated files
Field Type Default Description
Level string "info" Minimum log level: debug, info, warn, error
Format string "text" Console format: text or json
FilePath string "" Log file path; empty disables file output
MaxSize int 100 Max MB per file before rotation
MaxBackups int 3 Max old files to retain
MaxAge int 30 Max days to retain old files
Compress bool true Gzip compress rotated files

Log Levels

Level Value Purpose
DEBUG -4 Development debug info
INFO 0 Normal operational status
WARN 4 Recoverable anomalies / degradation
ERROR 8 Errors requiring attention

Output Format

Console text mode (stderr):

time=2026-07-27T18:00:00.000+08:00 level=INFO source=main.go:42 msg="server started" port=8080

Console json mode (stderr):

{"time":"2026-07-27T18:00:00.000+08:00","level":"INFO","source":{"function":"main.main","file":"main.go","line":42},"msg":"server started","port":8080}

File output (always JSON):

{"time":"2026-07-27T18:00:00.000+08:00","level":"INFO","source":{"function":"main.main","file":"main.go","line":42},"msg":"server started","port":8080}

File Structure

File Responsibility
doc.go Package-level documentation
config.go Config struct, defaults, factory functions
logger.go Logger wrapper, multiHandler, level parsing, file handler
default.go Global default logger + package-level functions
manager.go Named logger registry (create/get/close-all)
rotator.go Built-in log file rotator (adapted from lumberjack)
chown.go No-op chown for non-Linux platforms
chown_linux.go Preserve file ownership on Linux

Dependencies

  • log/slog — Go standard library
  • No third-party dependencies (file rotation is vendored in rotator.go, adapted from lumberjack MIT License)

Testing

go test ./... -v -cover

API Documentation

Full API documentation is available at pkg.go.dev/github.com/winezer0/slogs.

License

MIT

internal/logging

基于 Go 标准库 log/slog 的统一日志包,提供结构化日志、文件轮转、多目标输出和命名日志器管理能力。

A unified logging package built on Go's standard log/slog, providing structured logging, file rotation, multi-target output, and named logger management.

特性 / Features

  • 零外部依赖 — 基于 log/slog(Go 1.21+ 标准库),文件轮转内置(源自 lumberjack MIT 协议,已 vendor 化)
  • 双目标输出 — 控制台(stderr)+ 文件(JSON),通过 multiHandler fanout 分发
  • 文件轮转 — 内置 Rotator,支持按大小/数量/天数自动轮转 + gzip 压缩
  • 结构化属性With() 附加 key-value 上下文,贯穿所有输出目标
  • 命名日志器Manager 管理多个隔离的命名日志器实例
  • 全局默认 — 懒初始化 + 包级便捷函数,开箱即用

  • Zero external dependencies — Built on log/slog (Go 1.21+ stdlib); file rotation is vendored internally (adapted from lumberjack, MIT License)
  • Dual-target output — Console (stderr) + file (JSON), dispatched via multiHandler fanout
  • File rotation — Built-in Rotator with size/count/age-based rolling + gzip compression
  • Structured attributesWith() attaches key-value context across all output targets
  • Named loggersManager manages multiple isolated named logger instances
  • Global default — Lazy initialization + package-level convenience functions, ready out of the box

文件结构 / File Structure

文件 / File 职责 / Responsibility
config.go 配置结构体 + 默认值 + 工厂函数 / Config struct, defaults, factory functions
logger.go Logger 封装、multiHandler、级别解析、文件轮转接入 / Logger wrapper, multiHandler, level parsing, file handler
default.go 全局默认日志器 + 包级便捷函数 / Global default logger + package-level functions
manager.go 命名日志器注册/获取/统一关闭 / Named logger registry (create/get/close-all)
rotator.go 内置日志文件轮转器(源自 lumberjack) / Built-in log file rotator (adapted from lumberjack)
chown.go 非 Linux 平台 chown 空实现 / No-op chown for non-Linux platforms
chown_linux.go Linux 文件所有权保持 / Preserve file ownership on Linux
logging_test.go 主单元测试 / Main unit tests
rotator_test.go 轮转器单元测试 / Rotator unit tests

快速使用 / Quick Start

包级函数(零配置) / Package-level Functions (Zero Config)
import "github.com/winezer0/mgsast/internal/logging"

logging.Info("server started", "port", 8080)
logging.Errorf("connection failed: %v", err)
显式初始化 / Explicit Initialization
cfg := logging.NewConfig("debug", "logs/app.log", "json")
if err := logging.Init(cfg); err != nil {
    log.Fatal(err)
}
logging.Debug("initialized with file output")
独立 Logger 实例 / Standalone Logger Instance
cfg := logging.Config{
    Level:      "info",
    Format:     "text",
    FilePath:   "logs/audit.log",
    MaxSize:    50,
    MaxBackups: 5,
    MaxAge:     14,
    Compress:   true,
}
logger, err := logging.NewLogger(cfg)
if err != nil {
    log.Fatal(err)
}
defer logger.Close()

// Structured attributes
reqLogger := logger.With("request_id", "abc-123")
reqLogger.Info("processing request", "method", "GET", "path", "/api/scan")
命名日志器(多模块隔离) / Named Loggers (Multi-module Isolation)
// Create
scanLogger, _ := logging.CreateLogger("scanengine", logging.NewConfig("info", "logs/scan.log", "json"))
auditLogger, _ := logging.CreateLogger("auditflow", logging.NewConfig("debug", "logs/audit.log", "json"))

// Retrieve
logger, ok := logging.GetLogger("scanengine")

// Close all on shutdown
logging.CloseAll()
获取底层 slog.Logger / Access Underlying slog.Logger
logger := logging.Default()
slogLogger := logger.Slog() // *slog.Logger, can be passed directly to frameworks like eino

配置说明 / Configuration

logging:
  level: info          # debug | info | warn | error
  format: text         # text (human-readable) | json (structured)
  file_path: ""        # log file path; empty = console only
  max_size: 100        # max megabytes per file before rotation
  max_backups: 3       # max number of old files to retain
  max_age: 30          # max days to retain old files
  compress: true       # gzip compress rotated files

日志级别 / Log Levels

级别 / Level 值 / Value 用途 / Purpose
DEBUG -4 开发调试信息 / Development debug info
INFO 0 正常运行状态 / Normal operational status
WARN 4 可恢复的异常/降级 / Recoverable anomalies / degradation
ERROR 8 需要关注的错误 / Errors requiring attention

输出格式 / Output Format

控制台 text 模式(stderr) / Console text mode (stderr):

time=2026-07-27T18:00:00.000+08:00 level=INFO source=main.go:42 msg="server started" port=8080

控制台 json 模式(stderr) / Console json mode (stderr):

{"time":"2026-07-27T18:00:00.000+08:00","level":"INFO","source":{"function":"main.main","file":"main.go","line":42},"msg":"server started","port":8080}

文件输出(始终 JSON) / File output (always JSON):

{"time":"2026-07-27T18:00:00.000+08:00","level":"INFO","source":{"function":"main.main","file":"main.go","line":42},"msg":"server started","port":8080}

依赖 / Dependencies

  • log/slog — Go 标准库 / Go standard library
  • 无第三方依赖 / No third-party dependencies(文件轮转已内置于 rotator.go,源自 lumberjack MIT 协议 / file rotation is vendored in rotator.go, adapted from lumberjack MIT License)

测试 / Testing

go test ./internal/logging/... -v -cover

覆盖率 ≥ 84% / Coverage ≥ 84%.

internal/logging

基于 Go 标准库 log/slog 的统一日志包,提供结构化日志、文件轮转、多目标输出和命名日志器管理能力。

特性

  • 零框架依赖 — 基于 log/slog(Go 1.21+ 标准库),无第三方日志框架
  • 双目标输出 — 控制台(stderr)+ 文件(JSON),通过 multiHandler fanout 分发
  • 文件轮转 — 集成 lumberjack.v2,支持按大小/数量/天数自动轮转 + 压缩
  • 结构化属性With() 附加 key-value 上下文,贯穿所有输出目标
  • 命名日志器Manager 管理多个隔离的命名日志器实例
  • 全局默认 — 懒初始化 + 包级便捷函数,开箱即用

文件结构

文件 职责
config.go 配置结构体 + 默认值 + 工厂函数
logger.go Logger 封装、multiHandler、级别解析、文件轮转
default.go 全局默认日志器 + 包级便捷函数
manager.go 命名日志器注册/获取/统一关闭

快速使用

包级函数(零配置)
import "github.com/winezer0/mgsast/internal/logging"

logging.Info("server started", "port", 8080)
logging.Errorf("connection failed: %v", err)
显式初始化
cfg := logging.NewConfig("debug", "logs/app.log", "json")
if err := logging.Init(cfg); err != nil {
    log.Fatal(err)
}
logging.Debug("initialized with file output")
独立 Logger 实例
cfg := logging.Config{
    Level:      "info",
    Format:     "text",
    FilePath:   "logs/audit.log",
    MaxSize:    50,
    MaxBackups: 5,
    MaxAge:     14,
    Compress:   true,
}
logger, err := logging.NewLogger(cfg)
if err != nil {
    log.Fatal(err)
}
defer logger.Close()

// 结构化属性
reqLogger := logger.With("request_id", "abc-123")
reqLogger.Info("processing request", "method", "GET", "path", "/api/scan")
命名日志器(多模块隔离)
// 创建
scanLogger, _ := logging.CreateLogger("scanengine", logging.NewConfig("info", "logs/scan.log", "json"))
auditLogger, _ := logging.CreateLogger("auditflow", logging.NewConfig("debug", "logs/audit.log", "json"))

// 获取
logger, ok := logging.GetLogger("scanengine")

// 程序退出时统一关闭
logging.CloseAll()
获取底层 slog.Logger
logger := logging.Default()
slogLogger := logger.Slog() // *slog.Logger,可直接传递给 eino 等框架

配置说明

logging:
  level: info          # debug | info | warn | error
  format: text         # text(人类可读)| json(结构化)
  file_path: ""        # 日志文件路径,空 = 仅控制台
  max_size: 100        # 单文件最大 MB
  max_backups: 3       # 保留旧文件数
  max_age: 30          # 保留天数
  compress: true       # 轮转文件是否 gzip 压缩

日志级别

级别 用途
DEBUG -4 开发调试信息
INFO 0 正常运行状态
WARN 4 可恢复的异常/降级
ERROR 8 需要关注的错误

输出格式

控制台 text 模式(stderr):

time=2026-07-27T18:00:00.000+08:00 level=INFO source=main.go:42 msg="server started" port=8080

控制台 json 模式(stderr):

{"time":"2026-07-27T18:00:00.000+08:00","level":"INFO","source":{"function":"main.main","file":"main.go","line":42},"msg":"server started","port":8080}

文件输出(始终 JSON):

{"time":"2026-07-27T18:00:00.000+08:00","level":"INFO","source":{"function":"main.main","file":"main.go","line":42},"msg":"server started","port":8080}

依赖

  • log/slog — Go 标准库
  • gopkg.in/natefinch/lumberjack.v2 — 文件轮转

测试

go test ./internal/logging/... -v -cover

覆盖率 ≥ 84%。

Documentation

Overview

Package slogs provides a lightweight, zero-external-dependency unified logging solution built on Go's standard log/slog package (Go 1.21+).

Features

  • Dual-target output: console (stderr) and file (JSON), dispatched via a built-in fan-out multiHandler.
  • File rotation: built-in Rotator (adapted from lumberjack, MIT License) supports size-based rolling, backup retention, age-based cleanup, and gzip compression.
  • Structured attributes: Logger.With attaches key-value context that propagates to all output targets.
  • Named loggers: package-level CreateLogger/GetLogger manage multiple isolated logger instances via a singleton Manager.
  • Global default: lazy-initialized default logger with package-level convenience functions (Info, Debug, Error, etc.).

Quick Start

Zero-configuration usage with the global default logger:

slogs.Info("server started", "port", 8080)
slogs.Errorf("connection failed: %v", err)

Explicit initialization with file output:

cfg := slogs.NewConfig("debug", "logs/app.log", "json")
if err := slogs.Init(cfg); err != nil {
    log.Fatal(err)
}

Standalone logger instance:

logger, err := slogs.NewLogger(slogs.Config{
    Level:    "info",
    FilePath: "logs/audit.log",
    MaxSize:  50,
})
if err != nil {
    log.Fatal(err)
}
defer logger.Close()
logger.Info("audit event", "user", "admin")

Named loggers for multi-module isolation:

scanLog, _ := slogs.CreateLogger("scan", slogs.NewConfig("info", "logs/scan.log", "json"))
auditLog, _ := slogs.CreateLogger("audit", slogs.NewConfig("debug", "logs/audit.log", "json"))
defer slogs.CloseAll()

Configuration

The Config struct controls logging behavior:

  • Level: minimum log level ("debug", "info", "warn", "error").
  • Format: console format ("text" or "json").
  • FilePath: log file path; empty disables file output.
  • MaxSize: max megabytes per file before rotation (default 100).
  • MaxBackups: max old files to retain (default 3).
  • MaxAge: max days to retain old files (default 30).
  • Compress: gzip-compress rotated files (default true).

Thread Safety

All exported types and functions are safe for concurrent use. The Manager uses a read-write mutex; Logger and Rotator use internal synchronization.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAll

func CloseAll() error

CloseAll closes all managed loggers and resets the registry.

func Debug

func Debug(msg string, args ...any)

Debug logs at debug level using the default logger.

func Debugf

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

Debugf logs a formatted message at debug level using the default logger.

func Error

func Error(msg string, args ...any)

Error logs at error level using the default logger.

func Errorf

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

Errorf logs a formatted message at error level using the default logger.

func Info

func Info(msg string, args ...any)

Info logs at info level using the default logger.

func Infof

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

Infof logs a formatted message at info level using the default logger.

func Init

func Init(config Config) error

Init initializes the global default logger with the given configuration. Subsequent calls are no-ops (first call wins).

Example

ExampleInit demonstrates initializing the global default logger with file output.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/winezer0/slogs"
)

func main() {
	dir, _ := os.MkdirTemp("", "slogs-example")
	defer os.RemoveAll(dir)

	cfg := slogs.NewConfig("debug", filepath.Join(dir, "app.log"), "json")
	if err := slogs.Init(cfg); err != nil {
		fmt.Println("init error:", err)
		return
	}
	slogs.Debug("initialized with file output")
	// Output is written to stderr and the log file.
}

func SetDefault

func SetDefault(logger *Logger)

SetDefault replaces the global default logger (useful for testing or late configuration).

Example

ExampleSetDefault demonstrates replacing the global default logger.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/winezer0/slogs"
)

func main() {
	dir, _ := os.MkdirTemp("", "slogs-example")
	defer os.RemoveAll(dir)

	cfg := slogs.NewConfig("warn", filepath.Join(dir, "warn.log"), "json")
	logger, err := slogs.NewLogger(cfg)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer logger.Close()

	slogs.SetDefault(logger)
	// Now package-level functions use the new logger.
	slogs.Warn("disk usage high", "percent", 92)
}

func Warn

func Warn(msg string, args ...any)

Warn logs at warn level using the default logger.

func Warnf

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

Warnf logs a formatted message at warn level using the default logger.

Types

type Config

type Config struct {
	// Level is the minimum log level: debug, info, warn, error.
	Level string `yaml:"level"`
	// Format is the console output format: "text" (human-readable) or "json" (structured).
	Format string `yaml:"format"`
	// FilePath is the log file path; empty means no file output.
	FilePath string `yaml:"file_path"`
	// MaxSize is the maximum size in megabytes of a single log file before rotation.
	MaxSize int `yaml:"max_size"`
	// MaxBackups is the maximum number of old log files to retain.
	MaxBackups int `yaml:"max_backups"`
	// MaxAge is the maximum number of days to retain old log files.
	MaxAge int `yaml:"max_age"`
	// Compress determines whether rotated files are compressed.
	Compress bool `yaml:"compress"`
}

Config holds logging configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a sensible default configuration (info level, text console, no file).

Example

ExampleDefaultConfig demonstrates obtaining the default configuration.

package main

import (
	"fmt"

	"github.com/winezer0/slogs"
)

func main() {
	cfg := slogs.DefaultConfig()
	fmt.Println(cfg.Level)
	fmt.Println(cfg.Format)
	fmt.Println(cfg.MaxSize)
}
Output:
info
text
100

func NewConfig

func NewConfig(level, filePath, format string) Config

NewConfig creates a Config with the given level, file path, and format, applying defaults for rotation parameters.

Example

ExampleNewConfig demonstrates creating a configuration with custom parameters.

package main

import (
	"fmt"

	"github.com/winezer0/slogs"
)

func main() {
	cfg := slogs.NewConfig("error", "/var/log/myapp.log", "json")
	fmt.Println(cfg.Level)
	fmt.Println(cfg.FilePath)
	fmt.Println(cfg.Compress)
}
Output:
error
/var/log/myapp.log
true

type Logger

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

Logger wraps slog.Logger with configuration and lifecycle management.

func CreateLogger

func CreateLogger(name string, config Config) (*Logger, error)

CreateLogger creates and registers a named logger.

Example

ExampleCreateLogger demonstrates creating and retrieving named loggers.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/winezer0/slogs"
)

func main() {
	dir, _ := os.MkdirTemp("", "slogs-example")
	defer os.RemoveAll(dir)
	defer slogs.CloseAll()

	_, err := slogs.CreateLogger("scan", slogs.NewConfig("info", filepath.Join(dir, "scan.log"), "json"))
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	logger, ok := slogs.GetLogger("scan")
	fmt.Println("retrieved:", ok)
	logger.Info("scan completed", "targets", 42)
}
Output:
retrieved: true

func Default

func Default() *Logger

Default returns the global default logger, initializing it if necessary.

Example

ExampleDefault demonstrates using the global default logger with zero configuration.

package main

import (
	"github.com/winezer0/slogs"
)

func main() {
	logger := slogs.Default()
	logger.Info("using default logger", "version", "1.0.0")
}

func GetLogger

func GetLogger(name string) (*Logger, bool)

GetLogger retrieves a previously created named logger.

func NewLogger

func NewLogger(config Config) (*Logger, error)

NewLogger creates a Logger from the given configuration. Console output goes to stderr; file output (if configured) uses JSON with rotation.

Example

ExampleNewLogger demonstrates creating a standalone logger instance.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/winezer0/slogs"
)

func main() {
	dir, _ := os.MkdirTemp("", "slogs-example")
	defer os.RemoveAll(dir)

	cfg := slogs.Config{
		Level:      "info",
		Format:     "text",
		FilePath:   filepath.Join(dir, "audit.log"),
		MaxSize:    50,
		MaxBackups: 5,
		MaxAge:     14,
		Compress:   true,
	}
	logger, err := slogs.NewLogger(cfg)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer logger.Close()

	logger.Info("audit event", "user", "admin", "action", "login")
}

func (*Logger) Close

func (l *Logger) Close() error

Close flushes and releases file resources.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug logs at debug level.

func (*Logger) Debugf

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

Debugf logs a formatted message at debug level.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Error logs at error level.

func (*Logger) Errorf

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

Errorf logs a formatted message at error level.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Info logs at info level.

func (*Logger) Infof

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

Infof logs a formatted message at info level.

func (*Logger) Slog

func (l *Logger) Slog() *slog.Logger

Slog returns the underlying *slog.Logger for direct use.

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...any)

Warn logs at warn level.

func (*Logger) Warnf

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

Warnf logs a formatted message at warn level.

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

With returns a Logger with the given attributes attached.

Example

ExampleLogger_With demonstrates attaching structured attributes to a logger.

package main

import (
	"github.com/winezer0/slogs"
)

func main() {
	logger := slogs.Default()
	reqLogger := logger.With("request_id", "abc-123", "service", "gateway")
	reqLogger.Info("processing request", "method", "GET", "path", "/api/users")
}

type Manager

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

Manager manages named logger instances.

func (*Manager) CloseAll

func (m *Manager) CloseAll() error

CloseAll closes all managed loggers and clears the registry.

func (*Manager) Create

func (m *Manager) Create(name string, config Config) (*Logger, error)

Create creates and registers a named logger.

func (*Manager) Get

func (m *Manager) Get(name string) (*Logger, bool)

Get retrieves a named logger.

type Rotator

type Rotator struct {
	// Filename is the file to write logs to. Backup log files will be retained
	// in the same directory.
	Filename string

	// MaxSize is the maximum size in megabytes of the log file before rotation.
	// Defaults to 100 megabytes.
	MaxSize int

	// MaxAge is the maximum number of days to retain old log files.
	MaxAge int

	// MaxBackups is the maximum number of old log files to retain.
	MaxBackups int

	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time. Default is UTC.
	LocalTime bool

	// Compress determines if rotated log files should be compressed using gzip.
	Compress bool
	// contains filtered or unexported fields
}

Rotator is an io.WriteCloser that writes to a rotating log file.

Rotator opens or creates the logfile on first Write. If the file exists and is less than MaxSize megabytes, it will open and append to that file. If the file exists and its size is >= MaxSize megabytes, the file is renamed by putting the current time in a timestamp in the name immediately before the file's extension. A new log file is then created using original filename.

Backups use the form `name-timestamp.ext` where timestamp is formatted with `2006-01-02T15-04-05.000`.

func (*Rotator) Close

func (r *Rotator) Close() error

Close implements io.Closer, and closes the current logfile.

func (*Rotator) Rotate

func (r *Rotator) Rotate() error

Rotate causes Rotator to close the existing log file and immediately create a new one. After rotating, this initiates compression and removal of old log files according to the configuration.

func (*Rotator) Write

func (r *Rotator) Write(p []byte) (n int, err error)

Write implements io.Writer. If a write would cause the log file to exceed MaxSize, the file is closed, renamed with a timestamp, and a new file created.

Jump to

Keyboard shortcuts

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