Help us shape the future of Go! Take the 2026 Go Developer Survey

ezlog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2018 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Ezlog

Ezlog 是一个Go语言的日志输出工具,支持日志文件分割和日志缓冲功能。

初始化

Ezlog支持的初始化参数如下:

  • Filename 文件路径
  • Pattern 可选,日期表达式(默认:"")
  • Suffix 可选,日志文件后缀(默认:"log")
  • LogLevel 可选,日志级别(默认:"LVL_DEBUG")
  • BufferSize 可选,缓存容量(默认:0,代表不启用缓存)

在指定了 Filename、Pattern、Suffix时,文件输出路径会按:Filename + Pattern + "." + Suffix 拼接

最简初始化

日志的log级别默认为debug,输出日志位置:/var/log/demo.log

	logger := ezlog.Log{Filename: "/var/log/demo"}	
指定日志级别

日志级别常量:

  • LVL_DEBUG
  • LVL_INFO
  • LVL_WARN
  • LVL_ERROR

指定日志级别为info

	logger := ezlog.Log{Filename: "/var/log/demo", LogLevel: ezlog.LVL_INFO}
指定日志文件后缀

指定文件后缀为“txt”,输出日志位置:/var/log/demo.txt

	logger := ezlog.Log{Filename: "/var/log/demo", Suffix:   "txt"}
日志分割

Pattern 参数支持时间表达式。例如按秒分割文件"-2006-01-02_150405",如果当前时间为:"2018-12-11 11:28:00",输出日志位置就是 "/var/log/demo-2018-12-11_112800.log"

	logger := ezlog.Log{Filename: "/var/log/demo", Pattern:  "-2006-01-02_150405"}	
日志缓冲

在初始化参数设置BufferSize,可以设置缓冲区大小,ezlog会在日志信息超过缓冲大小时才将缓冲区的数据写入文件,提高日志写入性能。为了保证没有数据丢失,请在应用中使用func (l *Log) Flush() error 方法手动清空缓冲区数据。

	logger := ezlog.Log{Filename: "/var/log/demo",BufferSize:  1024}
	logger.Print("this is a test log.")
	...
	logger.Flush()
	

在设置BufferSize之后,ezlog还会默认每200ms自动清空缓冲区并将缓冲区的数据写入文件。可以使用func (l *Log) SetFlushDuration(duration int) error 方法指定时间间隔,参数单位为毫秒(ms)

	logger := ezlog.Log{Filename: "/var/log/demo",BufferSize:  1024}
	logger.SetFlushDuration(100)
	...

如果不想使用自动缓冲清空功能,可以使用func (l *Log) DisableAutoFlush() error 方法。

	logger := ezlog.Log{Filename: "/var/log/demo",BufferSize:  1024}
	logger.DisableAutoFlush()
        logger.Print("this is a test log.")
	...
	logger.Flush()
方法
Print(msg string)

输出指定字符串,该方法会始终打印日志,不受日志级别设置的影响

	logger.Print("this is a test log.")
Printf(msg string)

格式化输出,该方法会始终打印日志,不受日志级别设置的影响

	logger.Printf("this is a test log. %d", 123)
Debug(msg string)

输出指定字符串,Debug 级别

	logger.Debug("this is a debug log.")
Info(msg string)

输出指定字符串,Info 级别

	logger.Info("this is a info log.")
Warn(msg string)

输出指定字符串,Warn 级别

	logger.Warn("this is a warn log.")
Error(msg string)

输出指定字符串,Error 级别

	logger.Error("this is a error log.")
示例
	package main
	
	import (
		"github.com/sialot/ezlog"
	)
	
	func main() {
	
		logger := ezlog.Log{
			Filename:   "/var/log/demo",
			Pattern:    "-2006-01-02_150405",
			Suffix:     "txt",
			LogLevel:   ezlog.LVL_DEBUG,
			BufferSize: 1024}
	
		logger.Print("this is a test log.")
		logger.Printf("this is a test log. %d", 123)
		logger.Debug("debug msg")
		logger.Info("info msg")
		logger.Warn("warn msg")
		logger.Error("error msg")
	
	}

Documentation

Index

Constants

View Source
const (
	LVL_DEBUG = 1 << iota
	LVL_INFO
	LVL_WARN
	LVL_ERROR
)

日志级别常量 LogLevel Constants

Variables

This section is empty.

Functions

This section is empty.

Types

type Log

type Log struct {
	Filename   string
	Pattern    string
	Suffix     string
	LogLevel   int
	BufferSize int
	// contains filtered or unexported fields
}

Log

Filename 文件路径 Pattern 日期表达式(可选,默认无) Suffix 日志文件后缀(可选,默认"log") LogLevel 日志级别(可选,默认"LVL_DEBUG") BufferSize 缓存容量 autoFlush 自动flush标志 curLogFile 当前日志文件 buf 用于输出的日志数据缓存

func (*Log) Debug

func (l *Log) Debug(msg string)

debug级别

func (*Log) DisableAutoFlush

func (l *Log) DisableAutoFlush() error

关闭自动flush disable auto flush

func (*Log) Error

func (l *Log) Error(msg string)

error级别

func (*Log) Flush

func (l *Log) Flush() error

清空缓冲区,写入日志文件 Flush

func (*Log) Info

func (l *Log) Info(msg string)

info级别

func (*Log) Print

func (l *Log) Print(msg string)

业务日志,不受日志级别影响

func (*Log) Printf

func (l *Log) Printf(msg string, v ...interface{})

业务日志,不受日志级别影响 Arguments are handled in the manner of fmt.Printf.

func (*Log) SetFlushDuration

func (l *Log) SetFlushDuration(duration int)

设置自动Flush间隔 SetFlushDuration

func (*Log) Warn

func (l *Log) Warn(msg string)

warn级别

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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