Created
April 5, 2016 07:07
-
-
Save xigang/347469104b1e75324e46a72e6a2bdd60 to your computer and use it in GitHub Desktop.
golang log的简单操作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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