方便易用的全局函數(shù)

大多數(shù)時候,只不過是寫一個簡單的測試程序。例如:

package mainimport (    "log")func main(){
    log.Fatal("Come with fatal,exit with 1 \n")
}

這是Go語言標準log庫的用法。

無須用logger := log.New(...)來產(chǎn)生一個指針。而且可以在程序的任何地方都能使用這個log。

閱讀 log.go 源碼:

...var std = New(os.Stderr, "", LstdFlags)
...// Fatal is equivalent to Print() followed by a call to os.Exit(1).func Fatal(v ...interface{}) {
    std.Output(2, fmt.Sprint(v...))
    os.Exit(1)
}

奧妙就在于 log.go 申請了一個全局變量 std,并封裝了全局函數(shù) log.Fatal。

log4go 的一個簡單例子

這是 log4go 的一個簡單例子:

package mainimport (    "time"
    log "github.com/ccpaging/log4go"
    "github.com/ccpaging/log4go/colorlog")func main() {
    log.AddFilter("stdout", log.DEBUG, colorlog.NewColorLogWriter())
    log.Debug("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))
    log.Info("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))
    log.Warn("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))

    time.Sleep(200 * time.Millisecond)
}

效果如下圖:

是不是很漂亮?運行環(huán)境:windows ConEmu。
colorlog 目前可以支持 Windows、Linux 以及 Cygwin 的 mintty 和 tmux。

github.com/ccpaging/log4go/colorlog,這是一個彩色字符終端的log4go擴展插件。

喧賓奪主了……color term log 的話題以后再聊。

全局變量和全局函數(shù)

所以,log4go也有一個全局變量和一堆全局函數(shù)封裝。詳見:wrapper.go

var (
    Global Logger
)func init() {
    Global = Logger {        "stdout": NewFilter(DEBUG, NewConsoleLogWriter().SetFormat("%T %L %s %M")),
    }
}// Wrapper for (*Logger).AddFilterfunc AddFilter(name string, lvl Level, writer LogWriter) {
    Global.AddFilter(name, lvl, writer)
}

全局變量Global被自動初始化為一個 ConsoleLogWriter。它是黑白兩色的字符終端。

// Add a new LogWriter to the Logger which will only log messages at lvl or// higher.  This function should not be called from multiple goroutines.// Returns the logger for chaining.func (log Logger) AddFilter(name string, lvl Level, writer LogWriter) Logger {    if filt, isExist := log[name]; isExist {
        filt.Close()        delete(log, name)
    }
    log[name] = NewFilter(lvl, writer)    return log
}

加入 ColorLogWriter 時,使用了同樣的關(guān)鍵字 stdout,
所以,原來的黑白終端輸出被關(guān)閉,Global 中只保留了新的彩色終端。

如果 AddFilter 時,換一個關(guān)鍵字,例如 xxxout 會怎樣?我不知道。
系統(tǒng)不會崩潰吧 :(

兼容標準log庫

為了方便習慣于標準log庫的程序員,封裝了與標準庫兼容的函數(shù)。
原來使用標準 log 庫的程序只需換成 log4go 就可以用了?
想得美?!這是一個值得嘗試的設想……

    func Fatal(v ...interface{})    func Fatalf(format string, v ...interface{})    func Fatalln(v ...interface{})    func Output(calldepth int, s string) error
    func Panic(v ...interface{})    func Panicf(format string, v ...interface{})    func Panicln(v ...interface{})    func Print(v ...interface{})    func Printf(format string, v ...interface{})    func Println(v ...interface{})

后續(xù)可能會加入:

    func Prefix() string
    func SetPrefix(prefix string)

這個功能也許在nanomsg的訂閱模型中用到。

異步寫日志的坑

細心的童鞋可能注意到測試程序中的一個特殊語句。

time.Sleep(200 * time.Millisecond)

納尼?延時了200ms。我承認,這個問題令人困惑。如果沒有這個延時,
可能在終端上什么也看不到。后果很嚴重……考試通不過,掛科……慘……

log4go 的每個日志都在一個 go routine 中運行。如果測試程序?qū)懙奶唵瘟耍?br/>go routine 還沒有來得及運行,主程序就退出了。

曾經(jīng)設想在go routine中加 channel……然并沒有什么用……

方便易用的全局函數(shù)

大多數(shù)時候,只不過是寫一個簡單的測試程序。例如:

package mainimport (    "log")func main(){
    log.Fatal("Come with fatal,exit with 1 \n")
}

這是Go語言標準log庫的用法。

無須用logger := log.New(...)來產(chǎn)生一個指針。而且可以在程序的任何地方都能使用這個log。

閱讀 log.go 源碼:

...var std = New(os.Stderr, "", LstdFlags)
...// Fatal is equivalent to Print() followed by a call to os.Exit(1).func Fatal(v ...interface{}) {
    std.Output(2, fmt.Sprint(v...))
    os.Exit(1)
}

奧妙就在于 log.go 申請了一個全局變量 std,并封裝了全局函數(shù) log.Fatal。

log4go 的一個簡單例子

這是 log4go 的一個簡單例子:

package mainimport (    "time"
    log "github.com/ccpaging/log4go"
    "github.com/ccpaging/log4go/colorlog")func main() {
    log.AddFilter("stdout", log.DEBUG, colorlog.NewColorLogWriter())
    log.Debug("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))
    log.Info("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))
    log.Warn("The time is now: %s", time.Now().Format("15:04:05 MST 2006/01/02"))

    time.Sleep(200 * time.Millisecond)
}

效果如下圖:

是不是很漂亮?運行環(huán)境:windows ConEmu。
colorlog 目前可以支持 Windows、Linux 以及 Cygwin 的 mintty 和 tmux。

github.com/ccpaging/log4go/colorlog,這是一個彩色字符終端的log4go擴展插件。

喧賓奪主了……color term log 的話題以后再聊。

全局變量和全局函數(shù)

所以,log4go也有一個全局變量和一堆全局函數(shù)封裝。詳見:wrapper.go

var (
    Global Logger
)func init() {
    Global = Logger {        "stdout": NewFilter(DEBUG, NewConsoleLogWriter().SetFormat("%T %L %s %M")),
    }
}// Wrapper for (*Logger).AddFilterfunc AddFilter(name string, lvl Level, writer LogWriter) {
    Global.AddFilter(name, lvl, writer)
}

全局變量Global被自動初始化為一個 ConsoleLogWriter。它是黑白兩色的字符終端。

// Add a new LogWriter to the Logger which will only log messages at lvl or// higher.  This function should not be called from multiple goroutines.// Returns the logger for chaining.func (log Logger) AddFilter(name string, lvl Level, writer LogWriter) Logger {    if filt, isExist := log[name]; isExist {
        filt.Close()        delete(log, name)
    }
    log[name] = NewFilter(lvl, writer)    return log
}

加入 ColorLogWriter 時,使用了同樣的關(guān)鍵字 stdout,
所以,原來的黑白終端輸出被關(guān)閉,Global 中只保留了新的彩色終端。

如果 AddFilter 時,換一個關(guān)鍵字,例如 xxxout 會怎樣?我不知道。
系統(tǒng)不會崩潰吧 :(

兼容標準log庫

為了方便習慣于標準log庫的程序員,封裝了與標準庫兼容的函數(shù)。
原來使用標準 log 庫的程序只需換成 log4go 就可以用了?
想得美?!這是一個值得嘗試的設想……

    func Fatal(v ...interface{})    func Fatalf(format string, v ...interface{})    func Fatalln(v ...interface{})    func Output(calldepth int, s string) error
    func Panic(v ...interface{})    func Panicf(format string, v ...interface{})    func Panicln(v ...interface{})    func Print(v ...interface{})    func Printf(format string, v ...interface{})    func Println(v ...interface{})

后續(xù)可能會加入:

    func Prefix() string
    func SetPrefix(prefix string)

這個功能也許在nanomsg的訂閱模型中用到。

異步寫日志的坑

細心的童鞋可能注意到測試程序中的一個特殊語句。

time.Sleep(200 * time.Millisecond)

納尼?延時了200ms。我承認,這個問題令人困惑。如果沒有這個延時,
可能在終端上什么也看不到。后果很嚴重……考試通不過,掛科……慘……

log4go 的每個日志都在一個 go routine 中運行。如果測試程序?qū)懙奶唵瘟耍?br/>go routine 還沒有來得及運行,主程序就退出了。

曾經(jīng)設想在go routine中加 channel……然并沒有什么用……

http://www.cnblogs.com/ccpaging/p/7218626.html