logit

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: Apache-2.0 Imports: 8 Imported by: 20

README

📝 logit

License

logit 是一个简单易用并且是基于级别控制的日志库,可以应用于所有的 GoLang 应用程序中。

Read me in English.

🥇 功能特性
  • 支持日志级别控制,目前一共有四个日志级别
  • 支持开启或者关闭日志功能,线上环境可以关闭或调高日志级别
  • 支持记录日志到文件中,并且可以按照时间间隔进行自动分割

历史版本的特性请查看 HISTORY.md。未来版本的新特性和计划请查看 FUTURE.md

🚀 安装方式

唯一需要的依赖就是 Golang 运行环境.

Go modules

$ go get -u github.com/FishGoddess/logit

您也可以直接编辑 go.mod 文件,然后执行 go build

module your_project_name

go 1.14

require (
    github.com/FishGoddess/logit v0.0.5
)

Go path

$ go get -u github.com/FishGoddess/logit

logit 没有任何其他额外的依赖,纯使用 Golang 标准库 完成。

package main

import (
    "github.com/FishGoddess/logit"
)

func main() {
    
    // Log as you want.
    logit.Debug("I am a debug message! But I will not be logged in default level!")
    logit.Info("I am an info message!")
    logit.Warn("I am a warn message!")
    logit.Error("I am an error message!")
    
    // Change logger level.
    logit.ChangeLevelTo(logit.DebugLevel)

    // If you want format your message, just add arguments!
    logit.Info("format info message! id = %d, content = %s", 1, "info!")
}
📖 参考案例

更多使用案例请查看 _examples 目录。

🔥 性能测试
$ go test -v ./_examples/benchmarks_test.go -bench=. -benchtime=20s

测试文件:_examples/benchmarks_test.go

测试 单位时间内运行次数 (large is better) ns/op (small is better) B/op (small is better) allocs/op (small is better)
logit   4405342 5409 ns/op   904 B/op 12 allocs/op
logit (关闭文件信息) 20341443 1130 ns/op     32 B/op   4 allocs/op
logrus   2990408 7991 ns/op 1633 B/op 52 allocs/op
Golang log   5308578 4539 ns/op   920 B/op 12 allocs/op
Golog 15536137 1556 ns/op   232 B/op 16 allocs/op

测试环境:I7-6700HQ CPU @ 2.6 GHZ,16 GB RAM

注意:golog 库是不会输出文件信息的,也就是少了运行时操作(runtime.Caller 方法),性能自然会高很多, 但是这个功能感觉还是比较实用的,尤其是在查找错误的时候,所以我们还是加了这个功能! 如果你更在乎性能,那我们也提供了一个选项可以关闭文件信息的查询(开发中)!

由于目前的 logit 是基于 Golang log 的,所以成绩相比更差,后续会重新设计内部日志输出模块,所以当前成绩仅供参考!

👥 贡献者

如果您觉得 logit 缺少您需要的功能,请不要犹豫,马上参与进来,发起一个 issue

📦 使用 logit 的项目
项目 作者 描述

Documentation

Overview

Package logit provides an easy way to use foundation for your logging operations.

1. The basic usage:

// Log messages with four levels.
// Notice that the default level is info, so first line of debug message
// will not be logged! If you want to change level, see logit.ChangeLevelTo
logit.Debug("I am a debug message! But I will not be logged in default level!")
logit.Info("I am an info message!")
logit.Warn("I am a warn message!")
logit.Error("I am an error message!")

// Also, you can create a new independent Logger to use. See logit.NewLogger.

// If you want format your message, just add arguments!
logit.Info("format info message! id = %d, content = %s", 1, "info!")

2. logger:

// NewStdoutLogger creates a new Logger holder to standard output, generally a terminal or a console.
logger := logit.NewStdoutLogger(logit.DebugLevel)

// Then you will be easy to log!
logger.Debug("this is a debug message!")
logger.Info("this is a info message!")
logger.Warn("this is a warn message!")
logger.Error("this is a error message!")

// NewLogger creates a new Logger holder.
// The first parameter "os.Stdout" is a writer for logging.
// As you know, file also can be written, just replace "os.Stdout" with your file!
// The second parameter "logit.DebugLevel" is the level of this Logger.
logger = logit.NewLogger(os.Stdout, logit.DebugLevel)

// If you want format your message, just add arguments!
logger.Info("format info message! id = %d, content = %s", 1, "info!")

3. enable or disable:

// Every new Logger is running.
logger := logit.NewLogger(os.Stdout, logit.DebugLevel)
logger.Info("I am running!")

// Shutdown the Logger.
// So the info message next line will not be logged!
logger.Disable()
logger.Info("I will not be logged!")

// Enable the Logger.
// The info message next line will be logged again!
logger.Enable()
logger.Info("I am running again!")

4. change logger level:

logit.Debug("Default logger level is info, so debug message will not be logged!")

// Change logger level to debug level.
logit.ChangeLevelTo(logit.DebugLevel)

logit.Debug("Now debug message will be logged!")

5. log to file:

// NewFileLogger creates a new logger which logs to file.
// It just need a file path like "D:/test.log" and a logger level.
logger := logit.NewFileLogger("D:/test.log", logit.DebugLevel)
logger.Info("我是 info 日志!")

// NewDurationRollingLogger creates a duration rolling logger with given duration.
// You should appoint a directory to store all log files generated in this time.
// Notice that duration must not less than minDuration (generally time.Second), see wrapper.minDuration.
// Also, default filename of log file is like "20200304-145246-45.log", see wrapper.NewFilename.
// If you want to appoint another filename, check this and do it by this way.
// See wrapper.NewDurationRollingFile (it is an implement of io.writer).
logger = logit.NewDurationRollingLogger("D:/", time.Second, logit.DebugLevel)
logger.Info("Rolling!!!")

Index

Constants

View Source
const Version = "0.0.5"

Version is the version string representation of the "logit" package.

Variables

This section is empty.

Functions

func ChangeLevelTo added in v0.0.2

func ChangeLevelTo(level LoggerLevel)

ChangeLevelTo will change the level of logit to newLevel.

func Debug added in v0.0.2

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

Debug will output msg as a debug message.

func Disable added in v0.0.2

func Disable()

Disable sets logit on shutdown status.

func Enable added in v0.0.2

func Enable()

Enable sets logit on running status.

func Error added in v0.0.2

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

Error will output msg as an error message.

func Info added in v0.0.2

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

Info will output msg as an info message.

func Warn added in v0.0.4

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

Warn will output msg as a warn message.

Types

type Logger

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

Logger is a struct based on "log.Logger".

func NewDurationRollingLogger added in v0.0.5

func NewDurationRollingLogger(directory string, duration time.Duration, level LoggerLevel) *Logger

NewDurationRollingLogger creates a duration rolling logger with given duration. You should appoint a directory to store all log files generated in this time. Notice that duration must not less than minDuration (generally time.Second), see wrapper.minDuration. Also, default filename of log file is like "20200304-145246-45.log", see wrapper.NewFilename. If you want to appoint another filename, check this and do it by this way. See wrapper.NewDurationRollingFile (it is an implement of io.writer).

func NewFileLogger added in v0.0.5

func NewFileLogger(logFile string, level LoggerLevel) *Logger

NewFileLogger returns a Logger holder which log to a file with given logFile and level.

func NewLogger

func NewLogger(out io.Writer, level LoggerLevel) *Logger

NewLogger create one Logger with given out and level. The first parameter out is a writer for logging. The second parameter level is the level of this Logger. It returns a new running Logger holder.

func NewStdoutLogger added in v0.0.2

func NewStdoutLogger(level LoggerLevel) *Logger

NewStdoutLogger returns a Logger holder with given logger level.

func (*Logger) ChangeLevelTo added in v0.0.2

func (l *Logger) ChangeLevelTo(newLevel LoggerLevel)

ChangeLevelTo will change the level of current Logger to newLevel.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...interface{})

Debug will output msg as a debug message.

func (*Logger) Disable

func (l *Logger) Disable()

Disable sets l on shutdown status.

func (*Logger) Enable

func (l *Logger) Enable()

Enable sets l on running status.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...interface{})

Error will output msg as an error message.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...interface{})

Info will output msg as an info message.

func (*Logger) Warn added in v0.0.4

func (l *Logger) Warn(msg string, args ...interface{})

Warn will output msg as a warn message.

type LoggerLevel added in v0.0.4

type LoggerLevel uint8

LoggerLevel is the type representation of the level.

const (
	DebugLevel LoggerLevel = iota
	InfoLevel
	WarnLevel
	ErrorLevel
)

Constants about logger level.

Directories

Path Synopsis
Package wrapper provides some writers to extend your Logger.
Package wrapper provides some writers to extend your Logger.

Jump to

Keyboard shortcuts

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