-
-
Save bbg/790eeb71b1d5a2e09f12c418f399c774 to your computer and use it in GitHub Desktop.
Log Example For Go
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 utils | |
import ( | |
"log" | |
"os" | |
) | |
var ( | |
Log *log.Logger | |
) | |
func NewLog(logpath string) { | |
println("LogFile: " + logpath) | |
file, err := os.Create(logpath) | |
if err != nil { | |
panic(err) | |
} | |
Log = log.New(file, "", log.LstdFlags|log.Lshortfile) | |
} |
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 ( | |
"./utils" | |
"flag" | |
"time" | |
) | |
var ( | |
logpath = flag.String("logpath", "/tmp/weather.log", "Log Path") | |
) | |
func main() { | |
flag.Parse() | |
utils.NewLog(*logpath) | |
utils.Log.Println("hello") | |
for i := 0; i < 10; i++ { | |
utils.Log.Println(i) | |
} | |
time.Sleep(3 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment