diff --git a/day06/mylogger_test/console.go b/day06/mylogger_test/console.go index 95a24a1..03b955b 100644 --- a/day06/mylogger_test/console.go +++ b/day06/mylogger_test/console.go @@ -4,19 +4,27 @@ import "fmt" //logger 日志结构体 type Logger struct { + Level LogLevel } //Newlog 构造函数 -func NewLog() Logger { - return Logger{} +func NewLog(levelStr string) Logger { + level,err :=parseLogLevel(levelStr) + if err !=nil{ + panic(err) + } + return Logger{ + Level:level, + } } func (l Logger) Debug(msg string) { - fmt.Println(msg) + now :=time.Now() + fmt.Printf("[%s] %s" now.Format("2006-01-02 15:04:05"),msg) } func (l Logger) Info(msg string) { - fmt.Println(msg) + fmt.Println(msg) } func (l Logger) Warning(msg string) { fmt.Println(msg) diff --git a/day06/mylogger_test/mylogger.go b/day06/mylogger_test/mylogger.go new file mode 100644 index 0000000..c227bed --- /dev/null +++ b/day06/mylogger_test/mylogger.go @@ -0,0 +1,11 @@ +package mylogger_test + +import "runtime" + +func main() { + +} + +func getInfo(n int) { + pc, fine, line, ok := runtime.Caller() +} diff --git a/day06/runtime_demo/main.go b/day06/runtime_demo/main.go new file mode 100644 index 0000000..b59ac54 --- /dev/null +++ b/day06/runtime_demo/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "runtime" +) + +func f() { + pc, fine, line, ok := runtime.Caller(2) + if !ok { + fmt.Printf("runtime.Caller() failed\n") + return + } + funcName := runtime.FuncForPC(pc).Name() //这里拿到的应该是main.main函数,如果需要拿到f()那么这里传递的应该是Caller(0) + fmt.Println(funcName) + fmt.Println(fine) // runtime_demo/main.go + fmt.Println(line) // 12,是去找这个runtime.caller所执行的行数 +} + +func f1() { + f() +} +func main() { + f1() //注意这里的f1()套了一层,所以应该传2 + // pc, fine, line, ok := runtime.Caller(0) //注意这里传0是当前main函数里面执行的package,那比如说上面有个f1()函数,这里的Caller就需要传到1 + // if !ok { + // fmt.Printf("runtime.Caller() failed\n") + // return + // } + // fmt.Println(pc) + // fmt.Println(fine) // runtime_demo/main.go + // fmt.Println(line) // 12,是去找这个runtime.caller所执行的行数 +}