mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-22 23:12:15 +08:00
docs: log 日志
This commit is contained in:
parent
fee0adc3da
commit
11361a4dab
35
base/log/main.go
Normal file
35
base/log/main.go
Normal file
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
// log
|
||||
|
||||
func main() {
|
||||
logFilePath, _ := filepath.Abs(filepath.Join(os.TempDir(), "./log.log"))
|
||||
|
||||
f, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_ = f.Close()
|
||||
}()
|
||||
|
||||
// 设置 log 输出位置
|
||||
// 默认 输出到 标准输出流
|
||||
//log.SetOutput(os.Stdout)
|
||||
// 输出 到 指定文件
|
||||
log.SetOutput(f)
|
||||
|
||||
for {
|
||||
log.Println("测试日志")
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user