logger

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

README

logger

基本用法

package main

import (
    "github.com/qkja/gobase/logger"
)

func main() {
    logger.Debug("hello %v", "debug")
    logger.Info("hello %v", "info")
    logger.Warn("hello %v", "warn")
    logger.Error("hello %v", "error")
}

配置

logger:
  level: info
  # 日志文件目录,默认工程目录的logs文件夹
  home: ./logs/
  color:
    # 启用:true/false,默认:false
    enable: false
  # 日志滚动策略
  rotate:
    # 日志滚动size;默认300MB
    max-size: 300MB
    # 日志文件最大保留天数;默认60天
    max-history: 60d
    # 多久滚动一次;默认一天
    time: 1d
  path:
    # 日志展示格式:full-全路径;short-短路径;默认short
    type: short
  # 指定分组
  group:
    # 分组1
    group1:
      # 分组1的日志级别
      level: info
    # 分组2
    group2:
      # 分组2的日志级别
      level: debug

更多用法

1. 分组打印

对日志进行精细化管控,这里增加日志分组功能,其中默认分组为"root",我们可以指定我们自己的分组,在代码中不同的业务代码中使用不同的分组,这样在需要的情况下,我们可以对该模块的日志级别进行精细化控制

package main

import (
    "github.com/qkja/gobase/logger"
)

func main() {
    logger.Group("group1").Info("hello", " ", "info")
    logger.Group("group1").Infof("hello %v", "info")
}
2. 线上日志级别动态修改

支持线上动态的日志修改,logger.level为默认分组的日志级别,如下修改为默认分组

2.1 root默认分组修改
curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"logger.level", "value":"debug"}'
func main() {
    // 设置为debug后,如下的debug级别能够记录到日志中
    logger.Debug("hello %v", "debug1")
}
2.2 指定分组修改

如下为指定group名字为xxx的,设置日志级别为debug

curl -X PUT http://localhost:xxx/{api-prefix}/{api-module}/config/update -d '{"key":"logger.group.xxxx.level", "value":"debug"}'
func main() {
    // 如下的日志就会被打印出来,否则默认info级别是不打印的
    logger.Group("group1").Debugf("hello %v", "debug")
}

Documentation

Overview

Package logger 提供基于 logrus 的统一日志封装: 分组、文件滚动、级别热更新、彩色输出、调用位置、ctx trace 前缀。

基本用法:

logger.Info("hello %s", "world")
logger.ErrorCtx(ctx, "call failed: %v", err)
logger.Group("orm").Debugf("[SQL] %s", sql)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigChangeListener

func ConfigChangeListener(event listener.BaseEvent)

ConfigChangeListener 配置变更监听:逐 key 事件 + 整文件重载合成事件。

func Debug

func Debug(format string, v ...any)

func DebugCtx added in v1.0.2

func DebugCtx(ctx context.Context, format string, v ...any)

DebugCtx 带上下文的 Debug 日志。

func DebugDirect

func DebugDirect(v ...any)

func DebugTenant added in v1.0.2

func DebugTenant(ctx context.Context, format string, v ...any)

DebugTenant 租户级 debug 日志。 仅当「全局 debug 开启」或「ctx 中租户在 debug_tenant_ids 列表」时输出, 自动带 [TenantId:xxx] [TraceId:xxx] 前缀。

用法:在业务逻辑中需要细粒度排查的地方调用。

logger.DebugTenant(ctx, "processing order: %v", order)

func Error

func Error(format string, v ...any)

func ErrorCtx added in v1.0.2

func ErrorCtx(ctx context.Context, format string, v ...any)

ErrorCtx 带上下文的 Error 日志。

func ErrorDirect

func ErrorDirect(v ...any)

func Fatal

func Fatal(format string, v ...any)

func Group

func Group(groupNames ...string) *logrus.Logger

Group 返回指定分组 logger。支持传入多个别名:返回第一个已存在的分组; 若都未创建,则创建第一个名字的分组。

func Info

func Info(format string, v ...any)

func InfoCtx added in v1.0.2

func InfoCtx(ctx context.Context, format string, v ...any)

InfoCtx 带上下文的 Info 日志。自动从 ctx 提取 TraceId 作为前缀。

func InfoDirect

func InfoDirect(v ...any)

func InitLog

func InitLog()

InitLog 应用配置到 root logger,并注册配置变更监听(支持级别热更新)。

func Record

func Record(level, format string, v ...any)

Record 按字符串级别动态输出(默认 debug)。

func SetGlobalLevel

func SetGlobalLevel(strLevel string)

SetGlobalLevel 设置 root logger 级别(热更新入口)。

func Warn

func Warn(format string, v ...any)

func WarnCtx added in v1.0.2

func WarnCtx(ctx context.Context, format string, v ...any)

WarnCtx 带上下文的 Warn 日志。

func WarnDirect

func WarnDirect(v ...any)

Types

type StandardFormatter

type StandardFormatter struct{}

StandardFormatter 统一日志格式:

[2006-01-02 15:04:05] [app-name] LEVEL path:line#func message fields

颜色输出受 logger.color.enable 控制(默认关闭)。

func (*StandardFormatter) Format

func (m *StandardFormatter) Format(entry *logrus.Entry) ([]byte, error)

Jump to

Keyboard shortcuts

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