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. Java Reflection(getXXX和getDeclaredXXX)

    package com.sunchao.reflection; public class Person { private int age ; private String name; public ...

  2. 用jquery怎么删除<table>的一行

    摘录网址:用jquery怎么删除<table>的一行 思路:获取<table>的一行,然后使用 remove() 方法删除之.实例演示如下: 1.HTML结构 <tabl ...

  3. whoops php调试库

    Whoops 适用于PHP环境的错误捕获与调试PHP库: whoops非常容易使用,它提供stackbased错误捕获及超美观的错误查看 测试效果如下 安装步骤(composer安装) 安装完成之后在 ...

  4. Python---多线程(threading)

    1. 概述 threading提供线程相关操作,python当前版本的多线程库没有实现优先级.线程组,线程也不能被停止.暂停.恢复和中断 threading提供的类: Thread,Lock,Rloc ...

  5. Linkin大话Java和internet概念

    整理电脑,无意中翻到不知道哪里来的文章,觉得里面写的很好,仔细看过一遍后,整理了下贴了出来,其中的好多概念我觉得讲的很透彻. 既然Java不过另一种类型的程序设计语言,为什么还有这么多的人认为它是计算 ...

  6. struts2 action 页面与action参数的传递的三种方式

    第一种: 初始页面: <form action="LoginAction.action" method="post"> 用户名:<input ...

  7. 简述 Hibernate 和 JDBC 的区别、优缺点

    1.hibernate是一个开源的.采用面向对象的思想实现ORM映射框架,它对jdbc进行了一层封装,对于数据库的连接.关闭.数据的持久化(增删改查).事务的管理都进行了封装,使得程序开发的时候可以用 ...

  8. PAT basic level 1001-1019 解题笔记

    1002 写出这个数 采用字符串输入数据,再对每位减去字符‘0’,得到该位相应的整数 int len=s.length();//字符串的长度 ; ;i<len;i++)//每位减去‘0’,逐位相 ...

  9. JAVA设计模式之---工厂模式

    1.引言 工厂模式可以分为类: 1)简单工厂模式(Simple Factory) 2)工厂方法模式(Factory Method) 3)抽象工厂模式(Abstract Factory)  这种模式从上 ...

  10. cmake编译安装mysql 5.6.12

    cmake安装mysql 5.6.12 从mysql 5.5 开始就要用cmake编译安装 下载mysql 下载地址:http://pan.baidu.com/s/1o68xxqE 一.安装mysql ...