package blog4go

import (
"errors"
"fmt"
)

var (
// ErrFilePathNotFound 文件路径找不到
ErrFilePathNotFound = errors.New("File Path must be defined")
// ErrInvalidLevel  非法日志级别
ErrInvalidLevel = errors.New("Invalid level string")
// ErrInvalidRotateType 非法管理类型
ErrInvalidRotateType = errors.New("Invalid log rotate type")
)

// MultiWriter struct defines an instance for multi writers with different message level
//MultiWriter 定义一个多写结构  带着不同的日志级别
type MultiWriter struct {
level LevelType

// file writers  不同级别对应不同的写通道
writers map[LevelType]Writer
        //颜色设置
colored bool
      //连接是否关闭的标志
closed bool

//配置回调函数
hook Hook
//回调函数日志级别设置
hookLevel LevelType
//回调函数 是同步还是异步执行
hookAsync bool

//管理日志频率
timeRotated bool
       //耗时
retentions  int64
       //循环次数
rotateSize  int64
rotateLines int
}

// TimeRotated get timeRotated
func (writer *MultiWriter) TimeRotated() bool {
return writer.timeRotated
}

// SetTimeRotated toggle time base logrotate
func (writer *MultiWriter) SetTimeRotated(timeRotated bool) {
writer.timeRotated = timeRotated
for _, fileWriter := range writer.writers {
fileWriter.SetTimeRotated(timeRotated)
}
}

// Retentions get retentions
func (writer *MultiWriter) Retentions() int64 {
return writer.retentions
}

// SetRetentions set how many logs will keep after logrotate
func (writer *MultiWriter) SetRetentions(retentions int64) {
if retentions < 1 {
return
}

writer.retentions = retentions
for _, fileWriter := range writer.writers {
fileWriter.SetRetentions(retentions)
}
}

// RotateSize get rotateSize
func (writer *MultiWriter) RotateSize() int64 {
return writer.rotateSize
}

// SetRotateSize set size when logroatate
func (writer *MultiWriter) SetRotateSize(rotateSize int64) {
writer.rotateSize = rotateSize
for _, fileWriter := range writer.writers {
fileWriter.SetRotateSize(rotateSize)
}
}

// RotateLines get rotateLines
func (writer *MultiWriter) RotateLines() int {
return writer.rotateLines
}

// SetRotateLines set line number when logrotate
func (writer *MultiWriter) SetRotateLines(rotateLines int) {
writer.rotateLines = rotateLines
for _, fileWriter := range writer.writers {
fileWriter.SetRotateLines(rotateLines)
}
}

// Colored get colored
func (writer *MultiWriter) Colored() bool {
return writer.colored
}

// SetColored set logging color
func (writer *MultiWriter) SetColored(colored bool) {
writer.colored = colored
for _, fileWriter := range writer.writers {
fileWriter.SetColored(colored)
}
}

// SetHook set hook for every logging actions
func (writer *MultiWriter) SetHook(hook Hook) {
writer.hook = hook
}

// SetHookAsync set hook async for base file writer
func (writer *MultiWriter) SetHookAsync(async bool) {
writer.hookAsync = async
}

// SetHookLevel set when hook will be called
func (writer *MultiWriter) SetHookLevel(level LevelType) {
writer.hookLevel = level
}

// SetLevel set logging level threshold
func (writer *MultiWriter) SetLevel(level LevelType) {
writer.level = level
for _, fileWriter := range writer.writers {
fileWriter.SetLevel(level)
}
}

// Level return logging level threshold
func (writer *MultiWriter) Level() LevelType {
return writer.level
}

// Close close file writer
func (writer *MultiWriter) Close() {
for _, fileWriter := range writer.writers {
fileWriter.Close()
}
writer.closed = true
}

func (writer *MultiWriter) write(level LevelType, args ...interface{}) {
defer func() {
// 异步调用log hook
if nil != writer.hook && !(level < writer.hookLevel) {
if writer.hookAsync {
go func(level LevelType, args ...interface{}) {
writer.hook.Fire(level, args...)
}(level, args...)

} else {
writer.hook.Fire(level, args...)
}
}
}()

writer.writers[level].write(level, args...)
}

func (writer *MultiWriter) writef(level LevelType, format string, args ...interface{}) {
defer func() {
// 异步调用log hook
if nil != writer.hook && !(level < writer.hookLevel) {
if writer.hookAsync {
go func(level LevelType, format string, args ...interface{}) {
writer.hook.Fire(level, fmt.Sprintf(format, args...))
}(level, format, args...)

} else {
writer.hook.Fire(level, fmt.Sprintf(format, args...))

}
}
}()

writer.writers[level].writef(level, format, args...)
}

// flush flush logs to disk
func (writer *MultiWriter) flush() {
for _, writer := range writer.writers {
writer.flush()
}
}

// Trace trace
func (writer *MultiWriter) Trace(args ...interface{}) {
_, ok := writer.writers[TRACE]
if !ok || TRACE < writer.level {
return
}

writer.write(TRACE, args...)
}

// Tracef tracef
func (writer *MultiWriter) Tracef(format string, args ...interface{}) {
_, ok := writer.writers[TRACE]
if !ok || TRACE < writer.level {
return
}

writer.writef(TRACE, format, args...)
}

// Debug debug
func (writer *MultiWriter) Debug(args ...interface{}) {
_, ok := writer.writers[DEBUG]
if !ok || DEBUG < writer.level {
return
}

writer.write(DEBUG, args...)
}

// Debugf debugf
func (writer *MultiWriter) Debugf(format string, args ...interface{}) {
_, ok := writer.writers[DEBUG]
if !ok || DEBUG < writer.level {
return
}

writer.writef(DEBUG, format, args...)
}

// Info info
func (writer *MultiWriter) Info(args ...interface{}) {
_, ok := writer.writers[INFO]
if !ok || INFO < writer.level {
return
}

writer.write(INFO, args...)
}

// Infof infof
func (writer *MultiWriter) Infof(format string, args ...interface{}) {
_, ok := writer.writers[INFO]
if !ok || INFO < writer.level {
return
}

writer.writef(INFO, format, args...)
}

// Warn warn
func (writer *MultiWriter) Warn(args ...interface{}) {
_, ok := writer.writers[WARNING]
if !ok || WARNING < writer.level {
return
}

writer.write(WARNING, args...)
}

// Warnf warnf
func (writer *MultiWriter) Warnf(format string, args ...interface{}) {
_, ok := writer.writers[WARNING]
if !ok || WARNING < writer.level {
return
}

writer.writef(WARNING, format, args...)
}

// Error error
func (writer *MultiWriter) Error(args ...interface{}) {
_, ok := writer.writers[ERROR]
if !ok || ERROR < writer.level {
return
}

writer.write(ERROR, args...)
}

// Errorf error
func (writer *MultiWriter) Errorf(format string, args ...interface{}) {
_, ok := writer.writers[ERROR]
if !ok || ERROR < writer.level {
return
}

writer.writef(ERROR, format, args...)
}

// Critical critical
func (writer *MultiWriter) Critical(args ...interface{}) {
_, ok := writer.writers[CRITICAL]
if !ok || CRITICAL < writer.level {
return
}

writer.write(CRITICAL, args...)
}

// Criticalf criticalf
func (writer *MultiWriter) Criticalf(format string, args ...interface{}) {
_, ok := writer.writers[CRITICAL]
if !ok || CRITICAL < writer.level {
return
}

writer.writef(CRITICAL, format, args...)
}

multiWriter.go的更多相关文章

  1. tee MultiWriter creates a writer that duplicates its writes to all the // provided writers, similar to the Unix tee(1) command.

    https://zh.wikipedia.org/wiki/Tee 在计算机科学中,tee是一个常见的指令,它能够将某个指令的标准输出,导向.存入某个档案中.许多不同的命令行界面(Shell)都提供这 ...

  2. Spike Notes on Lock based Concurrency Concepts

    Motivation 承并发编程笔记Outline,这篇文章专注于记录学习基于锁的并发概念的过程中出现的一些知识点,为并发高层抽象做必要的准备. 尽管存在Doug Lee开山之作Concurrent ...

  3. Golang学习 - io 包

    ------------------------------------------------------------ 先说一下接口,Go 语言中的接口很简单,在 Go 语言的 io 包中有这样一个 ...

  4. c++ freelockquque

    http://www.boost.org/doc/libs/1_56_0/doc/html/boost/lockfree/queue.html Class template queue boost:: ...

  5. boost 无锁队列

    一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...

  6. log4go的输出优化

    又看了一些golang的日志包和相关的文章,仔细阅读了go 1.9.2系统提供的log和go-log,产生了对log4go的日志输出进行优化的想法. 结构化与multiwriter log使用mult ...

  7. nxlog4go 简介 - 基于log4go的下一代go语言日志系统

    nxlog4go的项目网址: https://github.com/ccpaging/nxlog4go 项目历史 ccpaging's log4go forked from https://githu ...

  8. nxlog4go 按天或按文件大小分割日志

    Building a new rotate file writer: rfw := l4g.NewRotateFileWriter("_rfw.log").SetMaxSize(1 ...

  9. Go语言标准库_输入/输出

    Go语言标准库_输入/输出 转载节选自<Go语言标准库> Reader 接口 type Reader interface { Read(p []byte) (n int, err erro ...

随机推荐

  1. Java Socket:Java-NIO-Selector

    Selector 的出现,大大改善了多个 Java Socket的效率.在没有NIO的时候,轮询多个socket是通过read阻塞来完成,即使是非阻塞模式,我们在轮询socket是否就绪的时候依然需要 ...

  2. 一个简单的ruby生成器例子(用连续体Continuation实现)

    ruby中有很多经典的驱动器结构,比如枚举器和生成器等.这次简单介绍下生成器的概念.生成器是按照功能要求,一次产生一个对象,或称之为生成一个对象的方法.ruby中的连续体正好可以用来完成生成器的功能. ...

  3. SqlServer中的事务隔离级别、锁机制

    事务 作用:用来执行一连串的动作,并且保证所有动作要么都执行.要么都不执行. 属性:原子行.一致性.隔离性.持久性 锁 作用:SqlServer使用锁来实施事务隔离属性. 阻塞 定义:如果一个事务持有 ...

  4. 【抽象那些事】不完整的抽象&多方面抽象&未用的抽象&重复的抽象

    不完整的抽象 抽象未支持所有互补或相关的方法时,将导致这种坏味. 为什么要有完整的抽象? 一种重要的抽象实现手法是创建内聚而完整的抽象.抽象未支持相关的方法时,可能会影响抽象的内聚性和完整性.如果抽象 ...

  5. Day7 子类调用父类的方法supper 绑定方法与非绑定方法

    supper:可以利用supper来重用父类的方法,可以不用指名道姓的调用了. class OldboyPeople: school = 'oldboy' def __init__(self,name ...

  6. 在Django中使用Neo4j

    重要的先说在前面吧,最后的选型结构是安装了最新的neo4j版本3.0.3,使用了neo4j-rest-client客户端库.主要原因是更适用于django的neomodel库目前只支持neo4j2.2 ...

  7. 【数据可视化之Flask】快速设计和部署Flask网站

    Flask是Python应用于WEB开发的第三方开源框架,以设计简单高效著称.我也尝试过Django,相对于Flask显得更加全面同样也更加笨重,并且我也不需要它的后台管理功能,因此选择了Flask作 ...

  8. 洛谷 P1691 解题报告

    P1691 有重复元素的排列问题 题目描述 设\(R={r_1,r_2,--,r_n}\)是要进行排列的\(n\)个元素.其中元素\(r_1,r_2,--,r_n\)可能相同.使设计一个算法,列出\( ...

  9. angular访问后台服务及监控会话超时的封装

    angular访问后台服务及监控会话超时的封装 angular本身自带访问组件http和httpclient,组件本身都是异步模式访问.本文只列举了对http组件的封装同时也一同处理会话超时监控. 获 ...

  10. jquery-layer弹框在火狐浏览器中弹框不显示的问题

    在使用layer控件设置弹框时, 谷歌浏览器中能正常弹出, 显示在页面中央位置. 而在火狐浏览器中, 弹框只显示标题, 并且弹框内容不显示. 在火狐浏览器中弹框的效果如下图红色方框中的弹出框所示, 但 ...