GO写日志文件

package main

import (
    "fmt"
    "log"
    "os"
    "time"
)

func main() {
    fileObj, err := os.OpenFile("./log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    if err != nil {
        fmt.Println("文件打开错误")
    }
    log.SetOutput(fileObj)
    //每隔3S往文件里写日志
    for {
        log.Println("测试")
        time.Sleep(time.Second * 3)
    }
}

 

参考:https://www.bilibili.com/video/BV1fz4y1m7Pm?p=81&spm_id_from=pageDriver

 

posted @ 2021-06-24 15:08  蜗牛的礼物  阅读(132)  评论(0编辑  收藏  举报