47 lines
800 B
Go
47 lines
800 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type Logger struct {
|
|
logpath string
|
|
logfile []byte
|
|
}
|
|
|
|
// write to our file, if it exists
|
|
func (l *Logger) Log(str string) {
|
|
|
|
CheckLogPath(l.logpath)
|
|
|
|
var msg string
|
|
msg = str
|
|
os.WriteFile(l.logpath, []byte(msg), os.ModeAppend)
|
|
}
|
|
|
|
// dump the err into a string, write to our log file, then pass off the error to the log package
|
|
func (l *Logger) Fatal(err error) {
|
|
l.Log(fmt.Sprintf("%v\n", err))
|
|
log.Fatal(err)
|
|
}
|
|
|
|
func NewLogger(logpath string) *Logger {
|
|
CheckLogPath(logpath)
|
|
|
|
return &Logger{
|
|
logpath: logpath,
|
|
}
|
|
}
|
|
|
|
// checks for existence of given logpath file, creates if it doesn't
|
|
func CheckLogPath(logpath string) {
|
|
_, err := os.Stat(logpath)
|
|
if os.IsNotExist(err) {
|
|
os.Create(logpath)
|
|
} else if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|