Skip to content

Instantly share code, notes, and snippets.

@xigang
Created April 5, 2016 07:07
Show Gist options
  • Select an option

  • Save xigang/347469104b1e75324e46a72e6a2bdd60 to your computer and use it in GitHub Desktop.

Select an option

Save xigang/347469104b1e75324e46a72e6a2bdd60 to your computer and use it in GitHub Desktop.
golang log的简单操作
package main
import (
"flag"
"fmt"
"log"
"os"
)
var fpath *string = flag.String("d", "Null", "please input a file path.")
func main() {
flag.Parse()
if *fpath == "Null" {
fmt.Println("please input a file path")
return
}
//检查路径是否存在
_, err := os.Stat(*fpath)
if err != nil {
_, err := os.Create(*fpath)
if err != nil {
fmt.Println(err)
}
}
logfile, err := os.OpenFile(*fpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0)
if err != nil {
fmt.Printf("%s\r\n", err.Error())
os.Exit(-1)
}
defer logfile.Close()
logger := log.New(logfile, " ", log.Ldate|log.Ltime|log.Llongfile)
logger.Println("hello")
logger.Println("oh....")
logger.Println("test")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment