新聞中心
完整的方法列表可參考接口文檔:https://pkg.GO.dev/github.com/gogf/gf/v2/os/glog

涼州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
?glog?模塊支持非常簡(jiǎn)便的鏈?zhǔn)讲僮鞣绞剑饕逆準(zhǔn)讲僮鞣椒ㄈ缦拢?/p>
// 重定向日志輸出接口
func To(writer io.Writer) *Logger
// 日志內(nèi)容輸出到目錄
func Path(path string) *Logger
// 設(shè)置日志文件分類
func Cat(category string) *Logger
// 設(shè)置日志文件格式
func File(file string) *Logger
// 設(shè)置日志打印級(jí)別
func Level(level int) *Logger
// 設(shè)置日志打印級(jí)別(字符串)
func LevelStr(levelStr string) *Logger
// 設(shè)置文件回溯值
func Skip(skip int) *Logger
// 是否開啟trace打印
func Stack(enabled bool) *Logger
// 開啟trace打印并設(shè)定過濾trace的字符串
func StackWithFilter(filter string) *Logger
// 是否開啟終端輸出
func Stdout(enabled...bool) *Logger
// 是否輸出日志頭信息
func Header(enabled...bool) *Logger
// 輸出日志調(diào)用行號(hào)信息
func Line(long...bool) *Logger
// 異步輸出日志
func Async(enabled...bool) *Logger示例1, 基本使用
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gfile"
)
func main() {
ctx := context.TODO()
path := "/tmp/glog-cat"
g.Log().SetPath(path)
g.Log().Stdout(false).Cat("cat1").Cat("cat2").Print(ctx, "test")
list, err := gfile.ScanDir(path, "*", true)
g.Dump(err)
g.Dump(list)
}執(zhí)行后,輸出結(jié)果為:
null
[
"/tmp/glog-cat/cat1",
"/tmp/glog-cat/cat1/cat2",
"/tmp/glog-cat/cat1/cat2/2018-10-10.log",
]示例2, 打印調(diào)用行號(hào)
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
ctx := context.TODO()
g.Log().Line().Print(ctx, "this is the short file name with its line number")
g.Log().Line(true).Print(ctx, "lone file name with line number")
}執(zhí)行后,終端輸出結(jié)果為:
2019-05-23 09:22:58.141 glog_line.go:8: this is the short file name with its line number
2019-05-23 09:22:58.142 /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/os/glog/glog_line.go:9: lone file name with line number示例3, 文件回溯Skip
有時(shí)我們通過一些模塊封裝了?glog?模塊來打印日志,例如封裝了一個(gè)?logger?包通過?logger.Print?來打印日志,這個(gè)時(shí)候打印出來的調(diào)用文件行號(hào)總是同一個(gè)位置,因?yàn)閷?duì)于?glog?來講,它的調(diào)用方即總是?logger.Print?方法。這個(gè)時(shí)候,我們可以通過設(shè)置回溯值來跳過回溯的文件數(shù),使用?SetStackSkip?或者鏈?zhǔn)椒椒?Skip?實(shí)現(xiàn)。
文件回溯值的設(shè)置同樣也會(huì)影響?Stack?調(diào)用回溯打印結(jié)果。
package main
import (
"context"
"github.com/gogf/gf/v2/frame/g"
)
func PrintLog(ctx context.Context, content string) {
g.Log().Skip(1).Line().Print(ctx, "line number with skip:", content)
g.Log().Line().Print(ctx, "line number without skip:", content)
}
func main() {
ctx := context.TODO()
PrintLog(ctx, "just test")
}執(zhí)行后,終端輸出結(jié)果為:
2019-05-23 19:30:10.984 glog_line2.go:13: line number with skip: just test
2019-05-23 19:30:10.984 glog_line2.go:9: line number without skip: just test 當(dāng)前名稱:創(chuàng)新互聯(lián)GoFrame教程:GoFrame日志組件-鏈?zhǔn)讲僮?
分享路徑:http://m.fisionsoft.com.cn/article/djiiphs.html


咨詢
建站咨詢
