This repository has been archived on 2022-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
tacitus/osutil/logger_service.go

58 lines
878 B
Go
Raw Normal View History

2017-09-10 10:57:35 -07:00
package osutil
import (
"fmt"
"os"
"strings"
2017-09-21 14:41:55 -07:00
"time"
2017-09-10 10:57:35 -07:00
)
const tmpl = "%v - %v: "
type LoggerService struct {
DateFormat string
Utc bool
file *os.File
log bool
}
func NewLogger() *LoggerService {
2017-09-10 13:04:02 -07:00
l := LoggerService{}
2017-09-10 10:57:35 -07:00
l.DateFormat = time.RFC3339Nano
2017-09-21 14:41:55 -07:00
l.Utc = true
2017-09-10 10:57:35 -07:00
return &l
}
2017-09-21 14:41:55 -07:00
func (l *LoggerService) Start() {
2017-09-10 10:57:35 -07:00
l.log = true
}
func (l *LoggerService) Stop() {
l.log = false
}
func (l *LoggerService) Info(format string, params ...interface{}) {
format = tmpl + format
params = append([]interface{}{l.formattedTime(), "I"}, params...)
2017-09-21 14:41:55 -07:00
fmt.Fprintf(os.Stdout, formatNewLine(format), params...)
2017-09-10 10:57:35 -07:00
}
2017-09-10 13:04:02 -07:00
func (l *LoggerService) formattedTime() string {
2017-09-10 10:57:35 -07:00
t := time.Now()
if l.Utc {
t = t.UTC()
}
return t.Format(l.DateFormat)
}
func formatNewLine(format string) string {
s := strings.Replace(format, "\n", " ", -1)
s = strings.TrimSpace(s)
return s + "\n"
}