logit

package module
v0.0.2 Latest Latest
Warning

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

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

README

📝 logit

License

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

Read me in English.

🥇 功能特性
  • 支持日志级别控制,目前一共有四个日志级别
  • 支持开启或者关闭日志功能,线上环境可以关闭或调高日志级别
🚀 安装方式

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

Go modules

$ go get github.com/FishGoddess/logit@v0.0.2

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

module your_project_name

go 1.14

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

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.Warning("I am a warning message!")
    logit.Error("I am an error message!")
    
    // change log level.
    logit.ChangeLevelTo(logit.DebugLevel)
}
📖 参考案例

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

🔥 性能测试
$ go test -v -bench=. -benchtime=20s
测试 单位时间内运行次数 (large is better) ns/op (small is better) B/op (small is better) allocs/op (small is better)
logit 4800000 5062 ns/op 864 B/op 8 allocs/op
Golang log 5400000 4730 ns/op 928 B/op 12 allocs/op

由于目前的 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.Warning("I am a warning message!")
logit.Error("I am an error message!")

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

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.Warning("this is a warning 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)

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 log level:

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

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

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

Index

Constants

View Source
const Version = "0.0.2"

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 LogLevel)

ChangeLevelTo will change the level of logit to newLevel.

func Debug added in v0.0.2

func Debug(msg string)

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)

Error will output msg as an error message.

func Info added in v0.0.2

func Info(msg string)

Info will output msg as an info message.

func Warning added in v0.0.2

func Warning(msg string)

Warning will output msg as a warning message.

Types

type LogLevel

type LogLevel uint8

LogLevel is the type representation of the level.

const (
	DebugLevel LogLevel = iota
	InfoLevel
	WarningLevel
	ErrorLevel
)

Constants about log level.

type Logger

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

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

func NewLogger

func NewLogger(out io.Writer, level LogLevel) *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 LogLevel) *Logger

NewStdoutLogger returns a Logger holder with given log level.

func (*Logger) ChangeLevelTo added in v0.0.2

func (l *Logger) ChangeLevelTo(newLevel LogLevel)

ChangeLevelTo will change the level of current Logger to newLevel.

func (*Logger) Debug

func (l *Logger) Debug(msg string)

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)

Error will output msg as an error message.

func (*Logger) Info

func (l *Logger) Info(msg string)

Info will output msg as an info message.

func (*Logger) Warning

func (l *Logger) Warning(msg string)

Warning will output msg as a warning message.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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