MDGSF Software Engineer

[GO] log

2017-09-20
GO

1

logfile, err := os.OpenFile("accserver.log", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
    fmt.Printf("%s\n", err.Error())
    os.Exit(-1)
}
defer logfile.Close()
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
log.SetOutput(logfile)

2

package main

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

func main() {
    logfile, err := os.OpenFile("test.log", os.O_RDWR|os.O_CREATE, 0666)
    if err != nil {
        fmt.Printf("%s\n", err.Error())
        os.Exit(-1)
    }
    defer logfile.Close()

    arr := []int{2, 3}

    logger := log.New(logfile, "", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
    logger.Print("arr", arr, "\n")
    logger.Printf("arr[0] = %d", arr[0])
    logger.Println("hello")
    logger.Println("oh....")
    logger.Fatal("test")
}

weixingongzhonghao

上一篇 [GO] glog

下一篇 [GO] 生成素数

Comments

Content