log

package module
v0.29.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 16 Imported by: 10

README

log

log 是 infrago 的模块

包定位

  • 类型:模块
  • 作用:日志模块,负责日志采集、异步批量与多后端输出。

主要功能

  • 对上提供统一模块接口
  • 对下通过驱动接口接入具体后端
  • 支持按配置切换驱动实现

快速接入

import _ "github.com/infrago/log"
[log]
driver = "default"

驱动实现接口列表

以下接口由驱动实现(来自模块 driver.go):

Driver
  • Connect(*Instance) (Connection, error)
Connection
  • Open() error
  • Close() error
  • Write(logs ...Log) error

全局配置项(所有配置键)

配置段:[log]

  • driver
  • level
  • levels
  • sample
  • json
  • unsafe_fields
  • flag
  • format
  • buffer
  • batch
  • timeout
  • block
  • overflow
  • drop
  • setting

常用运行期 API

  • Stats():返回队列、丢弃、同步降级、写入错误、延迟和连接级指标
  • SetExitFunc(fn):替换 Fatal/Fatalf/Fatalw 的退出函数,便于测试 fatal 日志;返回值可恢复旧函数

说明

  • setting 一般用于向具体驱动透传专用参数
  • 多实例配置请参考模块源码中的 Config/configure 处理逻辑

Documentation

Index

Constants

View Source
const (
	OverflowDrop       = "drop"
	OverflowDropNewest = "drop_newest"
	OverflowDropOldest = "drop_oldest"
	OverflowBlock      = "block"

	DropOld = "old"
	DropNew = "new"
)

Variables

This section is empty.

Functions

func Debug

func Debug(args ...Any)

func Debugf added in v0.7.0

func Debugf(format string, args ...Any)

func Debugw added in v0.7.0

func Debugw(body string, fields Map)

func Error

func Error(args ...Any)

func Errorf added in v0.7.0

func Errorf(format string, args ...Any)

func Errorw added in v0.7.0

func Errorw(body string, fields Map)

func Fatal

func Fatal(args ...Any)

func Fatalf added in v0.7.0

func Fatalf(format string, args ...Any)

func Fatalw added in v0.7.0

func Fatalw(body string, fields Map)

func Info

func Info(args ...Any)

func Infof added in v0.7.0

func Infof(format string, args ...Any)

func Infow added in v0.7.0

func Infow(body string, fields Map)

func Levels

func Levels() map[Level]string

func Notice

func Notice(args ...Any)

func Noticef added in v0.7.0

func Noticef(format string, args ...Any)

func Noticew added in v0.7.0

func Noticew(body string, fields Map)

func Panic

func Panic(args ...Any)

func Panicf added in v0.7.0

func Panicf(format string, args ...Any)

func Panicw added in v0.7.0

func Panicw(body string, fields Map)

func SetExitFunc added in v0.26.0

func SetExitFunc(fn func(int)) func()

func Stats added in v0.7.0

func Stats() Map

func Trace

func Trace(args ...Any)

func Tracef added in v0.7.0

func Tracef(format string, args ...Any)

func Tracew added in v0.7.0

func Tracew(body string, fields Map)

func Warning

func Warning(args ...Any)

func Warningf added in v0.7.0

func Warningf(format string, args ...Any)

func Warningw added in v0.7.0

func Warningw(body string, fields Map)

func Write added in v0.7.0

func Write(level Level, args ...Any)

func Writef added in v0.7.0

func Writef(level Level, format string, args ...Any)

func Writew added in v0.7.0

func Writew(level Level, body string, fields Map)

Types

type Config

type Config struct {
	Driver       string
	Level        Level
	Levels       map[Level]bool
	Sample       float64
	Json         bool
	UnsafeFields bool
	Buffer       int
	Batch        int
	Timeout      time.Duration
	Block        bool
	Overflow     string
	Drop         string
	Flag         string
	Format       string
	Setting      Map
}

type Configs

type Configs map[string]Config

type Connection added in v0.7.0

type Connection interface {
	Open() error
	Close() error
	Write(logs ...Log) error
}

type Driver

type Driver interface {
	Connect(*Instance) (Connection, error)
}

type Instance

type Instance struct {
	Name    string
	Config  Config
	Setting map[string]any
	// contains filtered or unexported fields
}

func (*Instance) Allow added in v0.7.0

func (inst *Instance) Allow(level Level, body, project, role, profile, node string, fields Map) bool

func (*Instance) Format

func (inst *Instance) Format(entry Log) string

type Level

type Level = int
const (
	LevelFatal Level = iota
	LevelPanic
	LevelError
	LevelWarning
	LevelNotice
	LevelInfo
	LevelTrace
	LevelDebug
)

type Log

type Log struct {
	Time    time.Time
	Level   Level
	Body    string
	Project string
	Role    string
	Profile string
	Node    string
	Fields  Map
}

type Module

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

func (*Module) Close added in v0.7.0

func (m *Module) Close()

func (*Module) Config

func (m *Module) Config(global Map)

func (*Module) Logging

func (m *Module) Logging(level Level, args ...Any)

func (*Module) Loggingf added in v0.7.0

func (m *Module) Loggingf(level Level, format string, args ...Any)

func (*Module) Loggingw added in v0.7.0

func (m *Module) Loggingw(level Level, body string, fields Map)

func (*Module) Open added in v0.7.0

func (m *Module) Open()

func (*Module) Register

func (m *Module) Register(name string, value Any)

func (*Module) RegisterConfig added in v0.7.0

func (m *Module) RegisterConfig(name string, cfg Config)

func (*Module) RegisterConfigs added in v0.7.0

func (m *Module) RegisterConfigs(configs Configs)

func (*Module) RegisterDriver added in v0.7.0

func (m *Module) RegisterDriver(name string, driver Driver)

func (*Module) Setup added in v0.7.0

func (m *Module) Setup()

func (*Module) Start added in v0.7.0

func (m *Module) Start()

func (*Module) Stats added in v0.7.0

func (m *Module) Stats() Map

func (*Module) Stop added in v0.7.0

func (m *Module) Stop()

func (*Module) Write

func (m *Module) Write(entry Log)

func (*Module) WriteSync added in v0.26.0

func (m *Module) WriteSync(entry Log)

Jump to

Keyboard shortcuts

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