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. SerialChart串口示波器的成功调试

    2018-01-1601:29:06 深夜更新一波串口示波器! http://t.cn/RQMA3uq 心得体会 总之将数据输出设置为","分割的形式即可 重点注意事项!!!! m ...

  2. Linux指令--chgrp

    在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group的 ...

  3. POI--HSSFCellStyle类

    通过POI来进行单元格格式的设定 设定格式使用「HSSFCellStyle」类.它有一个构造方法: protected HSSFCellStyle(short index, ExtendedForma ...

  4. GIT工程迁移方法总结

    Git工程迁移方法总结 Git最近准备迁移一下位置,这里采用命令行的方式,做如下操作. 1.git init  初始化git仓库,这个时候发现本地文件夹多了个.git的文件夹. 2.git remot ...

  5. 错误:Invalid action class configuration that references an unknown class named [XXX]的解决

    问题: 用http的方式直接调用类,执行完毕后报错误信息Invalid action class configuration that references an unknown class name ...

  6. 00_HTML入门第一天

    HTML入门 body标记的常见属性:bgcolor 设置背景颜色:text 设置文本颜色:link 设置链接颜色:vlink 设置已经访问了的链接颜色:alink 正在点击的链接颜色: meta是单 ...

  7. 【转】GLONASS全球卫星导航系统

    GLONASS是“GLOBAL NAVIGATION SATELLITE SYSTE(全球卫星导航系统)”的缩写,作用类似于美国的GPS.欧洲的伽利略卫星定位系统.最早开发于苏联时期,后由俄罗斯继续该 ...

  8. Java中的最值

    Double.MAX_VALUE     等于 (2-2^(-52)) * 2^1023 约等于2^1024Double.MIN_VALUE      最小正非零值 2^(-1074)Long.MAX ...

  9. HTTP就是这么简单

    为什么要学HTTP? 我们绝大多数的Web应用都是基于HTTP来进行开发的.我们对Web的操作都是通过HTTP协议来进行传输数据的. 简单来说,HTTP协议就是客户端和服务器交互的一种通迅的格式. H ...

  10. encodeURI()和encodeURIComponent()

    encodeURI() 返回值 URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换. 说明 该方法会替换所有的字符,但不包括以下字符,即使它们具有适当的UTF-8转义序列: 保留 ...