logger

package module
v0.0.0-...-2bdc6c8 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2018 License: MIT Imports: 15 Imported by: 6

README

logger GoDoc

###获取方式

go get github.com/coffeehc/logger

1.0.1 更新日志 增加了 log.Logger的适配器方法 使用方式

    CreatLoggerAdapter(level byte, timeFormat string, format string, out io.Writer) *log.Logger

用于在某些可以注入 log.Logger的地方使用,输出仍然能适配为本项目格式,Writer 同样可以使用内置的FileWrite

=======================

###使用方式 coffeehc/logger 是一个基础的日志框架,提供扩展的开放logFilter接口,使用者可以自己定义何种级别的logger发布到对应的io.Writer中

日志级别定义了5个:

  1. trace
  2. debug
  3. info
  4. warn
  5. error
API说明
    func Trace(format string, v ...interface{}) string
    func Debug(format string, v ...interface{}) string
    func Warn(format string, v ...interface{}) string
    func Info(format string, v ...interface{}) string
    func Error(format string, v ...interface{}) string
编码方式定义Appender,用于程序自主定义日志
    func AddAppender(appender LoggerAppender)

    type LoggerAppender struct {
        Level         string `yaml:"level"`         //日志级别
        Package_path  string `yaml:"package_path"`  //日志路径
        Adapter       string `yaml:"adapter"`       //适配器,console,file两种
        Rotate        int    `yaml:"rotate"`        //日志切割个数
        Rotate_policy string `yaml:"rotate_policy"` //切割策略,time or size or  default
        Rotate_scope  int64  `yaml:"rotate_scope"`  //切割范围:如果按时间切割则表示的n分钟,如果是size这表示的是文件大小MB
        Log_path      string `yaml:"log_path"`      //如果适配器使用的file则用来指定文件路径
        Timeformat    string `yaml:"timeformat"`    //日志格式
        Format        string `yaml:"format"`
}
自定义更低级别的Filter

    func AddFileter(level byte, path string, timeFormat string, format string, out io.Writer)

只需要将out实现为任意想输出的方式,tcp,http,db等都可以

配置说明

使用配置的方式(yaml语法),配置文件内容如下:

context: Default
appenders:
 -
  level: debug
  package_path: /
  adapter: console
  #使用golang自己的timeFormat
  timeformat: 2006-01-02 15:04:05
  format: %T %L %C %M
 -
  level: error
  package_path: /
  adapter: file
  log_path: /logs/box/box.log
  rotate: 3
  #备份策略:size or time  or default
  rotate_policy: time
  #备份范围:如果策略是time则表示时间间隔N分钟,如果是size则表示每个日志的最大大小(MB)
  rotate_scope: 10

系统默认会取-loggerConf参数的值来加载配置文件,如果没有指定则使用debug对所有的包路径下的日志打印到控制台

2015-4-29

  1. AddAppender用于自己使用编程方式来定义日志,其实也可以用底层的Filter接口来扩展会更灵活

2015-5-15 在配置中加入了format的参数设置,提供四种标记来组合日志,标记说明如下:

  1. %T:时间标记,会与timeformat配合使用
  2. %L:日志级别,这会输出相应的日志级别
  3. %C:代码信息,这包括包文件描述和日志在第几行打印
  4. %M:这个就是需要打印的具体日志内容

支持在程序运行目录下查找conf/log.yml文件作为默认的日志配置,如果不指定-loggerConf的话,可以直接将配置文件放在这下面,程序启动的时候可以直接读取配置

###TODO

  1. 暂不支持TCP方式存储日志,以后看情况再提供,只要实现io.Writer的接口就可以了,自己动手,丰衣足食

Documentation

Index

Constants

View Source
const (
	//AdapterConsole 适配定义,控制台方式
	AdapterConsole = "console"
	//AdapterFile 适配定义,文件方式
	AdapterFile = "file"
)
View Source
const (
	FormatTime     = "%T" //显示时间
	FormatLevel    = "%L" //显示级别
	FormatCodeInfo = "%C" //显示代码详情
	FormatMessage  = "%M" //显示日志消息
)

日志格式定义

View Source
const (
	// LevelError  Error level
	LevelError Level = 1 << 0
	// LevelWarn Warn level
	LevelWarn Level = 1<<1 | LevelError
	//LevelInfo Info level
	LevelInfo Level = 1<<2 | LevelWarn
	// LevelDebug Debug level
	LevelDebug Level = 1<<3 | LevelInfo
	// LevelTrace Trace level
	LevelTrace Level = 1<<4 | LevelDebug

	// FromatTimeSecond 显示格式为2006-01-02 15:04:05
	FromatTimeSecond string = "2006-01-02 15:04:05"
	// FormatTimeNanosecond 显示格式为2006-01-02 15:04:05.999999999
	FormatTimeNanosecond string = "2006-01-02 15:04:05.999999999"
	// FormatTimeAll 显示格式为2006-01-02 15:04:05.999999999 -0700 UTC
	FormatTimeAll string = "2006-01-02 15:04:05.999999999 -0700 UTC"
)

Variables

View Source
var (
	//DefaultLevel 默认的日志级别
	DefaultLevel = "error"
	//DefaultBufsize 日志缓冲区默认大小
	DefaultBufsize = 1024
	//DefaultTimeout 默认超时时间
	DefaultTimeout = time.Second * 1
)

Functions

func AddAppender

func AddAppender(appender *Appender)

AddAppender 添加日志配置,支持Console与File两种方式

func AddFilter

func AddFilter(level Level, path string, timeFormat string, format string, out io.Writer)

AddFilter 添加日志过滤器,参数说明:级别,包路径,时间格式,Writer接口

func ClearFilter

func ClearFilter()

ClearFilter 清空过滤器,主要用于自定义处理日志

func CreateLoggerAdapter

func CreateLoggerAdapter(level Level, timeFormat string, format string, out io.Writer) *log.Logger

CreateLoggerAdapter 创建 *log.Logger 的适配器

func Debug

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

Debug print debug log

func Error

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

Error print Error log

func Info

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

Info print Info log

func InitLogger

func InitLogger()

InitLogger 初始化日志

func Printf

func Printf(logLevel Level, codeLevel int, format string, v ...interface{}) string

Printf logger Printer

func SetDefaultLevel

func SetDefaultLevel(path string, level Level)

SetDefaultLevel 设置对应路径下默认的日志级别,可动态调整日志级别

func Trace

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

Trace print trace log

func WaitToClose

func WaitToClose()

WaitToClose called then the application end

func Warn

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

Warn print Warn log

Types

type Appender

type Appender struct {
	Level        string `yaml:"level"`         //日志级别
	PackagePath  string `yaml:"package_path"`  //日志路径
	Adapter      string `yaml:"adapter"`       //适配器,console,file两种
	Rotate       int    `yaml:"rotate"`        //日志切割个数
	RotatePolicy string `yaml:"rotate_policy"` //切割策略,time or size or  default
	RotateScope  int64  `yaml:"rotate_scope"`  //切割范围:如果按时间切割则表示的n分钟,如果是size这表示的是文件大小MB
	LogPath      string `yaml:"log_path"`      //如果适配器使用的file则用来指定文件路径
	Timeformat   string `yaml:"timeformat"`    //指定时间输出格式.默认:"2006-01-02 15:04:05"
	Format       string `yaml:"format"`        //日志格式
}

Appender 日志处理机

type Config

type Config struct {
	//配置上下文
	Context string `yaml:"context"`
	//指定的日志处理机
	Appenders []*Appender `yaml:"appenders"`
}

Config 日志配置

type FileLogConfig

type FileLogConfig struct {
	Path         string //匹配路径
	Timeformat   string //时间格式
	Format       string
	Rotate       int                 //rotate备份个数
	StorePath    string              //日志存放路径,如:/log/test/log.log
	RotatePolicy FileLogRotatePolicy //循环策略
	Level        Level               //日志级别
	// contains filtered or unexported fields
}

FileLogConfig 文件日志配置

type FileLogRotatePolicy

type FileLogRotatePolicy interface {
	//是否需要循环分割
	CanRotate(fileLogWriter *FileLogWriter) bool
	//循环风格的后置处理
	RotateAfter()
}

FileLogRotatePolicy 文件日志循环策略

type FileLogWriter

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

FileLogWriter 文件日志Writer封装

func (*FileLogWriter) Flush

func (writer *FileLogWriter) Flush() error

Flush 落盘操作

func (*FileLogWriter) Rotate

func (writer *FileLogWriter) Rotate()

Rotate 执行循环切割日志操作

func (*FileLogWriter) Write

func (writer *FileLogWriter) Write(p []byte) (nn int, err error)

Write 写入日志

type Flusher

type Flusher interface {
	Flush() error
}

Flusher 日志持久化接口

type Level

type Level byte

Level 日志级别

type Logger

type Logger interface {
	Trace(format string, v ...interface{}) string
	Debug(format string, v ...interface{}) string
	Info(format string, v ...interface{}) string
	Warn(format string, v ...interface{}) string
	Error(format string, v ...interface{}) string
}

Logger 日志接口

func GetLogger

func GetLogger() Logger

GetLogger 获取一个日志对象,主要用于和第三方包做适配

type SizeRotatePolicy

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

SizeRotatePolicy 按文件大小的循环日志的策略

func NewSizeRotatePolicy

func NewSizeRotatePolicy(maxBytes int64) *SizeRotatePolicy

NewSizeRotatePolicy 创建一个按大小循环的策略

func (*SizeRotatePolicy) CanRotate

func (policy *SizeRotatePolicy) CanRotate(fileLogWriter *FileLogWriter) bool

CanRotate 判断文件是否到最大限制大小

func (*SizeRotatePolicy) RotateAfter

func (*SizeRotatePolicy) RotateAfter()

RotateAfter 在循环截断后的处理,未实现

type TimeRotatePolicy

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

TimeRotatePolicy 按时间循环日志的策略

func NewTimeRotatePolicy

func NewTimeRotatePolicy(delay time.Duration) *TimeRotatePolicy

NewTimeRotatePolicy 创建信的按时间循环日志的策略

func (*TimeRotatePolicy) CanRotate

func (policy *TimeRotatePolicy) CanRotate(fileLogWriter *FileLogWriter) bool

CanRotate 判断文件是否能 Rotate

func (*TimeRotatePolicy) RotateAfter

func (policy *TimeRotatePolicy) RotateAfter()

RotateAfter 在循环截断后的处理

Jump to

Keyboard shortcuts

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