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

package osutil
import (
"fmt"
"os"
"strings"
"time"
)
const tmpl = "%v - %v: "
type LoggerService struct {
DateFormat string
Utc bool
file *os.File
log bool
}
func NewLogger() *LoggerService {
l := LoggerService{}
l.DateFormat = time.RFC3339Nano
l.Utc = true
return &l
}
func (l *LoggerService) Start() {
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...)
fmt.Fprintf(os.Stdout, formatNewLine(format), params...)
}
func (l *LoggerService) formattedTime() string {
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"
}