logger

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 11 Imported by: 3

Documentation

Overview

Package logger 提供Tigo框架自带的log纪录功能

Package logger logger 包是Tigo框架的日志组件包,内部里面封装了日志相关操作,包括从配置文件初始化日志,通过config对象初始化日志,以及配置日志等级等。 目前该包不支持分布式日志,如果要使用分布式日志,推荐使用其他的第三方日志模块。 --------------------------------------------------------------------------------------------------------------------- 初始化日志配置,如下示例所示:

Basic Example:

logLevel := logger.LogLevel{
	Info: "stdout",
	Warning: "/path/to/logfile",
	Error: "/path/to/logfile",
	Trace: "/path/to/logfile",
	TimeRoll: "D*3",
}
logger.InitLoggerWithObject(logLevel)

--------------------------------------------------------------------------------------------------------------------- 日志打印用例:

Basic Example:

logger.Info.Print("Hello Logger")
logger.Info.Println("Hello Logger")
logger.Info.Printf("Hello %d", 250)

Index

Constants

View Source
const (
	TraceLevel int = iota + 1
	InfoLevel
	WarningLevel
	ErrorLevel
)

TraceLevel 等变量表示log实例的级别

Variables

This section is empty.

Functions

func InitError

func InitError(level string)

InitError 初始化Error,默认情况下输出到文件

func InitInfo

func InitInfo(level string)

InitInfo 初始化Info,默认情况下输出到终端

func InitLoggerWithConfigFile

func InitLoggerWithConfigFile(filePath string)

InitLoggerWithConfigFile 根据配置文件路径初始化log模块; 配置文件需要配置如下部分:

  • trace "discard": 不输出;"stdout": 终端输出不打印到文件;"/path/demo.log": 输出到指定文件
  • info "discard": 不输出;"stdout": 终端输出不打印到文件;"/path/demo.log": 输出到指定文件
  • warning "discard": 不输出;"stdout": 终端输出不打印到文件;"/path/demo.log": 输出到指定文件
  • error "discard": 不输出;"stdout": 终端输出不打印到文件;"/path/demo.log": 输出到指定文件

func InitLoggerWithObject

func InitLoggerWithObject(logLevel LogLevel)

InitLoggerWithObject 根据LogLevel结构体的实例初始化log模块; 配置文件需要配置如下部分:

  • Trace "discard": 不输出;"stdout": 终端输出不打印到文件;"/path/demo.log": 输出到指定文件
  • Info "discard": 不输出;"stdout": 终端输出不打印到文件;"/path/demo.log": 输出到指定文件
  • Warning "discard": 不输出;"stdout": 终端输出不打印到文件;"/path/demo.log": 输出到指定文件
  • Error "discard": 不输出;"stdout": 终端输出不打印到文件;"/path/demo.log": 输出到指定文件

func InitTrace

func InitTrace(level string)

InitTrace 初始化Trace,默认情况下不输出

func InitWarning

func InitWarning(level string)

InitWarning 初始化Warning,默认情况下输出到终端

func SetLogPath

func SetLogPath(defineLogPath string)

SetLogPath 设置log输出路径,警告:若使用了InitLoggerWithConfigFile和InitLoggerWithObject请不要使用此方法,会覆盖原有的log输出结构。

func StatusColor added in v1.6.1

func StatusColor(status int) (coloredStatus string)

StatusColor 给http状态码进行终端着色

  • status: 状态码

Types

type LogLevel

type LogLevel struct {
	Trace    string `json:"trace" yaml:"trace"`
	Info     string `json:"info" yaml:"info"`
	Warning  string `json:"warning" yaml:"warning"`
	Error    string `json:"error" yaml:"error"`
	TimeRoll string `json:"time_roll" yaml:"timeRoll"`
}

LogLevel 是log分级结构体

  • Trace 跟踪
  • Info 信息
  • Warning 预警
  • Error 错误

discard: 丢弃,stdout: 终端输出,文件路径表示log具体输出的位置

type TiLog

type TiLog struct {
	*log.Logger
	Level int
}

TiLog 是Tigo自定义的log结构体

var (
	Trace   *TiLog
	Info    *TiLog
	Warning *TiLog
	Error   *TiLog
)

Trace 等变量不同级别的log实例

func (*TiLog) Print

func (l *TiLog) Print(v ...interface{})

Print 打印log,不换行

func (*TiLog) Printf

func (l *TiLog) Printf(format string, v ...interface{})

Printf 格式化输出log

func (*TiLog) Println

func (l *TiLog) Println(v ...interface{})

Println 打印log并且换行

Jump to

Keyboard shortcuts

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