1.常用简洁版
package main
import "github.com/astaxie/beego/logs"
func main(){
logs.EnableFuncCallDepth(true)
logs.SetLogFuncCallDepth(3)
_ = logs.SetLogger("console")
}
2.进阶详细版本
package main
import (
"encoding/json"
"fmt"
"runtime"
"github.com/astaxie/beego/logs"
)
func main() {
logs.EnableFuncCallDepth(true)
logs.SetLogFuncCallDepth(3)
config := make(map[string]interface{})
config["filename"] = "project.log"
config["level"] = logs.LevelDebug
config["maxlines"] = 0
config["maxsize"] = 0
config["daily"] = true
config["maxdays"] = 10
config["color"] = true
config["perm"] = "0777"
config["rotate"] = true
configStr, err := json.Marshal(config)
if err != nil {
fmt.Println("marshal failed, err:", err)
return
}
_ = logs.SetLogger(logs.AdapterConsole)
_ = logs.SetLogger(logs.AdapterFile, string(configStr))
funcName, _, _, _ := runtime.Caller(0)
logs.Debug(runtime.FuncForPC(funcName).Name(), "11111111111111111111111111111111")
logs.Info(runtime.FuncForPC(funcName).Name(), "222222222222222222222222222222222")
logs.Warn(runtime.FuncForPC(funcName).Name(), "333333333333333333333333333333333")
logs.Error(runtime.FuncForPC(funcName).Name(), "444444444444444444444444444444444")
logs.Critical(runtime.FuncForPC(funcName).Name(), "5555555555555555555555555555555555")
}