Update dlog; now with support for the Windows Event Log

(completely untested)
This commit is contained in:
Frank Denis 2018-01-19 19:33:25 +01:00
parent 4b4bf36633
commit b02a4e6c73
7 changed files with 122 additions and 23 deletions

View file

@ -9,8 +9,6 @@ import (
"sync"
"sync/atomic"
"time"
"github.com/hashicorp/go-syslog"
)
type Severity int32
@ -21,7 +19,7 @@ type globals struct {
useSyslog *bool
appName string
syslogFacility string
syslogger *gsyslog.Syslogger
systemLogger *systemLogger
fileName *string
outFd *os.File
}
@ -54,16 +52,6 @@ var SeverityName = []string{
SeverityFatal: "FATAL",
}
var severityToSyslogPriority = []gsyslog.Priority{
SeverityDebug: gsyslog.LOG_DEBUG,
SeverityInfo: gsyslog.LOG_INFO,
SeverityNotice: gsyslog.LOG_NOTICE,
SeverityWarning: gsyslog.LOG_WARNING,
SeverityError: gsyslog.LOG_ERR,
SeverityCritical: gsyslog.LOG_CRIT,
SeverityFatal: gsyslog.LOG_ALERT,
}
func Debugf(format string, args ...interface{}) {
logf(SeverityDebug, format, args...)
}
@ -150,12 +138,37 @@ func Init(appName string, logLevel Severity, syslogFacility string) error {
}
_globals.appName = appName
_globals.syslogFacility = syslogFacility
_globals.useSyslog = flag.Bool("syslog", false, "Send logs to the local system logger")
_globals.useSyslog = flag.Bool("syslog", false, "Send logs to the local system logger (Eventlog on Windows, syslog on Unix)")
_globals.fileName = flag.String("logfile", "", "Write logs to file")
flag.Var(&_globals.logLevel, "loglevel", fmt.Sprintf("Log level (%d-%d)", SeverityDebug, SeverityFatal))
return nil
}
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()
}
func logf(severity Severity, format string, args ...interface{}) {
if severity < _globals.logLevel.get() {
return
@ -170,12 +183,12 @@ func logf(severity Severity, format string, args ...interface{}) {
}
_globals.Lock()
defer _globals.Unlock()
if *_globals.useSyslog && _globals.syslogger == nil {
syslogger, err := gsyslog.NewLogger(gsyslog.LOG_INFO, _globals.syslogFacility, _globals.appName)
if *_globals.useSyslog && _globals.systemLogger == nil {
systemLogger, err := newSystemLogger(_globals.appName, _globals.syslogFacility)
if err != nil {
panic(err)
}
_globals.syslogger = &syslogger
_globals.systemLogger = systemLogger
}
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)
@ -184,8 +197,8 @@ func logf(severity Severity, format string, args ...interface{}) {
}
_globals.outFd = outFd
}
if _globals.syslogger != nil {
(*_globals.syslogger).WriteLevel(severityToSyslogPriority[severity], []byte(message))
if _globals.systemLogger != nil {
(*_globals.systemLogger).writeString(severity, message)
} else {
line := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d] [%s] %s\n", year, int(month), day, hour, minute, second, SeverityName[severity], message)
if _globals.outFd != nil {