58 lines
886 B
Go
58 lines
886 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"
|
|
}
|