logger

package module
v0.0.0-...-081176c Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 18 Imported by: 3

README

logger

golang 基本日志类库,支持自定义logid,tag,普通格式及json格式

获取

go get github.com/go-irain/logger

日志格式如下:

    {"atime":"2017-12-18 10:45:50","bfile":"logger/logger_test.go 41","clevel":"LOG","dlogid":"bbbbbbbbbbbbb","etag":"getUserInfo","msg":"rolling  fatal log"}
 2017-12-18 10:51:29 logger/logger_test.go:53 aaaaaaaaaaa login ERROR rolling  error log
  • 日期 2017-07-06
  • 时间 15:45:41
  • 等级 INFO
  • 编号 14e0e960525d46c7b1223cf09160cb38 (默认-)
  • 标签 login (默认-) 刻录本条日志的事件名称或分类等。
  • 代码 main:32 main.go文件的32行
  • 内容 used time 0.0005s

LogOjb对象JSON方法设置了则以json格式输出,正常模式下以文本格式输出

切割

按文件大小切割
    RollingLogger("log", "a.log", 10, 1, MB)
按日期切割
    DailyLogger("log", "a.log")

使用示例

对象使用
    package main

    import (
        "github.com/go-irain/logger"
    )

    func main(){
        logger.RollingLogger("log", "a.log", 10, 1, MB)
        logobj := new(LogObj).ID("bbbbbbbbbbbbb").Tag("getUserInfo").JSON()
        logobj.Log("rolling ", "log start")
        logobj.Debug("rolling ", "debug log")
        logobj.Info("rolling ", "info log")
        logobj.Warn("rolling ", "warn log")
        logobj.Error("rolling ", "error log")
        logobj.Fatal("rolling ", "fatal log")
        logobj.Log("rolling ", "fatal log")
    }
非对象使用
    package main

    import (
        "github.com/go-irain/logger"
    )

    func main(){
        logger.RollingLogger("log", "a.log", 10, 1, MB)
        logger.Log("rolling ", "log start")
        logger.Debug("rolling ", "debug log")
        logger.Info("rolling ", "info log")
        logger.Warn("rolling ", "warn log")
        logger.Error("rolling ", "error log")
        logger.Fatal("rolling ", "fatal log")
        logger.Log("rolling ", "fatal log")
    }
    

远程日志

需要发短信预警的日志使用方式如下:

    //需要到ums服务上注册告警服务id
    logger.SetRemoteUrl(alertURL) //ums告警地址
    logger.SetRemoteServerId(serverID)//ums告警服务id

    logger.Debug("remote","warning will send by sms")

日志文件名格式

文件名支持带"-"文件名和普通文件分割(例:bc7fg-a.log和a.log)

Documentation

Index

Constants

View Source
const (
	LOG int = iota
	DEBUG
	INFO
	WARN
	ERROR
	FATAL
)
View Source
const (
	OS_LINUX = iota
	OS_X
	OS_WIN
	OS_OTHERS
)
View Source
const (
	//TimeDayFormat 日期格式化到日
	TimeDayFormat = "2006-01-02"
	//TimeFormat 日期格式化到秒
	TimeFormat = "2006-01-02 15:04:05"
	//TimeFormat2 毫秒时间
	TimeFormat2 = "2006-01-02T15:04:05.000"
)

Variables

This section is empty.

Functions

func DailyLogger

func DailyLogger(fileDir, filename string)

DailyLogger new按日期分割日子类

func Debug

func Debug(v ...interface{}) bool

Debug DEBUG

func Error

func Error(v ...interface{}) bool

Error ERROR

func Fatal

func Fatal(v ...interface{}) bool

Fatal FATAL

func Info

func Info(v ...interface{}) bool

Info INFO

func JSON

func JSON(js bool)

JSON 设置日志格式

func Log

func Log(v ...interface{}) bool

Log LOG

func Post

func Post(url, param string) (string, error)

Post http请求发送日志

func RollingLogger

func RollingLogger(fileDir, fileName string, maxNumber int32, maxSize int64, _unit UNIT)

RollingLogger 生成按文件大小及数量分割日子类

func SetConsole

func SetConsole(isConsole bool)

SetConsole 设置终端是否显示

func SetLevel

func SetLevel(_level int)

SetLevel 设置日子级别

func SetRemoteServerId

func SetRemoteServerId(id string)

SetRemoteServerId 设置服务id

func SetRemoteUrl

func SetRemoteUrl(url string)

SetRemoteUrl 设置告警地址

func SetRollingFile

func SetRollingFile(fileDir, fileName string, maxNumber int32, maxSize int64, _unit UNIT)

SetRollingFile 生成按文件大小及数量分割日子类

func SetServiceName

func SetServiceName(name string)

SetServiceName 设置服务名称

func Trace

func Trace(level int, l *LogObj, v ...interface{}) bool

Trace write

func Warn

func Warn(v ...interface{}) bool

Warn WARN

Types

type LogFile

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

LogFile 日子结构

func (*LogFile) SetJSON

func (f *LogFile) SetJSON()

SetJSON 设置json格式

func (*LogFile) UnSetJSON

func (f *LogFile) UnSetJSON()

UnSetJSON 取消json格式

type LogObj

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

LogObj 日志对象

func NewLog

func NewLog(logid, tag string) *LogObj

NewLog 生成logobj对象

func (*LogObj) Data

func (l *LogObj) Data(d interface{}) *LogObj

Data 设置数据对象

func (*LogObj) Debug

func (l *LogObj) Debug(v ...interface{}) bool

Debug DEBUG

func (*LogObj) Error

func (l *LogObj) Error(v ...interface{}) bool

Error ERROR

func (*LogObj) Fatal

func (l *LogObj) Fatal(v ...interface{}) bool

Fatal FATAL

func (*LogObj) GetData

func (l *LogObj) GetData() interface{}

func (*LogObj) GetTag

func (l *LogObj) GetTag() string

GetTag 返回tag

func (*LogObj) ID

func (l *LogObj) ID(id string) *LogObj

ID 设置id

func (*LogObj) Info

func (l *LogObj) Info(v ...interface{}) bool

Info INFO

func (*LogObj) JSON

func (l *LogObj) JSON() *LogObj

JSON 设置日志格式为json

func (*LogObj) Log

func (l *LogObj) Log(v ...interface{}) bool

Log LOG

func (*LogObj) Logid

func (l *LogObj) Logid() string

Logid 获取logid

func (*LogObj) Tag

func (l *LogObj) Tag(tag string) *LogObj

Tag 设置id

func (*LogObj) UnJSON

func (l *LogObj) UnJSON() *LogObj

UnJSON 设置日志非json格式

func (*LogObj) Warn

func (l *LogObj) Warn(v ...interface{}) bool

Warn WARN

type UNIT

type UNIT int64
const (
	KB UNIT = 1 << (iota * 10)
	MB
	GB
	TB
)

Jump to

Keyboard shortcuts

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