log各种格式日志输出
-
package main
-
-
import (
-
"bytes"
-
"fmt"
-
"log"
-
)
-
-
func ExampleLogger() {
-
-
var (
-
buf bytes.Buffer
-
logger = log.New(&buf, "logger: ", log.Lshortfile)
-
)
-
-
logger.Print("Hello, log file!\n")
-
fmt.Print(&buf)
-
-
var buf1 bytes.Buffer
-
var logger1 = log.New(&buf1, "logger1: ", log.Ldate)
-
logger1.Print("Hello, log file!\n")
-
fmt.Print(&buf1)
-
-
var buf2 bytes.Buffer
-
var logger2 = log.New(&buf2, "logger2: ", log.Ltime)
-
logger2.Print("Hello, log file!\n")
-
fmt.Print(&buf2)
-
-
var buf3 bytes.Buffer
-
var logger3 = log.New(&buf3, "logger3: ", log.Lmicroseconds)
-
logger3.Print("Hello, log file!\n")
-
fmt.Print(&buf3)
-
-
var buf4 bytes.Buffer
-
var logger4 = log.New(&buf4, "logger4: ", log.Llongfile)
-
logger4.Print("Hello, log file!\n")
-
fmt.Print(&buf4)
-
-
var buf5 bytes.Buffer
-
var logger5 = log.New(&buf5, "logger5: ", log.Lshortfile)
-
logger5.Print("Hello, log file!\n")
-
fmt.Print(&buf5)
-
-
var buf6 bytes.Buffer
-
var logger6 = log.New(&buf6, "logger6: ", log.LUTC)
-
logger6.Print("Hello, log file!\n")
-
fmt.Print(&buf6)
-
-
var buf7 bytes.Buffer
-
var logger7 = log.New(&buf7, "logger7: ", log.LstdFlags)
-
logger7.Print("Hello, log file!\n")
-
fmt.Print(&buf7)
-
-
// Output:
-
// logger: example_test.go:19: Hello, log
-
}
-
-
func main() {
-
ExampleLogger()
-
}
结果输出:
logger: main.go:16: Hello, log file!
logger1: 2018/01/27 Hello, log file!
logger2: 23:15:55 Hello, log file!
logger3: 23:15:55.273132 Hello, log file!
logger4: C:/Users/liyuan/go/src/file_io/main.go:36: Hello, log file!
logger5: main.go:41: Hello, log file!
logger6: Hello, log file!
logger7: 2018/01/27 23:15:55 Hello, log file!
成功: 进程退出代码 0.
------------------------------------------------------------------------
-
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 main() {
-
ExampleLogger_Output()
-
}
结果:
INFO: main.go:19: Hello world
成功: 进程退出代码 0.
阅读(411) | 评论(0) | 转发(0) |