2018-01-11 22:38:50 +01:00
|
|
|
package dlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Severity int32
|
|
|
|
|
|
|
|
type globals struct {
|
|
|
|
sync.Mutex
|
2018-01-18 14:25:45 +01:00
|
|
|
logLevel Severity
|
|
|
|
useSyslog *bool
|
|
|
|
appName string
|
|
|
|
syslogFacility string
|
2018-01-19 19:33:25 +01:00
|
|
|
systemLogger *systemLogger
|
2018-01-18 14:25:45 +01:00
|
|
|
fileName *string
|
|
|
|
outFd *os.File
|
2018-02-06 16:07:54 +01:00
|
|
|
lastMessage string
|
|
|
|
lastOccurrence time.Time
|
|
|
|
occurrences uint64
|
2018-01-11 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_globals = globals{
|
2018-02-06 16:07:54 +01:00
|
|
|
logLevel: SeverityLast,
|
|
|
|
appName: "-",
|
|
|
|
lastMessage: "",
|
|
|
|
lastOccurrence: time.Now(),
|
|
|
|
occurrences: 0,
|
2018-01-11 22:38:50 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SeverityDebug Severity = iota
|
|
|
|
SeverityInfo
|
|
|
|
SeverityNotice
|
|
|
|
SeverityWarning
|
|
|
|
SeverityError
|
|
|
|
SeverityCritical
|
|
|
|
SeverityFatal
|
|
|
|
SeverityLast
|
|
|
|
)
|
|
|
|
|
2018-02-06 16:07:54 +01:00
|
|
|
const (
|
2018-02-14 14:39:43 +01:00
|
|
|
floodDelay = 5 * time.Second
|
|
|
|
floodMinRepeats = 3
|
2018-02-06 16:07:54 +01:00
|
|
|
)
|
|
|
|
|
2018-01-11 22:38:50 +01:00
|
|
|
var SeverityName = []string{
|
|
|
|
SeverityDebug: "DEBUG",
|
|
|
|
SeverityInfo: "INFO",
|
|
|
|
SeverityNotice: "NOTICE",
|
|
|
|
SeverityWarning: "WARNING",
|
|
|
|
SeverityError: "ERROR",
|
|
|
|
SeverityCritical: "CRITICAL",
|
|
|
|
SeverityFatal: "FATAL",
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debugf(format string, args ...interface{}) {
|
|
|
|
logf(SeverityDebug, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Infof(format string, args ...interface{}) {
|
|
|
|
logf(SeverityInfo, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Noticef(format string, args ...interface{}) {
|
|
|
|
logf(SeverityNotice, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warnf(format string, args ...interface{}) {
|
|
|
|
logf(SeverityWarning, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Errorf(format string, args ...interface{}) {
|
|
|
|
logf(SeverityError, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Criticalf(format string, args ...interface{}) {
|
|
|
|
logf(SeverityCritical, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalf(format string, args ...interface{}) {
|
|
|
|
logf(SeverityFatal, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(message interface{}) {
|
|
|
|
log(SeverityDebug, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Info(message interface{}) {
|
|
|
|
log(SeverityInfo, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Notice(message interface{}) {
|
|
|
|
log(SeverityNotice, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warn(message interface{}) {
|
|
|
|
log(SeverityWarning, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(message interface{}) {
|
|
|
|
log(SeverityError, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Critical(message interface{}) {
|
|
|
|
log(SeverityCritical, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(message interface{}) {
|
|
|
|
log(SeverityFatal, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Severity) get() Severity {
|
|
|
|
return Severity(atomic.LoadInt32((*int32)(s)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Severity) set(val Severity) {
|
|
|
|
atomic.StoreInt32((*int32)(s), int32(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Severity) String() string {
|
|
|
|
return strconv.FormatInt(int64(*s), 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Severity) Get() interface{} {
|
|
|
|
return s.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Severity) Set(strVal string) error {
|
|
|
|
val, _ := strconv.Atoi(strVal)
|
|
|
|
s.set(Severity(val))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 14:25:45 +01:00
|
|
|
func Init(appName string, logLevel Severity, syslogFacility string) error {
|
2018-01-11 22:38:50 +01:00
|
|
|
_globals.logLevel.set(logLevel)
|
2018-01-18 14:25:45 +01:00
|
|
|
|
|
|
|
if len(syslogFacility) == 0 {
|
|
|
|
syslogFacility = "DAEMON"
|
|
|
|
}
|
|
|
|
_globals.appName = appName
|
|
|
|
_globals.syslogFacility = syslogFacility
|
2018-01-19 19:33:25 +01:00
|
|
|
_globals.useSyslog = flag.Bool("syslog", false, "Send logs to the local system logger (Eventlog on Windows, syslog on Unix)")
|
2018-01-18 14:25:45 +01:00
|
|
|
_globals.fileName = flag.String("logfile", "", "Write logs to file")
|
2018-01-11 22:38:50 +01:00
|
|
|
flag.Var(&_globals.logLevel, "loglevel", fmt.Sprintf("Log level (%d-%d)", SeverityDebug, SeverityFatal))
|
2018-01-18 14:25:45 +01:00
|
|
|
return nil
|
2018-01-11 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
2018-01-19 19:33:25 +01:00
|
|
|
func LogLevel() Severity {
|
|
|
|
_globals.Lock()
|
|
|
|
logLevel := _globals.logLevel.get()
|
|
|
|
_globals.Unlock()
|
|
|
|
return logLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetLogLevel(logLevel Severity) {
|
|
|
|
_globals.Lock()
|
|
|
|
_globals.logLevel.set(logLevel)
|
|
|
|
_globals.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func UseSyslog(value bool) {
|
|
|
|
_globals.Lock()
|
|
|
|
_globals.useSyslog = &value
|
|
|
|
_globals.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func UseLogFile(fileName string) {
|
|
|
|
_globals.Lock()
|
|
|
|
_globals.fileName = &fileName
|
|
|
|
_globals.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-01-11 22:38:50 +01:00
|
|
|
func logf(severity Severity, format string, args ...interface{}) {
|
|
|
|
if severity < _globals.logLevel.get() {
|
|
|
|
return
|
|
|
|
}
|
2018-01-31 10:14:06 +01:00
|
|
|
now := time.Now().Local()
|
2018-01-11 22:38:50 +01:00
|
|
|
year, month, day := now.Date()
|
|
|
|
hour, minute, second := now.Clock()
|
|
|
|
message := fmt.Sprintf(format, args...)
|
|
|
|
message = strings.TrimSpace(strings.TrimSuffix(message, "\n"))
|
|
|
|
if len(message) <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_globals.Lock()
|
2018-01-18 14:25:45 +01:00
|
|
|
defer _globals.Unlock()
|
2018-02-06 16:07:54 +01:00
|
|
|
if _globals.lastMessage == message {
|
2018-02-14 14:39:43 +01:00
|
|
|
if time.Since(_globals.lastOccurrence) < floodDelay {
|
2018-02-06 16:07:54 +01:00
|
|
|
_globals.occurrences++
|
2018-02-14 14:39:43 +01:00
|
|
|
if _globals.occurrences > floodMinRepeats {
|
2018-02-06 16:07:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_globals.occurrences = 0
|
|
|
|
_globals.lastMessage = message
|
|
|
|
}
|
|
|
|
_globals.lastOccurrence = now
|
2018-01-19 19:33:25 +01:00
|
|
|
if *_globals.useSyslog && _globals.systemLogger == nil {
|
|
|
|
systemLogger, err := newSystemLogger(_globals.appName, _globals.syslogFacility)
|
2018-01-19 20:27:48 +01:00
|
|
|
if err == nil {
|
|
|
|
_globals.systemLogger = systemLogger
|
2018-01-18 14:25:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _globals.fileName != nil && len(*_globals.fileName) > 0 && _globals.outFd == nil {
|
|
|
|
outFd, err := os.OpenFile(*_globals.fileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
|
2018-01-19 20:27:48 +01:00
|
|
|
if err == nil {
|
|
|
|
_globals.outFd = outFd
|
2018-01-18 14:25:45 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-19 19:33:25 +01:00
|
|
|
if _globals.systemLogger != nil {
|
|
|
|
(*_globals.systemLogger).writeString(severity, message)
|
2018-01-18 14:25:45 +01:00
|
|
|
} else {
|
2018-01-18 14:46:19 +01:00
|
|
|
line := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d] [%s] %s\n", year, int(month), day, hour, minute, second, SeverityName[severity], message)
|
2018-01-18 14:25:45 +01:00
|
|
|
if _globals.outFd != nil {
|
|
|
|
_globals.outFd.WriteString(line)
|
|
|
|
_globals.outFd.Sync()
|
|
|
|
} else {
|
|
|
|
os.Stderr.WriteString(line)
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 22:38:50 +01:00
|
|
|
if severity >= SeverityFatal {
|
|
|
|
os.Exit(255)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func log(severity Severity, args interface{}) {
|
|
|
|
logf(severity, "%v", args)
|
|
|
|
}
|