1. 程式人生 > >golang語言log4go日誌包使用詳解

golang語言log4go日誌包使用詳解

在go專案的當前資料夾裡生成一個logs資料夾,生成test.log的日誌檔案存放於該路徑下。程式中按照下面程式碼初始化即可在程式中使用log4go新增日誌。

package main
import (
	"os"
	"strings"
	"github.com/alecthomas/log4go"
	)

func initConfigs() {
	log4goInit()
	log4go.LoadConfiguration("log4go.xml")
	log4go.Info("log4go init ok.")
}

func log4goInit() {
	path, _ := os.Getwd()
	path = strings.Replace(path, "\\", "/", -1) + "/logs"
	if !pathExists(path) {
		log4go.Warn("dir: logs/ not found.")
		err := os.MkdirAll(path, 0711)
		if err != nil {
			log4go.Error(err.Error())
		}
	}
}

func pathExists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return false
}

載入配置檔案,在該專案中新建一個log4go.xml檔案,內容如下

<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</tag>  
    <type>file</type>  
    <level>FINEST</level>  
    <property name="filename">logs/test.log</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 enables log rotation, otherwise append -->  
    <property name="maxsize">0M</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 -->  
  </filter>  
  <filter enabled="false">  
    <tag>xmllog</tag>  
    <type>xml</type>  
    <level>TRACE</level>  
    <property name="filename">trace.xml</property>  
    <property name="rotate">true</property> <!-- true enables log rotation, otherwise append -->  
    <property name="maxsize">100M</property> <!-- \d+[KMG]? Suffixes are in terms of 2**10 -->  
    <property name="maxrecords">6K</property> <!-- \d+[KMG]? Suffixes are in terms of thousands -->  
    <property name="daily">false</property> <!-- Automatically rotates when a log message is written after midnight -->  
  </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> 

接下來對配置檔案進行介紹:

<filter enabled="true">  
    <tag>stdout</tag>  
    <type>console</type>  
    <!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->  
    <level>DEBUG</level>  
  </filter> 

這裡是設定控制檯輸出日誌級別為DEBUG級以上(過濾器表示日誌級別,在該級別以下),也就是控制檯會輸出TRACE、INFO、WARNING、ERROR等幾類日誌,可以進行更改配置。

 <filter enabled="true">  
    <tag>file</tag>  
    <type>file</type>  
    <level>INFO</level>  
    <property name="filename">logs/test.log</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 enables log rotation, otherwise append -->  
    <property name="maxsize">0M</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 -->  
  </filter>  

配置日誌檔案寫入logs/test.log,同時配置過濾器日誌級別為INFO,型別為file,也即是WARNING、ERROR等幾類日誌會寫入test.log檔案中。
對於不需要的過濾器可以設定為為filter enabled=“false”

 <filter enabled="false">  
    <tag>xmllog</tag>  
    <type>xml</type>  
    <level>TRACE</level>  
    <property name="filename">trace.xml</property>  
    <property name="rotate">true</property> <!-- true enables log rotation, otherwise append -->  
    <property name="maxsize">100M</property> <!-- \d+[KMG]? Suffixes are in terms of 2**10 -->  
    <property name="maxrecords">6K</property> <!-- \d+[KMG]? Suffixes are in terms of thousands -->  
    <property name="daily">false</property> <!-- Automatically rotates when a log message is written after midnight -->  
  </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> 

如有不對歡迎指正,相互學習,共同進步。