gowiki/pkg/log/log.go

52 lines
843 B
Go

// Copyright (C) 2018 Marius Schellenberger
package log
import (
stdlog "log"
"os"
)
const debug = "debug: "
var (
log *stdlog.Logger
DebugEnabled = false
)
func NewLogger(file string) {
if file == "" {
log = stdlog.New(os.Stdout, "", stdlog.LstdFlags)
return
}
f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
stdlog.Fatal(err)
}
stdlog.SetOutput(f)
log = stdlog.New(f, "", stdlog.LstdFlags)
}
func Println(v ...interface{}) {
log.Println(v...)
}
func Printf(fmt string, v ...interface{}) {
log.Printf(fmt, v...)
}
func Fatal(v ...interface{}) {
log.Fatal(v...)
}
func Debug(v ...interface{}) {
if DebugEnabled {
log.Println(append([]interface{}{debug}, v...))
}
}
func Debugf(fmt string, v ...interface{}) {
if DebugEnabled {
log.Printf(debug+fmt, v...)
}
}