package main

import (
    "flag"
    "fmt"
    "log"
    "os"
    "runtime"
)

var (
    logFileName = flag.String("log", "server.log", "Log file name")
)

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU() * 2)
    flag.Parse()

    //set logfile Stdout
    logFile, logErr := os.OpenFile(*logFileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
    if logErr != nil {
        fmt.Println("Fail to find", *logFile, "cServer start Failed")
        os.Exit(1)
    }
    log.SetOutput(logFile)
    // 默认会有log.Ldate | log.Ltime(日期 时间),这里重写为 日 时 文件名
    log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) //2015/04/22 11:28:41 test.go:29: content
    
    //write log
    log.Printf("Server log! Cause:%v \n", "test log file")

}

 

posted on 2015-04-22 11:34  浊浊然  阅读(1817)  评论(0编辑  收藏  举报