main
wpl 2024-10-15 20:05:52 +08:00
parent cb978f67ed
commit 083ec1e2f4
3 changed files with 56 additions and 4 deletions

View File

@ -4,16 +4,24 @@ import "fmt"
//logger 日志结构体 //logger 日志结构体
type Logger struct { type Logger struct {
Level LogLevel
} }
//Newlog 构造函数 //Newlog 构造函数
func NewLog() Logger { func NewLog(levelStr string) Logger {
return Logger{} level,err :=parseLogLevel(levelStr)
if err !=nil{
panic(err)
}
return Logger{
Level:level,
}
} }
func (l Logger) Debug(msg string) { 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) { func (l Logger) Info(msg string) {
fmt.Println(msg) fmt.Println(msg)

View File

@ -0,0 +1,11 @@
package mylogger_test
import "runtime"
func main() {
}
func getInfo(n int) {
pc, fine, line, ok := runtime.Caller()
}

View File

@ -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所执行的行数
}