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. Android开发 PopupWindow弹窗调用第三方地图(百度,高德)实现导航功能

    博客描述:后台返回地点的经纬度在地图上进行描点,点击导航弹出PopupWindow进行选择地图操作,如果手机中没有安装地图,提示没有,否则传值调起地图进行导航操作 看一下实现的效果,没图说再多都白搭 ...

  2. angularjs作用域之transclude

    transclude是一个可选的参数.如果设置了,其值必须为true,它的默认值是false.嵌入有时被认为是一个高级主题,但某些情况下它与我们刚刚学习过的作用域之间会有非常好的配合.使用嵌入也会很好 ...

  3. Tihinkphp3.2整合最新版阿里大鱼进行短信验证码发送

    阿里大鱼最新下载地址:阿里大鱼SDK下载 或者从官网进行下载:阿里大鱼SDK官网下载 下载完成后,将压缩包内的api_sdk文件夹放到ThinkPHP\Library\Vendor目录下,修改文件名为 ...

  4. VueJs(8)---组件(注册组件)

    组件(注册组件) 一.介绍 组件系统是Vue.js其中一个重要的概念,它提供了一种抽象,让我们可以使用独立可复用的小组件来构建大型应用,任意类型的应用界面都可以抽象为一个组件树 那么什么是组件呢? 组 ...

  5. [更新]单线程的JS引擎与 Event Loop

    先来思考一个问题,JS 是单线程的还是多线程的?如果是单线程,为什么JavaScript能让AJAX异步发送和回调请求,还有setTimeout也看起来像是多线程的?还有non-blocking IO ...

  6. 2018 CISCN reverse wp

    2018 CISCN reverse wp 这题比赛的时候没做出来,主要是心态崩了看不下去..赛后看了下网上的wp发现不难,是自己想复杂了.这里将我的思路和exp放出来,希望大家一起交流学习. mai ...

  7. Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】

    Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版]  发表于 2018-04-24 |  随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请 ...

  8. Django入门一之安装及项目创建

    1. 习惯性的创建虚拟环境 # 由于我安装也安装了pyhton3所以在前面要加python2 -m F:\Python Script\MyVirtualenv>python2 -m virtua ...

  9. rsync 密钥文件错误问题总结

    rsync 可以使用 --password-file 选项指定密钥文件,密钥文件中简单存放 rsync 密码:在第一次使用密钥文件的时候经常遇到文件权限相关问题:这里总结一下,我遇到的问题. 问题描述 ...

  10. 重写equals时,遵守的规定

      0 正确的equals方法 public class MyClass { // 主要属性1 private int primaryAttr1; // 主要属性2 private int prima ...