gologger

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: MIT Imports: 6 Imported by: 1

README

介绍

本项目是 projectdiscovery/gologger: A simple layer for leveled logging in go (github.com) 的改编,修改了一部分功能,让日志颜色更加舒服

安装

go get -u github.com/Zcentury/gologger

使用方法

基础语法

gologger.Info().Msg("验证成功")
gologger.Debug().Msg("身份验证已通过")
gologger.Warning().Msg("此版本已淘汰,请及时更新")
gologger.Error().Msg("参数校验失败!")
gologger.Fatal().Msg("程序中断!")

添加时间

gologger.Info().TimeStamp().Msg("验证成功")
gologger.Debug().TimeStamp().Msg("身份验证已通过")
gologger.Warning().TimeStamp().Msg("此版本已淘汰,请及时更新")
gologger.Error().TimeStamp().Msg("参数校验失败!")
gologger.Fatal().TimeStamp().Msg("程序中断!")

或者全局设置

gologger.LoggerOptions.SetTimestamp(true)
gologger.Info().Msg("验证成功")
gologger.Debug().Msg("身份验证已通过")
gologger.Warning().Msg("此版本已淘汰,请及时更新")
gologger.Error().Msg("参数校验失败!")
gologger.Fatal().Msg("程序中断!")

添加格外参数

gologger.Error().Str("错误位置", "18行").Msg("程序中断!")

msgf

gologger.Error().Msgf("程序中断!%s", "语法错误")

高级用法

自定义输出格式

定义一个结构体,实现 Format 接口

type FormatDemo struct{}

func (f *FormatDemo) Format(event *LogEvent) ([]byte, error) {
    result := ""
    
	for k, v := range event.Metadata {
        result += fmt.Sprintf("%s=%s", k, v)
	}

    return []byte(result)
}

配置全局Format

gologger.LoggerOptions.SetFormatter(&FormatDemo{})

然后正常使用

gologger.Info().Msg("验证成功")
gologger.Debug().Msg("身份验证已通过")
gologger.Warning().Msg("此版本已淘汰,请及时更新")
gologger.Error().Msg("参数校验失败!")
gologger.Fatal().Msg("程序中断!")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

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

func Debug

func Debug() *Event

func Error

func Error() *Event

func Fatal

func Fatal() *Event

func Info

func Info() *Event

func Warning

func Warning() *Event

func (*Event) Label

func (e *Event) Label(label string) *Event

func (*Event) Msg

func (e *Event) Msg(message string)

func (*Event) Msgf

func (e *Event) Msgf(format string, args ...interface{})

func (*Event) Str

func (e *Event) Str(key, value string) *Event

func (*Event) TimeStamp

func (e *Event) TimeStamp() *Event

type Logger

type Logger struct {
	// contains filtered or unexported fields
}
var (
	LoggerOptions *Logger
)

func (*Logger) Log

func (l *Logger) Log(event *Event)

func (*Logger) SetFormatter

func (l *Logger) SetFormatter(formatter formatter.Formatter)

SetFormatter 设置格式化方法

func (*Logger) SetTimestamp

func (l *Logger) SetTimestamp(timestamp bool)

SetTimestamp 是否自动添加时间戳

func (*Logger) SetWriter

func (l *Logger) SetWriter(writer writer.Writer)

SetWriter 设置输出方法

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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