logger

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: Apache-2.0 Imports: 12 Imported by: 59

README

logger

本项目通过logger接口的适配,让log15、zap、logrus的命令终端输出及文件保存格式一致。

特性

  • 统一终端输出效果
  • 终端输出及文件保存配置分开
  • 终端可颜色高亮输出
  • 可根据module选择性输出

项目地址

Github: https://github.com/chain5j/logger

使用

import (
	"github.com/chain5j/logger"
	"github.com/chain5j/logger/zap"
	//"github.com/chain5j/logger/logrus"
	//"github.com/chain5j/logger/log15"
	"sync"
	"testing"
)

func TestLog(t *testing.T) {
	log := zap.InitWithConfig(&logger.LogConfig{
		Console: logger.ConsoleLogConfig{
			Level:    4,
			Modules:  "*",
			ShowPath: false,
			UseColor: true,
			Console:  true,
		},
		File: logger.FileLogConfig{
			Level:    4,
			Modules:  "*",
			Save:     true,
			FilePath: "./logs",
			FileName: "log.json",
		},
	})

	var wg sync.WaitGroup
	startTime := logger.CurrentTime()

	debugLog := log.New("Debug")
	infoLog := log.New("Info")
	errorLog := log.New("Error")

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			infoLog.Info("=========================", "i", i)
			for j := 0; j < 100; j++ {
				debugLog.Debug("test1 debug", "i", i, "j", j)
				if i%9 == 0 {
					infoLog.Info("test2 info", "i", i, "j", j)
				}
				if i%13 == 0 {
					errorLog.Error("test2 info", "i", i, "j", j)
				}
			}
		}(i)
	}
	wg.Wait()
	log.Info("总耗时", "elapsed", logger.CurrentTime()-startTime)
}

LICENSE

logger 的源码允许用户在遵循 Apache 2.0 开源证书 规则的前提下使用。

版权

Copyright@2022 chain5j

chain5j

Documentation

Overview

Package logger

@author: xwc1125

Package logger

@author: xwc1125

Package logger

@author: xwc1125

Package logger

@author: xwc1125

Package logger

@author: xwc1125

Package logger

@author: xwc1125

Index

Constants

View Source
const (
	TermMsgJust = 44

	DefaultTimestampFormat     = time.RFC3339
	DefaultTermTimestampFormat = "2006-01-02 15:04:05.000"
	FieldKeyMsg                = "msg"
	FieldKeyLevel              = "lvl"
	FieldKeyTime               = "t"
	FieldKeyFile               = "f"
	FieldKeyModule             = "module"
	FieldKeyError              = "err"
)

Variables

View Source
var DefaultLogConfig = &LogConfig{
	Console: ConsoleLogConfig{
		Level:    3,
		Modules:  "*",
		ShowPath: false,
		UseColor: false,
		Console:  true,
	},
}

Functions

func ColorRender

func ColorRender(str string, color int, weight int, extraArgs ...interface{}) string

func Crit

func Crit(msg string, ctx ...interface{})

func CurrentTime

func CurrentTime() int64

CurrentTime 返回毫秒

func Debug

func Debug(msg string, ctx ...interface{})

func Error

func Error(msg string, ctx ...interface{})

func Fatal

func Fatal(v ...interface{})

func Fatalf

func Fatalf(format string, v ...interface{})

func Fatalln

func Fatalln(v ...interface{})

func GetColorByLevel

func GetColorByLevel(level Lvl) int

func GetWriter

func GetWriter(fileConfig FileLogConfig) (io.WriteCloser, error)

func Info

func Info(msg string, ctx ...interface{})

func Panic

func Panic(v ...interface{})

func Panicf

func Panicf(format string, v ...interface{})

func Panicln

func Panicln(v ...interface{})

func Print

func Print(v ...interface{})

func Printf

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

func Println

func Println(v ...interface{})

func SwitchLog

func SwitchLog(logKitName string)

SwitchLog 切换日志kit

func ToString

func ToString(i interface{}) string

ToString casts an interface to a string type.

func ToStringE

func ToStringE(i interface{}) (string, error)

ToStringE casts an interface to a string type.

func Trace

func Trace(msg string, ctx ...interface{})

func Warn

func Warn(msg string, ctx ...interface{})

Types

type ConsoleLogConfig

type ConsoleLogConfig struct {
	Level    Lvl    `json:"level" mapstructure:"level"`         // log level
	Modules  string `json:"modules" mapstructure:"modules"`     // need to show modules。"*":all
	ShowPath bool   `json:"show_path" mapstructure:"show_path"` // show path
	Format   string `json:"format" mapstructure:"format"`       // format

	UseColor bool `json:"use_color" mapstructure:"use_color"` // show console color
	Console  bool `json:"console" mapstructure:"console"`     // show console
}

func (*ConsoleLogConfig) GetModules

func (c *ConsoleLogConfig) GetModules() []string

type FileLogConfig

type FileLogConfig struct {
	Level  Lvl    `json:"level" mapstructure:"level"`   // log level
	Format string `json:"format" mapstructure:"format"` // format

	Save     bool   `json:"save" mapstructure:"save"`           // whether save file
	FilePath string `json:"file_path" mapstructure:"file_path"` // filepath
	FileName string `json:"file_name" mapstructure:"file_name"` // filename prefix

	MaxAge       int64 `json:"max_age" mapstructure:"max_age"`             // 文件最大保存时间[小时]
	RotationTime int64 `json:"rotation_time" mapstructure:"rotation_time"` // 日志切割时间间隔[小时]
}

func (*FileLogConfig) GetLogFile

func (c *FileLogConfig) GetLogFile() string

func (*FileLogConfig) GetMaxAge

func (c *FileLogConfig) GetMaxAge() int64

func (*FileLogConfig) GetRotationTime

func (c *FileLogConfig) GetRotationTime() int64

type LogConfig

type LogConfig struct {
	Console ConsoleLogConfig `json:"console" mapstructure:"console"`
	File    FileLogConfig    `json:"file" mapstructure:"file"`
}

LogConfig log config

type Logger

type Logger interface {
	Name() string
	New(module string, ctx ...interface{}) Logger

	Trace(msg string, ctx ...interface{})
	Debug(msg string, ctx ...interface{})
	Info(msg string, ctx ...interface{})
	Warn(msg string, ctx ...interface{})
	Error(msg string, ctx ...interface{})
	Crit(msg string, ctx ...interface{})

	Printf(format string, v ...interface{})
	Print(v ...interface{})
	Println(v ...interface{})
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})
	Panic(v ...interface{})
	Panicf(format string, v ...interface{})
	Panicln(v ...interface{})
}

func Log added in v1.0.3

func Log(obj interface{}) Logger

func New

func New(module string, ctx ...interface{}) Logger

New 方法用到root,因此此方法不能够应用到init()中,除非先执行了RegisterLog

func RegisterLog

func RegisterLog(logger Logger) Logger

RegisterLog 需要先注册logger

type Lvl

type Lvl int
const (
	LvlFatal Lvl = iota
	LvlError
	LvlWarn
	LvlInfo
	LvlDebug
	LvlTrace
)

func (Lvl) String

func (l Lvl) String() string

Directories

Path Synopsis
Package log15 @author: xwc1125
Package log15 @author: xwc1125
Package logrus @author: xwc1125 @date: 2021/8/30 Package logrus @author: xwc1125 @date: 2021/9/2 Package logrus @author: xwc1125 @date: 2021/8/30
Package logrus @author: xwc1125 @date: 2021/8/30 Package logrus @author: xwc1125 @date: 2021/9/2 Package logrus @author: xwc1125 @date: 2021/8/30
zap
Package zap @author: xwc1125 Package zap @author: xwc1125
Package zap @author: xwc1125 Package zap @author: xwc1125
bufferpool
Package bufferpool houses zap's shared internal buffer pool.
Package bufferpool houses zap's shared internal buffer pool.

Jump to

Keyboard shortcuts

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