mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-23 07:22:16 +08:00
36 lines
556 B
Go
36 lines
556 B
Go
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)
|
|
}
|
|
}
|