'How to use log4go with configuration file?
I have been trying to use log4go in golang. But I could not find a proper example where log4go configuration properties were used like rotation,maxSize etc to create a logger. Can somebody provide a example? I have referred to many sites.
Solution 1:[1]
log4go is not well documented, I found some documentation in the original repository.
If you can, I'd use a different library like logrus, has a better documentation, examples and is actively developed.
Solution 2:[2]
The easy way is to use the logConfig xml, for example:
<code>
<logging>
<filter enabled="true">
<tag>stdout</tag>
<type>console</type>
<!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->
<level>INFO</level>
</filter>
<filter enabled="true">
<tag>file</tag>
<type>file</type>
<level>INFO</level>
<property name="filename"><log file Path></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">true</property> <!-- true enables log rotation, otherwise append -->
<property name="maxsize">10M</property> <!-- \d+[KMG]? Suffixes are in terms of 2**10 -->
<property name="maxlines">0K</property> <!-- \d+[KMG]? Suffixes are in terms of thousands -->
<property name="daily">true</property> <!-- Automatically rotates when a log message is written after midnight -->
<property name="maxbackup">10</property> <!-- Max backup for logs rotation -->
</filter>
</logging>
Personally, I preferred zarolog : https://github.com/rs/zerolog
Solution 3:[3]
Here is one example which can have two logs:
{
"console": {
"enable": true,
"level": "ERROR"
},
"files": [{
"enable": true,
"level": "DEBUG",
"filename":"./log/sys.log",
"category": "syslog",
"pattern": "[%D %T] [%L] (%S) %M",
"rotate": true,
"maxsize": "5M",
"maxlines": "10K",
"daily": true
},
{
"enable": true,
"level": "INFO",
"filename":"./log/market.log",
"category": "marketlog",
"pattern": "[%D %T] [%L] (%S) %M",
"rotate": false,
"maxsize": "10M",
"maxlines": "20K",
"daily": false
}
]
}
usage in code:
log4go.LOGGER("syslog").Info("...")
log4go.LOGGER("marketlog").Debug("...")
the debug calls on marketlog would not be written in this case because the "INFO" level automatically filters it out.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tinwor |
Solution 2 | Chen Keinan |
Solution 3 | George Y |