Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

logger_en.md

maoxiaoyue edited this page May 14, 2026 · 1 revision

Logger Package (pkg/logger)

The logger package provides a lightweight and high-performance logging tool tailored for HypGo. Built on Go's standard library log, it extends functionality with colored output, multi-level filtering, and automatic log rotation.

Key Features

  • Multiple Log Levels: Supports DEBUG, INFO, NOTICE, WARN, ERROR, EMERGENCY levels for easy output granularity control.
  • Automatic Log Rotation: Built-in LogRotator that can automatically split log files based on file size or retention days and clean up old logs, preventing disk exhaustion.
  • Readable Colored Terminal Output: When logging to stdout/stderr, color highlighting of levels and alignment is automatically enabled, greatly improving the development experience.
  • Key-Value Logging Support: Provides convenient syntax for printing parameter variables, which are automatically formatted and appended to log messages.
  • Global Instance (Singleton) and Flexible Configuration: Provides InitLogger and GetLogger for a convenient global logger integration.

Basic Usage

Use GetLogger() to get the global Logger for logging anywhere:

package main
import (
 "github.com/maoxiaoyue/hypgo/pkg/logger"
)
func main() {
 log := logger.GetLogger()
 
 // Set output level (all levels at or above DEBUG will be output)
 log.SetLevel(logger.DEBUG)
 // Basic log output
 log.Info("Server started")
 
 // Output with variables
 log.Debug("New connection request received", "client_ip", "192.168.1.100", "port", 8080)
 
 // Error occurred
 log.Error("Unable to write to database: %v", err)
 
 // Fatal error, exits with os.Exit(1) after output
 log.Fatal("Core system component missing!")
}

Log Rotation and File Output

If you want to save logs to a file instead of the terminal, configure the output path and expiry mechanism:

log := logger.GetLogger()
// Set log output to logs/app.log file
log.SetFile("logs/app.log")
// Set automatic rotation strategy
log.SetRotator(&logger.LogRotator{
 filename: "logs/app.log", // Same path as SetFile output
 maxSize: 10485760, // Maximum file size (10 MB)
 maxAge: 7, // Keep logs for this many days
 maxBackups: 5, // Maximum number of historical log files to keep
})

HypGo

繁體中文 | English


中文文件

設計文件

套件

AI 協作工具鏈

CLI 命令


English Docs

Design Docs

Packages

AI Collaboration Toolchain

CLI Commands

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /