Go to file
2024-09-02 01:01:23 +08:00
example chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
.gitignore chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
console_handler.go chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
file_handler.go chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
global.go chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
go.mod chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
go.sum chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
LICENSE chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
logger.go chore: Add initial project files and dependencies 2024-09-01 00:03:12 +08:00
README.md 更新 README.md 2024-09-02 01:01:23 +08:00

logx

logx 是一个灵活、高效的 Go 日志库,基于 log/slog 包构建。它提供了结构化日志记录、多处理程序支持、上下文感知和全局日志记录器等功能。

安装

go get -u github.com/chenchi/logx

快速开始

package main

import (
    "context"
    "log/slog"

    "gitea.cccvno1.com/chenchi/logx"
)

func main() {
    logger := logx.New(
        logx.WithConsoleHandler(slog.LevelDebug),
        logx.WithFileHandler("app.log", 10, 5, 30, true, slog.LevelInfo),
    )
    
    logx.SetGlobalLogger(logger)

    logx.Info("Application started")

    ctx := context.WithValue(context.Background(), "trace_id", "abc123")
    contextLogger := logx.WithContext(ctx)
    contextLogger.Debug("This is a debug message with context")

    // 使用结构化日志
    logx.Info("User logged in",
        slog.String("username", "john_doe"),
        slog.Int("user_id", 12345),
    )

    logger.Close()
}