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

4
Gopkg.lock generated
View file

@ -68,7 +68,7 @@
branch = "master" branch = "master"
name = "github.com/jedisct1/dlog" name = "github.com/jedisct1/dlog"
packages = ["."] packages = ["."]
revision = "b53fa44bbfb62dc284a052d828390b21f50d61f9" revision = "c395a1da3e92d73b41c42bad89e8e14dadede3b8"
[[projects]] [[projects]]
branch = "master" branch = "master"
@ -112,7 +112,7 @@
"poly1305", "poly1305",
"salsa20/salsa" "salsa20/salsa"
] ]
revision = "13931e22f9e72ea58bb73048bc752b48c6d4d4ac" revision = "a6600008915114d9c087fad9f03d75087b1a74df"
[[projects]] [[projects]]
branch = "master" branch = "master"

View file

@ -7,9 +7,19 @@
packages = ["."] packages = ["."]
revision = "326bf4a7f709d263f964a6a96558676b103f3534" revision = "326bf4a7f709d263f964a6a96558676b103f3534"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
packages = [
"windows",
"windows/registry",
"windows/svc/eventlog"
]
revision = "2c42eef0765b9837fbdab12011af7830f55f88f0"
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "47c6ffc1a8ae6fd34bff3832f6d38cf0e950ca98e55fb4af939dfb80e61f0409" inputs-digest = "7dd24393da8db7d16249fa8f14426fb6418a2cefca2045da212cf4f299a5a1f0"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View file

@ -1,3 +1,5 @@
[![](https://godoc.org/github.com/jedisct1/dlog?status.svg)](https://godoc.org/github.com/jedisct1/dlog)
# dlog # dlog
Go's standard logger is fairly limited. As result, kazilion alternatives loggers have been written. Go's standard logger is fairly limited. As result, kazilion alternatives loggers have been written.
@ -6,6 +8,6 @@ All of these are wonderful. They can make your logs look colorful and pretty, fo
Cool, but all I wanted is something super dumb, that just exposes `log.Info()`, `log.Error()` and a couple other standard levels. Cool, but all I wanted is something super dumb, that just exposes `log.Info()`, `log.Error()` and a couple other standard levels.
I don't need a super flexible kitchen sink. Just something super basic and trivial to use. I just want it to handle different log levels, and be able to write simple logs to `stderr`, to a local file or to `syslog`. I don't need a super flexible kitchen sink. Just something super basic and trivial to use. I just want it to handle different log levels, and be able to write simple logs to `stderr`, to a local file, to `syslog` and the the Windows event log.
So, here's one more logging library for Go. The dumbest of them all. Enjoy. So, here's one more logging library for Go. The dumbest of them all. Enjoy.

View file

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

9
vendor/github.com/jedisct1/dlog/dlog_test.go generated vendored Normal file
View file

@ -0,0 +1,9 @@
package dlog
func Example() {
Init("example", SeverityNotice, "")
// Call flag.Parse() around that time
Info("Application is starting")
Debugf("Counter value: %d", 0)
Fatal("Kaboom")
}

33
vendor/github.com/jedisct1/dlog/sysdeps_others.go generated vendored Normal file
View file

@ -0,0 +1,33 @@
// +build !windows
package dlog
import (
"github.com/hashicorp/go-syslog"
)
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,
}
type systemLogger struct {
inner *gsyslog.Syslogger
}
func newSystemLogger(appName string, facility string) (*systemLogger, error) {
eventLogger, err := gsyslog.NewLogger(gsyslog.LOG_INFO, facility, appName)
if err != nil {
return nil, err
}
return &systemLogger{inner: &eventLogger}, nil
}
func (systemLogger *systemLogger) writeString(severity Severity, message string) {
(*systemLogger.inner).WriteLevel(severityToSyslogPriority[severity], []byte(message))
}

32
vendor/github.com/jedisct1/dlog/sysdeps_windows.go generated vendored Normal file
View file

@ -0,0 +1,32 @@
package dlog
import "golang.org/x/sys/windows/svc/eventlog"
type systemLogger struct {
inner *eventlog.Log
}
func newSystemLogger(appName string, facility string) (*systemLogger, error) {
err := eventlog.InstallAsEventCreate(appName, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
return nil, err
}
var eventLogger *eventlog.Log
if eventLogger, err = eventlog.Open(appName); err != nil {
return nil, err
}
return &systemLogger{inner: eventLogger}, nil
}
func (systemLogger *systemLogger) writeString(severity Severity, message string) {
switch severity {
case SeverityError:
case SeverityCritical:
case SeverityFatal:
systemLogger.inner.Error(uint32(severity), message)
case SeverityWarning:
systemLogger.inner.Warning(uint32(severity), message)
default:
systemLogger.inner.Info(uint32(severity), message)
}
}