This is a creation in Article, where the information may have evolved or changed.
Log4go usage of the log framework in the Go language
Package Mainimport (l4g "Github.com/alecthomas/log4go") func main () {l4g. AddFilter ("stdout", l4g. DEBUG, L4G. Newconsolelogwriter ()) //Output to console, level debugl4g.addfilter ("file", l4g. DEBUG, L4G. Newfilelogwriter ("Test.log", false))//output to file, level is debug, file name is Test.log, append the original file//l4g each time. Loadconfiguration ("Log.xml")//Use load Config file, similar to Java log4j.propertitesl4g.Debug ("The Time is now:%s--%s", "213", "sad") Defer L4G. Close ()//NOTE: If you are not running the program, please add this sentence, otherwise the main thread end, will not output and log to the log file}
This will generate a Test.log log file in the current folder of the Go project.
If it is used to load the configuration file, then create a new log4go.xml content below, roughly can understand not much to say.
<logging> <filter enabled= "true" > <tag>stdout</tag> <type>console</type> < ;! --Level is (:? Finest| fine| Debug| trace|info| Warning| ERROR)-<level>DEBUG</level> </filter> <filter enabled= "true" > <tag>file</t ag> <type>file</type> <level>FINEST</level> <property name= "filename" >test.log&l T;/property> <!--%t-time (15:04:05 MST)%t-time (15:04)%d-date (2006/01/02)%d- Date (01/02/06)%l-level (Fnst, FINE, DEBG, TRAC, WARN, Eror, crit)%s-source%m-message It ignores unknown format strings (and removes them) Recommended: "[%d%T] [%l] (%s)%M"-<property name = "format" >[%d%T] [%l] (%s)%m</property> <property name= "Rotate" >false</property> <!--true en Ables log rotation, otherwise append-to <property name= "MaxSize" >0m</propertY> <!--\D+[KMG]? Suffixes is in terms of 2**10--and <property name= "Maxlines" >0K</property> <!--\D+[KMG]? Suffixes is in terms of thousands-<property name= "daily" >true</property> <!--automatically rota TES When a log message was written after midnight-</filter> <filter enabled= "true" > <TAG>XMLL og</tag> <type>xml</type> <level>TRACE</level> <property name= "filename" >trac e.xml</property> <property name= "Rotate" >true</property> <!--true enables log rotation, Otherwis E Append--<property name= "MaxSize" >100M</property> <!--\D+[KMG]? Suffixes is in terms of 2**10--and <property name= "MaxRecords" >6K</property> <!--\D+[KMG]? Suffixes is in terms of thousands-<property name= "daily" >false</property> <!--automatically rot Ates when a log message was written after midnight--&Gt </filter> <filter enabled= "false" ><!--Enabled=false means this logger won ' t actually be created- <tag>donotopen</tag> <type>socket</type> <level>FINEST</level> <property Name= "Endpoint" >192.168.0.73:12124</property> <!--recommend UDP Broadcast---<property name= " Protocol ">udp</property> <!--TCP or UDP----</filter></logging>