go语言:log4go

go语言中日志框架log4go用法
[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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内容以下,大致都能看懂就不多说了.

[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值