zoukankan      html  css  js  c++  java
  • nxlog4go 按天或按文件大小分割日志

    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
    ")
    

    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

  • 相关阅读:
    jQuery的选择器中的通配符[id^='code']
    浏览器调试js
    google浏览器调试js
    【暑假】[实用数据结构]UVAlive 3026 Period
    【暑假】[实用数据结构]UVAlive 3942 Remember the Word
    【暑假】[实用数据结构] AC自动机
    【暑假】[实用数据结构]KMP
    【暑假】[实用数据结构]前缀树 Trie
    【暑假】[实用数据结构]UVa11235 Frequent values
    【暑假】[实用数据结构]UVAlive 4329 Ping pong
  • 原文地址:https://www.cnblogs.com/ccpaging/p/8486344.html
Copyright © 2011-2022 走看看