log

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2022 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Index

Constants

View Source
const (
	// DefaultEnabled 默认启用日志
	DefaultEnabled = 1

	// DefaultTag 默认日志标签
	DefaultTag LogTag = "ALOG"

	// DefaultSystemTag 默认系统日志消息标签
	DefaultSystemTag LogTag = "SYSTEM"

	// DefaultFilePath 默认日志文件存储路径
	DefaultFilePath = "logs"

	// DefaultFileSize 默认单个日志文件大小为2048KB
	DefaultFileSize = 2048

	// DefaultFileGCInterval 默认文件清理周期为720分钟
	DefaultFileGCInterval = 720

	// DefaultPrint 默认输出日志到控制台
	DefaultPrint = 1

	// DefaultLogLevel 默认日志级别
	DefaultLogLevel = DEBUG

	// DefaultInterval 默认写入时间间隔
	DefaultInterval = 1

	// DefaultShowFile 默认输出文件信息
	DefaultShowFile = 1

	// DefaultFileCaller 默认输出文件信息调用层级
	DefaultFileCaller = 6

	// DefaultElasticURL ElasticSearch默认节点
	DefaultElasticURL = "http://127.0.0.1:9200"

	// DefaultMongoURL 指定MongoDB的默认链接地址
	DefaultMongoURL = "mongodb://127.0.0.1:27017"
)
View Source
const (
	// DefaultTimeTmpl 默认时间输出模板
	DefaultTimeTmpl = `{{.Year}}-{{.Month}}-{{.Day}} {{.Hour}}:{{.Minute}}:{{.Second}}.{{.MilliSecond}}`
	// DefaultConsoleTimeTmpl 默认控制台时间输出模板
	DefaultConsoleTimeTmpl = `{{.Hour}}:{{.Minute}}:{{.Second}}`

	// DefaultConsoleTmpl 默认控制台输出模板
	DefaultConsoleTmpl = `[{{.Time}}|{{.Level}}|{{.Tag}}]{{.ShortName}}:{{.FileLine}}:{{.Message}}`
	// DefaultSystemTmpl 默认系统控制台输出模板
	DefaultSystemTmpl = `[{{.Time}} {{.Level}} {{.Tag}}] {{.Message}}`
	// DefaultMsgTmpl 默认文件存储日志模板
	DefaultMsgTmpl = `{{.Time}} {{.Level}} {{.Tag}} "{{.ShortName}} {{.FileFuncName}} {{.FileLine}}" {{.Message}}`

	// DefaultFileNameTmpl 默认存储日志文件名模板
	DefaultFileNameTmpl = `{{.Year}}{{.Month}}{{.Day}}.log`

	// DefaultElasticIndexTmpl ElasticSearch文档索引名称模板
	DefaultElasticIndexTmpl = `{{.Year}}.{{.Month}}.{{.Day}}`
	// DefaultElasticTypeTmpl ElasticSearch文档类型名称模板
	DefaultElasticTypeTmpl = `ALogs`

	// DefaultMongoDBTmpl MongoDB数据库名称模板
	DefaultMongoDBTmpl = `alog`
	// DefaultMongoCollectionTmpl MongoDB集合名称模板
	DefaultMongoCollectionTmpl = `{{.Year}}{{.Month}}{{.Day}}`
)

Variables

This section is empty.

Functions

func GetDateData

func GetDateData(v time.Time) map[string]interface{}

GetDateData 获取日期数据

func GetTimeData

func GetTimeData(data time.Time) map[string]interface{}

GetTimeData 获取时间数据

func ParseLogItem

func ParseLogItem(tmpl interface{}, timeTmpl interface{}, data *LogItem) string

ParseLogItem 解析日志项模板

func ParseLogItemToBuffer

func ParseLogItemToBuffer(tmpl interface{}, timeTmpl interface{}, data *LogItem) *bytes.Buffer

ParseLogItemWithBuffer 将日志项模板解析到缓冲区

func ParseName

func ParseName(tmpl interface{}, data *LogItem) string

ParseName 解析Name模板

func ParseTime

func ParseTime(tmpl interface{}, data time.Time) string

ParseTime 解析时间模板

Types

type BufferConfig

type BufferConfig struct {
	// Engine 存储引擎({1:memory,2:redis})
	// 默认为内存存储
	Engine LogBufferEngine `json:"engine" yaml:"engine"`
	// TargetStore 指向存储的名称
	TargetStore string `json:"target" yaml:"target"`
}

BufferConfig 缓冲区配置

type ConsoleConfig

type ConsoleConfig struct {
	// Level 输出级别
	Level LogLevel `json:"level" yaml:"level"`
	// Item 日志项配置
	Item LogItemConfig `json:"item" yaml:"item"`
}

ConsoleConfig 控制台输出配置

type CustomConfig

type CustomConfig struct {
	// IsPrint 是否控制台打印
	IsPrint int `json:"print" yaml:"print"`
	// TargetStore 目标存储
	TargetStore string `json:"target" yaml:"target"`
}

CustomConfig 定制配置

type ElasticConfig

type ElasticConfig struct {
	// URL 指定ElasticSearch的请求节点
	// 默认值为http://127.0.0.1:9200
	URL string `json:"url" yaml:"url"`
	// IndexTmpl 索引模板
	// 模板字段说明:
	// Year 年份
	// Month 月份
	// Day 天数
	// Level 日志级别
	// Tag 标签
	// 默认值为{{.Year}}.{{.Month}}.{{.Day}}
	IndexTmpl string `json:"index" yaml:"index"`
	// TypeTmpl 文档类型模板
	// 模板字段说明:
	// Year 年份
	// Month 月份
	// Day 天数
	// Level 日志级别
	// Tag 标签
	// 默认值为ALogs
	TypeTmpl string `json:"type" yaml:"type"`
}

ElasticConfig ElasticSearch持久化存储

type FileConfig

type FileConfig struct {
	// FilePath 文件存储路径,
	// 默认值为logs
	FilePath string `json:"filepath" yaml:"filepath"`

	// FileNameTmpl 文件名格式模板
	// 模板字段说明:
	// Year 年份
	// Month 月份
	// Day 天数
	// Level 日志级别
	// Tag 标签
	// 默认值为{{.Year}}{{.Month}}{{.Day}}.log
	FileNameTmpl string `json:"filename" yaml:"filename"`

	// FileSize 单个文件大小(单位KB),
	// 默认值为512KB
	FileSize int64 `json:"filesize" yaml:"filesize"`

	// Item 日志项配置
	// 默认值:
	// time:{{.Year}}-{{.Month}}-{{.Day}} {{.Hour}}:{{.Minute}}:{{.Second}}.{{.MilliSecond}}
	// tmpl:{{.ID}} {{.Time}} {{.Level}} {{.Tag}} "{{.FileName}} {{.FileFuncName}} {{.FileLine}}" {{.Message}}
	Item LogItemConfig `json:"item" yaml:"item"`

	// 文件保留天数,
	// 默认值为0(保留全部文件)
	RetainDay int `json:"retian" yaml:"retain"`

	// 清理文件周期(单位分钟)
	// 默认为720分钟
	GCInterval int `json:"interval" yaml:"interval"`
}

FileStoreConfig 文件存储

type GlobalConfig

type GlobalConfig struct {
	// IsEnabled 是否启用日志
	// 参数说明:
	// 1表示启用
	// 2表示不启用
	// 默认值为1
	IsEnabled int `yaml:"enabled" json:"enabled"`
	// IsPrint 是否打印到控制台
	// 1表示打印
	// 2表示不打印
	IsPrint int `yaml:"print" json:"print"`
	// Rule 日志输出规则
	// 参数说明:
	// 0表示所有配置输出
	// 1表示指定Global配置输出
	// 2表示指定Tag配置输出
	// 3表示指定Level配置输出
	// 默认值为0
	Rule LogRule `json:"rule" yaml:"rule"`
	// Level 日志级别
	Level LogLevel `json:"level" yaml:"level"`
	// ShowFile 是否输出日志文件信息,包括:文件名、行数、函数名
	// 1表示输出
	// 2表示不输出
	ShowFile int `json:"showfile" yaml:"showfile"`
	// FileCaller 文件信息调用层级
	// 默认为5(当前调用)
	FileCaller int `json:"caller" yaml:"caller"`
	// Interval 读取缓冲区时间间隔(以秒为单位)
	// 默认为1秒
	Interval int `json:"interval" yaml:"interval"`
	// Buffer 缓冲区配置
	Buffer BufferConfig `json:"buffer" yaml:"buffer"`
	// TargetStore 目标存储
	TargetStore string `json:"target" yaml:"target" `
}

GlobalConfig 全局配置

type LevelConfig

type LevelConfig struct {
	// Values 日志级别
	Values []LogLevel `json:"values" yaml:"values"`
	// Config 配置
	Config CustomConfig `json:"config" yaml:"config"`
}

LevelConfig 日志级别配置

type LogBuffer

type LogBuffer interface {
	// Push 将日志项追加写入到缓冲区
	Push(LogItem) error
	// Pop 弹出缓冲区的第一个元素
	Pop() (*LogItem, error)
}

LogBuffer 提供日志缓冲区操作接口

type LogBufferEngine

type LogBufferEngine byte

LogBufferEngine 日志缓冲区引擎

const (
	// MEMORY_BUFFER 内存缓冲区
	MEMORY_BUFFER LogBufferEngine = iota + 1
	// REDIS_BUFFER redis缓冲区
	REDIS_BUFFER
)

type LogConfig

type LogConfig struct {
	// Console 控制台输出
	Console ConsoleConfig `json:"console" yaml:"console"`
	// Global 全局配置
	Global GlobalConfig `json:"global" yaml:"global"`
	// Tags 标签配置
	Tags []TagConfig `json:"tags" yaml:"tags"`
	// Level 日志级别配置
	Levels []LevelConfig `json:"levels" yaml:"levels"`
	// Store 存储配置
	Store StoreConfig `json:"store" yaml:"store"`
}

LogConfig 提供日志配置信息

type LogFile

type LogFile struct {
	Name      string `json:",omitempty"` // Name 文件名
	ShortName string `json:",omitempty"` // ShortName 短文件名
	FuncName  string `json:",omitempty"` // FuncName 函数名
	Line      int    `json:",omitempty"` // Line 文件行
}

LogFile 发生日志的文件

type LogItem

type LogItem struct {
	ID      uint64    `json:",omitempty"` // ID 唯一标识
	Time    time.Time `json:",omitempty"` // Time 日志发生时间
	Level   LogLevel  `json:",omitempty"` // Level 级别
	Tag     LogTag    `json:",omitempty"` // Tag 标签
	Message string    `json:",omitempty"` // Message 日志明细
	File    LogFile   `json:",omitempty"` // File 发生日志的文件
}

LogItem 日志项

func (*LogItem) ToMap

func (lt *LogItem) ToMap() map[string]interface{}

ToMap 将LogItem转换为字典值

type LogItemConfig

type LogItemConfig struct {
	// Tmpl 日志项模板
	// 模板字段说明:
	// ID 唯一标识
	// Time 日志发生时间
	// Level 级别
	// Tag 标签
	// Message 日志明细
	// FileName 文件名
	// ShortName 短文件名
	// FileFuncName 函数名
	// FileLine 文件行
	Tmpl string `json:"tmpl" yaml:"tmpl"`
	// TimeTmpl 时间模板
	// 模板字段说明:
	// Year 年份
	// Month 月份
	// Day 天数
	// Hour 小时
	// Minute 分钟
	// Second 秒
	// MilliSecond 毫秒
	TimeTmpl string `json:"time" yaml:"time"`
}

LogItemConfig 日志项配置

type LogLevel

type LogLevel byte

LogLevel 日志级别

const (
	DEBUG LogLevel = iota + 1
	INFO
	WARN
	ERROR
	FATAL
)

func (LogLevel) ToString

func (l LogLevel) ToString() string

type LogManage

type LogManage interface {
	// Write 写入日志信息
	Write(level LogLevel, tag LogTag, v ...interface{})
	// Writef 写入格式化日志信息
	Writef(level LogLevel, tag LogTag, format string, v ...interface{})
	// Console 将日志输出到控制台(不写入文件)
	Console(level LogLevel, tag LogTag, v ...interface{})
	// Consolef 将格式化日志输出到控制台(不写入文件)
	Consolef(level LogLevel, tag LogTag, format string, v ...interface{})
	// TotalNum 写入日志总条数
	TotalNum() int64
}

LogManage 提供日志的写入、存储及控制台输出接口

type LogRule

type LogRule byte

LogRule 日志规则

const (
	// AlwaysRule 检查全部输出规则
	AlwaysRule LogRule = iota
	// GlobalRule 按照Global输出
	GlobalRule
	// TagRule 按照TagRule输出
	TagRule
	// LevelRule 按照LevelRule输出
	LevelRule
)

type LogStore

type LogStore interface {
	// Store 将日志项写入到存储区
	Store(*LogItem) error
}

LogStore 提供日志持久化存储接口

type LogStoreEngine

type LogStoreEngine byte

LogStoreEngine 日志存储引擎

const (
	// FILE_STORE 文件存储
	FILE_STORE LogStoreEngine = iota + 1
)

type LogTag

type LogTag string

LogTag 日志标签

type MongoConfig

type MongoConfig struct {
	// URL 指定MongoDB的链接地址
	// 默认值为mongodb://127.0.0.1:27017
	URL string `json:"url" yaml:"url"`
	// DBTmpl 存储数据库名称模板
	// 模板字段说明:
	// Year 年份
	// Month 月份
	// Day 天数
	// Level 日志级别
	// Tag 标签
	// 默认值为alog
	DBTmpl string `json:"db" yaml:"db"`
	// CollectionTmpl 存储集合名称模板
	// 模板字段说明:
	// Year 年份
	// Month 月份
	// Day 天数
	// Level 日志级别
	// Tag 标签
	// 默认值为{{.Year}}{{.Month}}{{.Day}}
	CollectionTmpl string `json:"collection" yaml:"collection"`
}

MongoConfig 提供MongoDB持久化存储

type RedisConfig

type RedisConfig struct {
	// Default is tcp.
	Network string `json:"network" yaml:"network"`
	// host:port address.
	Addr string `json:"addr" yaml:"addr"`
	// A database to be selected after connecting to server.
	DB int64 `json:"db" yaml:"db"`
	// The maximum number of socket connections.
	// Default is 10 connections.
	PoolSize int `json:"poolsize" yaml:"poolsize"`
}

RedisConfig redis配置

type StoreConfig

type StoreConfig struct {
	// Redis redis存储配置
	Redis map[string]RedisConfig `json:"redis" yaml:"redis"`
	// File 文件存储配置
	File map[string]FileConfig `json:"file" yaml:"file"`
	// Elastic ElasticSearch存储配置
	Elastic map[string]ElasticConfig `json:"elastic" yaml:"elastic"`
	// Mongo MongoDB存储配置
	Mongo map[string]MongoConfig `json:"mongo" yaml:"mongo"`
}

StoreConfig 存储配置

type TagConfig

type TagConfig struct {
	// Name 标签名
	Names []LogTag `json:"names" yaml:"names"`
	// Level 日志级别
	Level LogLevel `json:"level" yaml:"level"`
	// Config 配置
	Config CustomConfig `json:"config" yaml:"config"`
}

TagConfig 标签配置

type TmplKey

type TmplKey uint

TmplKey 提供模板键的标识

const (
	// TmplConsoleTime 控制台时间模板
	TmplConsoleTime TmplKey = iota + 1
	// TmplConsole 控制台模板
	TmplConsole
)

Jump to

Keyboard shortcuts

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