Created
July 3, 2015 09:16
-
-
Save dfer/8fec6a5548b27d8767d7 to your computer and use it in GitHub Desktop.
Example with log to file
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 ( | |
| "log" | |
| "os" | |
| ) | |
| var Error *log.Logger | |
| var Info *log.Logger | |
| func init() { | |
| file, err := os.OpenFile("log.txt", os.O_APPEND | os.O_CREATE | os.O_RDWR, 0666) | |
| if err != nil { | |
| log.Fatalf("Failed to open log file: %v", err) | |
| } | |
| //defer file.Close() | |
| //Error = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) | |
| Info = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) | |
| //logger = log.New(os.Stdout, "Info: ", log.Ldate|log.Ltime|log.Lshortfile) | |
| } | |
| func log_error(err interface{}) { | |
| Error.Printf("%s\n", err) | |
| } | |
| func log_info(text string){ | |
| Info.Printf("Value is %s \n", text) | |
| } | |
| func main() { | |
| log_info("Some text") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment