We always want to store the log recorder in file to analysis the program after running when developing.

nxlog4go includes an io.Writer which is similar to go log. So we can use io.MultiWriter.

        Building a new rotate file writer

        Setting output as MultiWriter

        Write log to MultiWriter
+-----------------------------------------
| Format by pattern layout
| Write to console
| Write to file
| +---------------------------------------
| | Rotate file if over max size or daily
| | Real write
| +---------------------------------------
+----------------------------------------- Close rotate file writer

The writer costs more CPU usage. Especially, it is expensive when using the caller function to get source's file name and line number as default. You may disable it by:

log.SetCaller(false)

Building a new rotate file writer

rfw := l4g.NewRotateFileWriter("_rfw.log").SetMaxSize(1024 * 5).SetMaxBackup(10)

Setting maxium file size as 5M, and keeping only 10 files.

Or:

rfw := l4g.NewRotateFileWriter("_rfw.log").SetDaily(true)

Rotating file every day at midnight.

Setting output as MultiWriter

Logging message to both term console and rotate file writer.

ww := io.MultiWriter(os.Stderr, rfw)
// Get a new logger instance
log := l4g.New(l4g.FINEST).SetOutput(ww).SetCaller(false)
log.SetPattern("[%D %T] [%L] (%s) %M\n")

Logging

log.Finest("Everything is created now (notice that I will not be printing to the file)")
log.Info("%d. The time is now: %s", j, time.Now().Format("15:04:05 MST 2006/01/02"))
log.Critical("Time to close out!")

CLOSING rotate file writer

Do not forget.

Call Close() function at the end of main program.

It is necessary to guarantee that all log messages are written.

rfw.Close()

Example

See also:

rotateWriter.go

nxlog4go 按天或按文件大小分割日志的更多相关文章

  1. Log4j配置按照文件大小和日期分割日志文件

    目录 Log4j 下载地址 文件大小分割日志文件 以日期分割每天产生一个日志文件 自定义信息输出到日志文件 Log4j 下载地址 Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控 ...

  2. 实现golang io.Writer支持按照天为单位分割日志

    golang中的日志不支持按照天分割,很多开源的日志包都是只支持按照文件大小分割日志,不太利于生产环境中的使用.因此我实现了timewriter,支持: 实现按照天为单位分割日志,可以完美支持gola ...

  3. Python 日志处理(一) 按Nginx log_format 分割日志记录

    要求:不使用正则 根据nginx 默认的日志记录格式,分割日志记录. log_format main '$remote_addr - $remote_user [$time_local] " ...

  4. [Java][log4j]支持同一时候按日期和文件大小切割日志

    依据DailyRollingFileAppender和RollingFileAppender改编,支持按日期和文件大小切割日志.  源文件: package com.bao.logging; impo ...

  5. nginx分割日志

    实现nginx日志按照时间分割存储 backups_log.sh #设置log日志的存储地址 LOG_PATH=/home/soft/nginx/logs #设置历史日志的存储地址 HISTORY_L ...

  6. nginx按日分割日志

    #!/bin/bash #按日切割nginx日志并压缩,加入crontab每天0:00切割 #作者:fafu_li #时间: source /etc/profile #加载系统环境变量 source ...

  7. 使用cronolog按日期分割日志

    cronologcronolog是一个简单的过滤程序从标准输入读取日志文件条目,每个条目写入到输出文件指定一个文件名模板和当前的日期和时间.当扩大的文件名更改,关闭当前文件,并打开一个新的. cron ...

  8. log4net 既要按日期分割日志文件,又要按文件大小分割。

    <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender"> ...

  9. apache 访问日志access_log 配置和解析 rotatelogs分割日志

    一.解析访问日志        apache 的访问日志记载着大量的信息,学会高效快捷的读出其中关键信息对我们的工作有极大帮助.       如果Apache的安装方式是默认安装,服务器一运行就会有两 ...

随机推荐

  1. requests关于Exceeded 30 redirects问题得出的结论

    昨天一个朋友在爬网页时出现的一个问题,以及后续我对这个问题进行了简单的测试. 先说出现的问题的简单描述. 首先是使用urllib请求网页: #urllib.request发起的请求 import ur ...

  2. JQ滚动特效

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  3. node.js进阶话题

    < h3>notes_控制流 //forloopi.js var fs = require('fs'); var files = ['a.txt', 'b.txt', 'c.txt']; ...

  4. chroot: failed to run command `/bin/bash': No such file or directory

    1 使用chroot命令时报错如下: testupgrade:/ # chroot /sb chroot: cannot change root directory to /sb: No such f ...

  5. JavaWeb项目架构之Redis分布式日志队列

    架构.分布式.日志队列,标题自己都看着唬人,其实就是一个日志收集的功能,只不过中间加了一个Redis做消息队列罢了. 前言 为什么需要消息队列? 当系统中出现"生产"和" ...

  6. PHP实现QQ登录的开发教程

    第三方登录,就是使用大家比较熟悉的比如QQ.微信.微博等第三方软件登录自己的网站,这可以免去注册账号.快速留住用户的目的,免去了相对复杂的注册流程.下边就给大家讲一下怎么使用PHP开发QQ登录的功能. ...

  7. redis数据类型-列表类型

    列表类型 列表类型(list)可以存储一个有序的字符串列表,常用的操作是向列表两端添加元素,或者获得列表的某一个片段. 列表类型内部是使用双向链表(double linked list)实现的,所以向 ...

  8. C# 类型基础(上)

    C#类型都派生自System.Object 祖先的优良传统:Object的公共方法 Equals: 对象的同一性而非相等性 GetHashCode:返回对象的值的哈希码 ToString:默认返回类型 ...

  9. ResultSet详细

    1. ResultSet细节1功能:封锁结果集数据操作:如何获得(取出)结果 package com.sjx.a; import java.sql.Connection; import java.sq ...

  10. 本地工程引入maven工程的配置方式

    一.准备 IDE: IntelliJ IDEA 2016.3.1 maven: 3.5.2 JDK: 1.8 操作系统: Window 7 二.配置 1. maven 3.5 下载地址:http:// ...