logger

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: MIT Imports: 13 Imported by: 5

README

logger

日志模块

feture

  • 多输出 - 同时支持标准输出,文件,socket等
  • 配置驱动
  • 文件切割 - 基于时间和文件大小

example

test.conf

[log]
type = stdout,file // 打标准输出和文件
prefix = xxx // 日志文件前缀 xxx.log.xxxx

file.enable = true
file.dir = /xxx/log
file.rotate_by_hour = true
"file.rotate_size = 10240000
file.level = TRACE
file.format = [%L][%Z][%S]%M // 日志输出格式
file.seprated = true // 如果为true则将info以上日志写进xxx.log.wf,其他写进xxx.log; false=所有级别在一个文件
file.auto_clear = true
file.clear_hours = 72

stdout.enable = false
"stdout.format = [%t %d] [%L] %M
"stdout.format = [%t %d] %M
stdout.level = TRACE

[offline]
dir = /tmp/log
file_list = public,track // 写两个文件,一个public.log 一个track.log
rotate_by_hour=true

demo.go

import (
	"github.com/dup2X/gopkg/config"
	"github.com/dup2X/gopkg/logger"
)

func main() {
	cfg, err := config.New("../testdata/test.conf")
	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}

	err = logger.NewLoggerWithConfig(cfg)
	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}

	ctx := context.TODO()
	for i := 0; i < 10; i++ {
		logger.Trace(logger.DLTagUndefined, "trace")
		logger.Debug(logger.DLTagUndefined, "trace")
		logger.Info(logger.DLTagUndefined, "trace")
		logger.Warn(logger.DLTagUndefined, "trace")
		logger.Error(logger.DLTagUndefined, "trace")
		logger.Debugf(ctx, "trace=%d", logger.DLTagUndefined, i)
	}
	time.Sleep(time.Second * 2)

	// 打离线日志
	trackSec, err := cfg.GetSection("offline")
	if err != nil {
		fmt.Printf("%v\n", err)
		return
	}
	logger.InitTrack(trackSec)
	logger.Track("public", "xxxxxxxx") // 往public.log 写入
	logger.Track("track", "xxxxxxxx")  // 往 trace.log写入
	...
}

Documentation

Overview

Package logger ...

Package logger ...

Package logger ...

Package logger ...

Package logger ...

Index

Constants

View Source
const (
	// DLTagUndefined ..
	DLTagUndefined DLTag = " _undef"
	// DLTagModuleStart ...
	DLTagModuleStart = " _com_module_start"
	// DLTagModuleStop ...
	DLTagModuleStop = " _com_module_stop"
	// DLTagConfLoad ...
	DLTagConfLoad = " _com_conf_load"
	// DLTagConfReload ...
	DLTagConfReload = " _com_conf_reload"
	// DLTagDataLoad ...
	DLTagDataLoad = " _com_data_load"
	// DLTagDataReload ...
	DLTagDataReload = " _com_data_reload"
	// DLTagMysqlFailed ...
	DLTagMysqlFailed = " _com_mysql_failed"
	// DLTagRedisFailed ...
	DLTagRedisFailed = " _com_redis_failure"
	// DLTagThriftFailed ...
	DLTagThriftFailed = " _com_thrift_failure"
	// DLTagThriftSuccess ...
	DLTagThriftSuccess = " _com_thrift_success"
	// DLTagHTTPSuccess ...
	DLTagHTTPSuccess = " _com_http_success"
	// DLTagHTTPFailed ...
	DLTagHTTPFailed = " _com_http_failure"
	// DLTagBackendRPCFailed ...
	DLTagBackendRPCFailed = " _com_interactive_failure"
	// DLTagRequestIn ...
	DLTagRequestIn = " _com_request_in"
	// DLTagRequestOut ...
	DLTagRequestOut = " _com_request_out"
	// DLTagTegPushSend ...
	DLTagTegPushSend = " _com_teg_push_send"
	// DLTagTegPushRecv ...
	DLTagTegPushRecv = " _com_teg_push_recv"
)
View Source
const (
	//TRACE :logLevel TRACE
	TRACE logLevel = iota
	//DEBUG :logLevel DEBUG
	DEBUG
	//INFO :logLevel INFO
	INFO
	//WARNING :logLevel WARNING
	WARNING
	//ERROR :logLevel ERROR
	ERROR
	//FATAL :logLevel FATAL
	FATAL
)
View Source
const (
	//LogTypeStdout :Log Type Stdout
	LogTypeStdout = "stdout"
	//LogTypeFile :Log Type File
	LogTypeFile = "file"
)

Variables

This section is empty.

Functions

func Close

func Close()

Close :

func Debug

func Debug(tag DLTag, args ...interface{})

Debug ...

func Debugf

func Debugf(ctx context.Context, tag DLTag, format string, args ...interface{})

Debugf ...

func Error

func Error(tag DLTag, args ...interface{})

Error ...

func Errorf

func Errorf(ctx context.Context, tag DLTag, format string, args ...interface{})

Errorf ...

func Fatal

func Fatal(tag DLTag, args ...interface{})

Fatal ...

func Fatalf

func Fatalf(ctx context.Context, tag DLTag, format string, args ...interface{})

Fatalf ...

func Info

func Info(tag DLTag, args ...interface{})

Info ...

func Infof

func Infof(ctx context.Context, tag DLTag, format string, args ...interface{})

Infof ...

func InitTrack

func InitTrack(sec config.Sectioner)

InitTrack :Init

func NewLoggerWithConfig

func NewLoggerWithConfig(cfg config.Configer) error

NewLoggerWithConfig :by cfg

func PrintStack

func PrintStack() []byte

PrintStack :print

func Public

func Public(ctx context.Context, key string, pairs map[string]interface{})

Public ...

func PublicString

func PublicString(ctx context.Context, public string)

PublicString ...

func RegisterContextFormat

func RegisterContextFormat(fn func(ctx context.Context) string)

RegisterContextFormat ...

func RegisterLogger

func RegisterLogger(obj Logger)

RegisterLogger ...

func SetDepth

func SetDepth(delt int)

SetDepth :

func Trace

func Trace(tag DLTag, args ...interface{})

Trace ...

func Tracef

func Tracef(ctx context.Context, tag DLTag, format string, args ...interface{})

Tracef ...

func Track

func Track(prefix, msg string)

Track :Track

func Warn

func Warn(tag DLTag, args ...interface{})

Warn ...

func Warnf

func Warnf(ctx context.Context, tag DLTag, format string, args ...interface{})

Warnf ...

Types

type DLTag

type DLTag string

DLTag ...

type LogType

type LogType string

LogType :Log Type

type Logger

type Logger interface {
	//Trace :Trace interface for log
	Trace(args ...interface{})
	//Tracef :Tracef interface for log
	Tracef(format string, args ...interface{})
	//Debug :Debug interface for log
	Debug(args ...interface{})
	//Debugf :Debugf interface for log
	Debugf(format string, args ...interface{})

	//Info :Info interface for log
	Info(args ...interface{})
	//Infof :Infof interface for log
	Infof(format string, args ...interface{})
	//Warn :Warn interface for log
	Warn(args ...interface{})
	//Warnf :Warnf interface for log
	Warnf(format string, args ...interface{})
	//Error :Error interface for log
	Error(args ...interface{})
	//Errorf :Errorf interface for log
	Errorf(format string, args ...interface{})
	//Fatal :Fatal interface for log
	Fatal(args ...interface{})
	//Fatalf :Fatalf interface for log
	Fatalf(format string, args ...interface{})
	//Close :Close interface for log
	Close()
}

Logger :interface for log

func GetLogger

func GetLogger(t LogType) Logger

GetLogger :

func GetLoggers

func GetLoggers() Logger

GetLoggers :

func NewLoggerWithOption

func NewLoggerWithOption(dir string, opts ...Option) Logger

NewLoggerWithOption ...

type Option

type Option func(fs *fileSetting)

Option ...

func SetAutoClear

func SetAutoClear(autoClear bool) Option

SetAutoClear 自动清理日志

func SetAutoClearHours

func SetAutoClearHours(keepHours int) Option

SetAutoClearHours 清理keepHours小时以前的日志

func SetFileCallerDepth

func SetFileCallerDepth(dep int) Option

SetFileCallerDepth 设置当前的depth

func SetFormat

func SetFormat(format string) Option

SetFormat 设置日志格式

func SetLinkable

func SetLinkable(useSoftLink bool) Option

SetLinkable 是否设置软连接

func SetMaxLevel

func SetMaxLevel(logLevel string) Option

SetMaxLevel 设置日志级别

func SetPrefix

func SetPrefix(prefix string) Option

SetPrefix 设置日志文件名前缀,如xxx,则日志为xxx.log

func SetRotateByHour

func SetRotateByHour() Option

SetRotateByHour 按小时切分

func SetSeprate

func SetSeprate(seprated bool) Option

SetSeprate info日志和wf日志分开

Directories

Path Synopsis
package main ...
package main ...

Jump to

Keyboard shortcuts

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