go语言:log4go

LvanNeo · · 10735 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

go语言中日志框架log4go用法
  1. package main  
  2.   
  3. import (  
  4.     l4g "github.com/alecthomas/log4go"  
  5. )  
  6.   
  7. func main() {  
  8.     l4g.AddFilter("stdout", l4g.DEBUG, l4g.NewConsoleLogWriter())             //输出到控制台,级别为DEBUG  
  9.     l4g.AddFilter("file", l4g.DEBUG, l4g.NewFileLogWriter("test.log", false)) //输出到文件,级别为DEBUG,文件名为test.log,每次追加该原文件  
  10.     //l4g.LoadConfiguration("log.xml")//使用加载配置文件,类似与java的log4j.propertites  
  11.     l4g.Debug("the time is now :%s -- %s", "213", "sad")  
  12.     defer l4g.Close()//注:如果不是一直运行的程序,请加上这句话,否则主线程结束后,也不会输出和log到日志文件  
  13. }  


这样将会在go项目的当前文件夹里生成一个test.log的日志文件.

如果是用加载配置文件的话,则新建一个log4go.xml内容以下,大致都能看懂就不多说了.

  1. <logging>  
  2.   <filter enabled="true">  
  3.     <tag>stdout</tag>  
  4.     <type>console</type>  
  5.     <!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->  
  6.     <level>DEBUG</level>  
  7.   </filter>  
  8.   <filter enabled="true">  
  9.     <tag>file</tag>  
  10.     <type>file</type>  
  11.     <level>FINEST</level>  
  12.     <property name="filename">test.log</property>  
  13.     <!--  
  14.        %T - Time (15:04:05 MST)  
  15.        %t - Time (15:04)  
  16.        %D - Date (2006/01/02)  
  17.        %d - Date (01/02/06)  
  18.        %L - Level (FNST, FINE, DEBG, TRAC, WARN, EROR, CRIT)  
  19.        %S - Source  
  20.        %M - Message  
  21.        It ignores unknown format strings (and removes them)  
  22.        Recommended: "[%D %T] [%L] (%S) %M"  
  23.     -->  
  24.     <property name="format">[%D %T] [%L] (%S) %M</property>  
  25.     <property name="rotate">false</property> <!-- true enables log rotation, otherwise append -->  
  26.     <property name="maxsize">0M</property> <!-- \d+[KMG]? Suffixes are in terms of 2**10 -->  
  27.     <property name="maxlines">0K</property> <!-- \d+[KMG]? Suffixes are in terms of thousands -->  
  28.     <property name="daily">true</property> <!-- Automatically rotates when a log message is written after midnight -->  
  29.   </filter>  
  30.   <filter enabled="true">  
  31.     <tag>xmllog</tag>  
  32.     <type>xml</type>  
  33.     <level>TRACE</level>  
  34.     <property name="filename">trace.xml</property>  
  35.     <property name="rotate">true</property> <!-- true enables log rotation, otherwise append -->  
  36.     <property name="maxsize">100M</property> <!-- \d+[KMG]? Suffixes are in terms of 2**10 -->  
  37.     <property name="maxrecords">6K</property> <!-- \d+[KMG]? Suffixes are in terms of thousands -->  
  38.     <property name="daily">false</property> <!-- Automatically rotates when a log message is written after midnight -->  
  39.   </filter>  
  40.   <filter enabled="false"><!-- enabled=false means this logger won't actually be created -->  
  41.     <tag>donotopen</tag>  
  42.     <type>socket</type>  
  43.     <level>FINEST</level>  
  44.     <property name="endpoint">192.168.0.73:12124</property> <!-- recommend UDP broadcast -->  
  45.     <property name="protocol">udp</property> <!-- tcp or udp -->  
  46.   </filter>  
  47. </logging>  

有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:LvanNeo

查看原文:go语言:log4go

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

10735 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传