pax_global_header00006660000000000000000000000064144654334520014524gustar00rootroot0000000000000052 comment=443b333ff1b38324c40cea429acd6b155bd7c8d6 dlog-0.9/000077500000000000000000000000001446543345200123215ustar00rootroot00000000000000dlog-0.9/.github/000077500000000000000000000000001446543345200136615ustar00rootroot00000000000000dlog-0.9/.github/workflows/000077500000000000000000000000001446543345200157165ustar00rootroot00000000000000dlog-0.9/.github/workflows/go.yml000066400000000000000000000012321446543345200170440ustar00rootroot00000000000000name: Go on: push: branches: [ master ] pull_request: branches: [ master ] jobs: build: name: Build runs-on: ubuntu-latest steps: - name: Set up Go 1.x uses: actions/setup-go@v2 with: go-version: ^1.13 - name: Check out code into the Go module directory uses: actions/checkout@v2 - name: Get dependencies run: | go get -v -t -d ./... if [ -f Gopkg.toml ]; then curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh dep ensure fi - name: Build run: go build -v ./... - name: Test run: go test -v ./... dlog-0.9/.gitignore000066400000000000000000000004231446543345200143100ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.dll *.so *.dylib # Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ dlog-0.9/LICENSE000066400000000000000000000024511446543345200133300ustar00rootroot00000000000000BSD 2-Clause License Copyright (c) 2018-2023, Frank Denis All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. dlog-0.9/README.md000066400000000000000000000014651446543345200136060ustar00rootroot00000000000000[![](https://godoc.org/github.com/jedisct1/dlog?status.svg)](https://godoc.org/github.com/jedisct1/dlog) # dlog Go's standard logger is fairly limited. As result, kazilion alternatives loggers have been written. All of these are wonderful. They can make your logs look colorful and pretty, buffer things in complicated ways, format data for ElasticSearch, and more. 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, to `syslog` and to the Windows event log. So, here's one more logging library for Go. The dumbest of them all. Enjoy. dlog-0.9/dlog.go000066400000000000000000000134421446543345200136010ustar00rootroot00000000000000package dlog import ( "flag" "fmt" "os" "strconv" "strings" "sync" "sync/atomic" "time" ) type Severity int32 type globals struct { sync.Mutex logLevel Severity useSyslog *bool truncateLogFile *bool appName string syslogFacility string systemLogger *systemLogger fileName *string outFd *os.File lastMessage string lastOccurrence time.Time occurrences uint64 } var _globals = globals{ logLevel: SeverityLast, appName: "-", lastMessage: "", lastOccurrence: time.Now(), occurrences: 0, } const ( SeverityDebug Severity = iota SeverityInfo SeverityNotice SeverityWarning SeverityError SeverityCritical SeverityFatal SeverityLast ) const ( floodDelay = 5 * time.Second floodMinRepeats = 3 ) 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 } func Init(appName string, logLevel Severity, syslogFacility string) error { _globals.logLevel.set(logLevel) if len(syslogFacility) == 0 { syslogFacility = "DAEMON" } _globals.appName = appName _globals.syslogFacility = syslogFacility _globals.useSyslog = flag.Bool("syslog", false, "Send application logs to the local system logger (Eventlog on Windows, syslog on Unix)") _globals.fileName = flag.String("logfile", "", "Write application logs to file") _globals.truncateLogFile = flag.Bool("logfile-truncate", false, "Truncate the application log file; keep only data from the most recent application launch") flag.Var(&_globals.logLevel, "loglevel", fmt.Sprintf("Application 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 TruncateLogFile(value bool) { _globals.Lock() _globals.truncateLogFile = &value _globals.Unlock() } func UseLogFile(fileName string) { _globals.Lock() _globals.fileName = &fileName _globals.Unlock() } func GetFileDescriptor() *os.File { _globals.Lock() createFileDescriptor() _globals.Unlock() return _globals.outFd } func SetFileDescriptor(fd *os.File) { _globals.Lock() _globals.outFd = fd _globals.Unlock() } func createFileDescriptor() { if _globals.fileName != nil && len(*_globals.fileName) > 0 && _globals.outFd == nil { mode := os.O_WRONLY | os.O_CREATE if _globals.truncateLogFile != nil && *_globals.truncateLogFile { mode |= os.O_TRUNC } else { mode |= os.O_APPEND } outFd, err := os.OpenFile(*_globals.fileName, mode, 0o644) if err == nil { _globals.outFd = outFd } } } func logf(severity Severity, format string, args ...interface{}) { if severity < _globals.logLevel.get() { return } now := time.Now().Local() 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() defer _globals.Unlock() if _globals.lastMessage == message { if time.Since(_globals.lastOccurrence) < floodDelay { _globals.occurrences++ if _globals.occurrences > floodMinRepeats { return } } } else { _globals.occurrences = 0 _globals.lastMessage = message } _globals.lastOccurrence = now if *_globals.useSyslog && _globals.systemLogger == nil { systemLogger, err := newSystemLogger(_globals.appName, _globals.syslogFacility) if err == nil { _globals.systemLogger = systemLogger } } createFileDescriptor() 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 { _globals.outFd.WriteString(line) _globals.outFd.Sync() } else { os.Stderr.WriteString(line) } } if severity >= SeverityFatal { os.Exit(255) } } func log(severity Severity, args interface{}) { logf(severity, "%v", args) } dlog-0.9/dlog_test.go000066400000000000000000000002771446543345200146420ustar00rootroot00000000000000package dlog func Example() { Init("example", SeverityNotice, "") // Call flag.Parse() around that time Info("Application is starting") Debugf("Counter value: %d", 0) Fatal("Kaboom") } dlog-0.9/go.mod000066400000000000000000000001671446543345200134330ustar00rootroot00000000000000module github.com/jedisct1/dlog go 1.20 require ( github.com/hashicorp/go-syslog v1.0.0 golang.org/x/sys v0.11.0 ) dlog-0.9/go.sum000066400000000000000000000005141446543345200134540ustar00rootroot00000000000000github.com/hashicorp/go-syslog v1.0.0 h1:KaodqZuhUoZereWVIYmpUgZysurB1kBLX2j0MwMrUAE= github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= dlog-0.9/sysdeps_others.go000066400000000000000000000015731446543345200157340ustar00rootroot00000000000000//go:build !windows // +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)) } dlog-0.9/sysdeps_windows.go000066400000000000000000000012651446543345200161200ustar00rootroot00000000000000package dlog import "golang.org/x/sys/windows/svc/eventlog" type systemLogger struct { inner *eventlog.Log } func newSystemLogger(appName string, facility string) (*systemLogger, error) { eventLogger, err := eventlog.Open(appName) if 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) } }