格式化日志输出
-
package main
-
-
import (
-
"bytes"
-
"fmt"
-
"log"
-
)
-
-
func ExampleLogger_Output() {
-
var (
-
buf bytes.Buffer
-
logger = log.New(&buf, "INFO: ", log.Lshortfile)
-
-
infof = func(info string) {
-
logger.Output(2, info)
-
}
-
)
-
-
infof("Hello world")
-
-
fmt.Print(&buf)
-
// Output:
-
// INFO: example_test.go:36: Hello world
-
}
-
-
func logger_print() {
-
var (
-
buf bytes.Buffer
-
logger = log.New(&buf, "logger: ", log.Lshortfile)
-
)
-
-
logger.Print("Hello, log file!")
-
-
fmt.Print(&buf)
-
}
-
-
func main() {
-
ExampleLogger_Output()
-
logger_print()
-
}
输出结果:
INFO: main.go:19: Hello world
logger: main.go:32: Hello, log file!
成功: 进程退出代码 0.
阅读(594) | 评论(0) | 转发(0) |