1. 程式人生 > >Go -- log4go日誌

Go -- log4go日誌

全部 導入 ren tle ack stat body utm app

折騰:

【已解決】go語言中實現log信息同時輸出到文件和控制臺(命令行)

期間,已經通過io的MultiWriter搞定了同時輸出信息到文件和console,但是不支持level。

所以,再去試試這個log4go。

github.com/keepeye/log4go

【折騰過程】

1.大概看了看,貌似功能很強大。

有點類似於之前的C#的NLog,為C#的log功能擴展;

此處log4go,是針對go語言的log功能的擴展。

2.此log4go,是go語言的第三方的包,所以,先要去搞懂:

【記錄】go語言中安裝第三方包package(庫):log4go

3.安裝完畢後,再去參考官網的:

http://code.google.com/p/log4go/wiki/GettingStarted

去看看如何使用。

4.先去導入:

?
1 2 //import l4g "log4go.googlecode.com/hg" import l4g "code.google.com/p/log4go"

結果是:

?
1 2 3 E:\Dev_Root\go\src\EmulateLoginBaidu>go run EmulateLoginBaidu.go # command-line-arguments
.\EmulateLoginBaidu.go:34: imported and not used: "code.google.com/p/log4go"

這說明:

正確導入了(但只是暫時還沒去使用而已)

5.接著去使用。

用如下代碼:

?
1 2 3 4 5 6 7 8 9 10 11 //do some init for crifanLib func initCrifanLib(){ l4g.Info("init for crifanLib") //gLogger.Println("init for crifanLib");
gCurCookies = nil return } //init for logger func initLogger(){ l4g.Debug("in initLogger")

效果是:

技術分享

6.看到其解釋了:

上述用的,是默認的,已經實現了的,對於console的log

默認配置為:DEBUG的level:debug及以上的level都顯示。

所有的level,依次是:Finest, Fine, Debug, Trace, Info, Warning, Error, Critical

而此處,我想要實現:

(1)重新更改此console的level為Info

(2)添加此處的log文件作為輸出,且設置level所有,即最低一級的Finest

7.先去添加log文件:

但是想要去看對應的文檔的,結果原先作者的說明:

先去:

?
1 godoc -http=:6060

(我此處實際上本身已經打開godoc的server了)

再去打開地址:

http://localhost:6060/pkg/log4go.googlecode.com/svn/stable/

結果肯定不對的。

後來經過自己的分析,去訪問:

http://localhost:6060/pkg/code.google.com/p/log4go/

就可以了。

8.去看看對應的:

http://localhost:6060/pkg/code.google.com/p/log4go/#AddFilter

內容是:

func AddFilter

func AddFilter(name string, lvl level, writer LogWriter)

Wrapper for (*Logger).AddFilter

去試試代碼。

結果基本實現了所要的效果:

技術分享

技術分享

即:

對於代碼中debug信息,只在文件中顯示

對於info及以上的信息,console和文件,都顯示。

實現了level的自定義。

9.目前,唯一一個小問題,不是我喜歡的:

對於,我經常調試程序,

希望每次重新運行時,對於log文件,都自動清空。

而不要對於log文件是APPEND模式:

每次的輸出,都添加到後面。

而不清空log文件。

10.所以,再去想辦法,新建log時,判斷是否已經存在文件,如果存在去刪除掉:

【已解決】go語言中判斷文件是否存在如果有已存在則刪除

【總結】

最終,基本實現了全部的效果:

1.每次運行程序,可以OVERWRITE舊的log文件,而不是APPEND

2.可以同時輸出內容到log文件和console

3.且console和log文件的輸出level都可以配置:

此處要的是:

INFO及以上的,都可以在console中顯示;

全部信息(包括debug),都可以在log文件中顯示。

全部代碼如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 /* * [File] * EmulateLoginBaidu.go * * [Function] * 【記錄】用go語言實現模擬登陸百度 * http://www.crifan.com/emulate_login_baidu_using_go_language/ * * [Version] * 2013-09-19 * * [Contact] * http://www.crifan.com/about/me/ */ package main import ( //"fmt" //"builtin" //"log" "os" "runtime" "path" "strings" //"io" "time" "io/ioutil" "net/http" //"net/http/cookiejar" //"sync" //"net/url" ) //import l4g "log4go.googlecode.com/hg" //import l4g "code.google.com/p/log4go" import "
github.com/keepeye/log4go
" /*************************************************************************************************** Global Variables ***************************************************************************************************/ var gCurCookies []*http.Cookie; //var gLogger *log.Logger; var gLogger log4go.Logger; /*************************************************************************************************** Functions ***************************************************************************************************/ //do init before all others func initAll(){ gCurCookies = nil gLogger = nil initLogger() initCrifanLib() } //de-init for all func deinitAll(){ gCurCookies = nil if(nil == gLogger) { gLogger.Close(); gLogger = nil } } //do some init for crifanLib func initCrifanLib(){ gLogger.Debug("init for crifanLib") gCurCookies = nil return } //init for logger func initLogger(){ var filenameOnly string filenameOnly = GetCurFilename() var logFilename string = filenameOnly + ".log"; //gLogger = log4go.NewLogger() gLogger = make(log4go.Logger) //for console //gLogger.AddFilter("stdout", log4go.INFO, log4go.NewConsoleLogWriter()) gLogger.AddFilter("stdout", log4go.INFO, log4go.NewConsoleLogWriter()) //for log file if _, err := os.Stat(logFilename); err == nil { //fmt.Printf("found old log file %s, now remove it\n", logFilename) os.Remove(logFilename) } //gLogger.AddFilter("logfile", log4go.FINEST, log4go.NewFileLogWriter(logFilename, true)) gLogger.AddFilter("logfile", log4go.FINEST, log4go.NewFileLogWriter(logFilename, false)) gLogger.Info("Current time is : %s", time.Now().Format("15:04:05 MST 2006/01/02")) return } // GetCurFilename // Get current file name, without suffix func GetCurFilename() string { _, fulleFilename, _, _ := runtime.Caller(0) //fmt.Println(fulleFilename) var filenameWithSuffix string filenameWithSuffix = path.Base(fulleFilename) //fmt.Println("filenameWithSuffix=", filenameWithSuffix) var fileSuffix string fileSuffix = path.Ext(filenameWithSuffix) //fmt.Println("fileSuffix=", fileSuffix) var filenameOnly string filenameOnly = strings.TrimSuffix(filenameWithSuffix, fileSuffix) //fmt.Println("filenameOnly=", filenameOnly) return filenameOnly } //get url response html func GetUrlRespHtml(url string) string{ gLogger.Debug("GetUrlRespHtml, url=%s", url) var respHtml string = ""; resp, err := http.Get(url) if err != nil { gLogger.Warn("http get url=%s response errror=%s\n", url, err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) //gLogger.Debug("body=%s\n", body) gCurCookies = resp.Cookies() respHtml = string(body) return respHtml } func printCurCookies() { var cookieNum int = len(gCurCookies); gLogger.Info("cookieNum=%d", cookieNum) for i := 0; i < cookieNum; i++ { var curCk *http.Cookie = gCurCookies[i]; //gLogger.Info("curCk.Raw=%s", curCk.Raw) gLogger.Info("------ Cookie [%d]------", i) gLogger.Info("Name\t=%s", curCk.Name) gLogger.Info("Value\t=%s", curCk.Value) gLogger.Info("Path\t=%s", curCk.Path) gLogger.Info("Domain\t=%s", curCk.Domain) gLogger.Info("Expires\t=%s", curCk.Expires) gLogger.Info("RawExpires=%s", curCk.RawExpires) gLogger.Info("MaxAge\t=%d", curCk.MaxAge) gLogger.Info("Secure\t=%t", curCk.Secure) gLogger.Info("HttpOnly=%t", curCk.HttpOnly) gLogger.Info("Raw\t=%s", curCk.Raw) gLogger.Info("Unparsed=%s", curCk.Unparsed) } } func main() { initAll() gLogger.Info("this is EmulateLoginBaidu.go") var baiduMainUrl string baiduMainUrl = "http://www.baidu.com/"; //baiduMainUrl := "http://www.baidu.com/"; //var baiduMainUrl string = "http://www.baidu.com/"; gLogger.Info("baiduMainUrl=%s", baiduMainUrl) respHtml := GetUrlRespHtml(baiduMainUrl) gLogger.Debug("respHtml=%s", respHtml) printCurCookies() deinitAll() }

效果是:

console中輸出的內容:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 E:\Dev_Root\go\src\EmulateLoginBaidu>go run EmulateLoginBaidu.go [09/20/13 18:39:40] [INFO] Current time is : 18:39:40 +0800 2013/09/20 [09/20/13 18:39:40] [INFO] this is EmulateLoginBaidu.go [09/20/13 18:39:40] [INFO] baiduMainUrl=http://www.baidu.com/ [09/20/13 18:39:40] [INFO] cookieNum=3 [09/20/13 18:39:40] [INFO] ------ Cookie [0]------ [09/20/13 18:39:40] [INFO] Name =BDSVRTM [09/20/13 18:39:40] [INFO] Value =3 [09/20/13 18:39:40] [INFO] Path =/ [09/20/13 18:39:40] [INFO] Domain = [09/20/13 18:39:40] [INFO] Expires =0001-01-01 00:00:00 +0000 UTC [09/20/13 18:39:40] [INFO] RawExpires= [09/20/13 18:39:40] [INFO] MaxAge =0 [09/20/13 18:39:40] [INFO] Secure =false [09/20/13 18:39:40] [INFO] HttpOnly=false [09/20/13 18:39:40] [INFO] Raw =BDSVRTM=3; path=/ [09/20/13 18:39:40] [INFO] Unparsed=[] [09/20/13 18:39:40] [INFO] ------ Cookie [1]------ [09/20/13 18:39:40] [INFO] Name =H_PS_PSSID [09/20/13 18:39:40] [INFO] Value =3415_1431_2975_2981 [09/20/13 18:39:40] [INFO] Path =/ [09/20/13 18:39:40] [INFO] Domain =.baidu.com [09/20/13 18:39:40] [INFO] Expires =0001-01-01 00:00:00 +0000 UTC [09/20/13 18:39:40] [INFO] RawExpires= [09/20/13 18:39:40] [INFO] MaxAge =0 [09/20/13 18:39:40] [INFO] Secure =false [09/20/13 18:39:40] [INFO] HttpOnly=false [09/20/13 18:39:40] [INFO] Raw =H_PS_PSSID=3415_1431_2975_2981; path=/; domain=.baidu.com [09/20/13 18:39:40] [INFO] Unparsed=[] [09/20/13 18:39:40] [INFO] ------ Cookie [2]------ [09/20/13 18:39:40] [INFO] Name =BAIDUID [09/20/13 18:39:40] [INFO] Value =AF99372EE54C9816618EED94475DDD26:FG=1 [09/20/13 18:39:40] [INFO] Path =/ [09/20/13 18:39:40] [INFO] Domain =.baidu.com [09/20/13 18:39:40] [INFO] Expires =0001-01-01 00:00:00 +0000 UTC [09/20/13 18:39:40] [INFO] RawExpires=Fri, 20-Sep-43 10:39:32 GMT E:\Dev_Root\go\src\EmulateLoginBaidu>

log文件中輸出的內容:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [2013/09/20 18:39:40 ] [INFO] (main.initLogger:91) Current time is : 18:39:40 +0800 2013/09/20 [2013/09/20 18:39:40 ] [DEBG] (main.initCrifanLib:68) init for crifanLib [2013/09/20 18:39:40 ] [INFO] (main.main:159) this is EmulateLoginBaidu.go [2013/09/20 18:39:40 ] [INFO] (main.main:165) baiduMainUrl=http://www.baidu.com/ [2013/09/20 18:39:40 ] [DEBG] (main.GetUrlRespHtml:117) GetUrlRespHtml, url=http://www.baidu.com/ [2013/09/20 18:39:40 ] [DEBG] (main.main:167) respHtml=<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><title>百度一下,你就知道</title><style >html,.......... new Date(_t + 10000).toGMTString()})}catch(e){}</script></html><!--0556c44ca22cac33--> [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:137) cookieNum=3 [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:141) ------ Cookie [0]------ [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:142) Name =BDSVRTM [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:143) Value =3 [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:144) Path =/ [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:145) Domain = [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:146) Expires =0001-01-01 00:00:00 +0000 UTC [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:147) RawExpires= [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:148) MaxAge =0 [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:149) Secure =false [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:150) HttpOnly=false [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:151) Raw =BDSVRTM=3; path=/ [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:152) Unparsed=[] [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:141) ------ Cookie [1]------ [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:142) Name =H_PS_PSSID [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:143) Value =3415_1431_2975_2981 [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:144) Path =/ [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:145) Domain =.baidu.com [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:146) Expires =0001-01-01 00:00:00 +0000 UTC [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:147) RawExpires= [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:148) MaxAge =0 [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:149) Secure =false [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:150) HttpOnly=false [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:151) Raw =H_PS_PSSID=3415_1431_2975_2981; path=/; domain=.baidu.com [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:152) Unparsed=[] [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:141) ------ Cookie [2]------ [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:142) Name =BAIDUID [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:143) Value =AF99372EE54C9816618EED94475DDD26:FG=1 [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:144) Path =/ [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:145) Domain =.baidu.com [2013/09/20 18:39:40 ] [INFO] (main.printCurCookies:146) Expires =0001-01-01 00:00:00 +0000 UTC

整體效果還不錯的說。

Go -- log4go日誌