Skip to content

Instantly share code, notes, and snippets.

@alex-alex2006hw
Forked from border/log.go
Created November 4, 2018 22:10
Show Gist options
  • Save alex-alex2006hw/dfb11d9e49463729ff1843446c44a248 to your computer and use it in GitHub Desktop.
Save alex-alex2006hw/dfb11d9e49463729ff1843446c44a248 to your computer and use it in GitHub Desktop.
Log Example For Go
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)
}
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