From 80985d108442f5b1c85f2a7263d0565430c8c914 Mon Sep 17 00:00:00 2001 From: brent s Date: Sat, 27 Feb 2021 00:51:58 -0500 Subject: [PATCH] updating to have func to print prefix --- logging/funcs.go | 30 +++++++++++++++++------------- logging/funcs_file.go | 14 ++++++++------ logging/funcs_std.go | 10 ++++++---- logging/funcs_sysd.go | 16 +++++++++------- logging/funcs_syslog.go | 26 +++++++++++++++----------- logging/types.go | 7 ++++--- 6 files changed, 59 insertions(+), 44 deletions(-) diff --git a/logging/funcs.go b/logging/funcs.go index ec12e56..25f1831 100644 --- a/logging/funcs.go +++ b/logging/funcs.go @@ -1,14 +1,14 @@ package logging import ( - native `log` - `os` - `path` + native "log" + "os" + "path" "r00t2.io/goutils/types" - sysd `github.com/coreos/go-systemd/journal` - `r00t2.io/sysutils/paths` + sysd "github.com/coreos/go-systemd/journal" + "r00t2.io/sysutils/paths" ) var ( @@ -39,9 +39,13 @@ func GetLogger(enableDebug bool, prefix string, logpaths ...string) (logger Logg var stat os.FileInfo var devlogPath string = devlog - if hasSyslog, stat, err = paths.RealPathExistsStat(&devlogPath); hasSyslog && err != nil {return} + if hasSyslog, stat, err = paths.RealPathExistsStat(&devlogPath); hasSyslog && err != nil { + return + } - if hasSyslog && !stat.Mode().IsRegular() {logflags.AddFlag(LogSyslog)} else { + if hasSyslog && !stat.Mode().IsRegular() { + logflags.AddFlag(LogSyslog) + } else { var exists bool var success bool var ckLogPaths []string @@ -80,27 +84,27 @@ func GetLogger(enableDebug bool, prefix string, logpaths ...string) (logger Logg if logflags.HasFlag(LogJournald) { logger = &SystemDLogger{ - Prefix: logPrefix, + Prefix: logPrefix, EnableDebug: enableDebug, } } else { if logflags.HasFlag(LogSyslog) { logger = &SyslogLogger{ - Prefix: logPrefix, + Prefix: logPrefix, EnableDebug: enableDebug, } } else { if logflags.HasFlag(LogFile) { logger = &FileLogger{ StdLogger: StdLogger{ - Prefix: logPrefix, + Prefix: logPrefix, EnableDebug: enableDebug, }, - Path: logPath, + Path: logPath, } } else { logger = &StdLogger{ - Prefix: logPrefix, + Prefix: logPrefix, EnableDebug: enableDebug, } } @@ -112,7 +116,7 @@ func GetLogger(enableDebug bool, prefix string, logpaths ...string) (logger Logg logger.setPrefix(prefix) } - logger.Info("logger initialized of type %T with prefix %v", logger, logger.Prefix) + logger.Info("logger initialized of type %T with prefix %v", logger, logger.GetPrefix()) return } diff --git a/logging/funcs_file.go b/logging/funcs_file.go index afae21e..8bbd7a8 100644 --- a/logging/funcs_file.go +++ b/logging/funcs_file.go @@ -1,10 +1,10 @@ package logging import ( - `fmt` - `io` - `log` - `os` + "fmt" + "io" + "log" + "os" ) func (l *FileLogger) Setup() { @@ -31,6 +31,10 @@ func (l *FileLogger) Shutdown() { } +func (l *FileLogger) GetPrefix() string { + return l.Prefix +} + func (l *FileLogger) doDebug(d bool) { l.EnableDebug = d } @@ -91,7 +95,6 @@ func (l *FileLogger) Debug(s string, v ...interface{}) (err error) { func (l *FileLogger) Emerg(s string, v ...interface{}) (err error) { - var msg string if v != nil { @@ -152,7 +155,6 @@ func (l *FileLogger) Notice(s string, v ...interface{}) (err error) { func (l *FileLogger) Warning(s string, v ...interface{}) (err error) { - var msg string if v != nil { diff --git a/logging/funcs_std.go b/logging/funcs_std.go index 20b686c..6292000 100644 --- a/logging/funcs_std.go +++ b/logging/funcs_std.go @@ -1,8 +1,8 @@ package logging import ( - `fmt` - `log` + "fmt" + "log" ) func (l *StdLogger) Setup() { @@ -27,6 +27,10 @@ func (l *StdLogger) setPrefix(prefix string) { l.Logger.SetPrefix(prefix) } +func (l *StdLogger) GetPrefix() string { + return l.Prefix +} + func (l *StdLogger) Alert(s string, v ...interface{}) (err error) { var msg string @@ -78,7 +82,6 @@ func (l *StdLogger) Debug(s string, v ...interface{}) (err error) { func (l *StdLogger) Emerg(s string, v ...interface{}) (err error) { - var msg string if v != nil { @@ -139,7 +142,6 @@ func (l *StdLogger) Notice(s string, v ...interface{}) (err error) { func (l *StdLogger) Warning(s string, v ...interface{}) (err error) { - var msg string if v != nil { diff --git a/logging/funcs_sysd.go b/logging/funcs_sysd.go index 42e842d..8f7bfb1 100644 --- a/logging/funcs_sysd.go +++ b/logging/funcs_sysd.go @@ -1,10 +1,10 @@ package logging import ( - `fmt` - `log` + "fmt" + "log" - `github.com/coreos/go-systemd/journal` + "github.com/coreos/go-systemd/journal" ) func (l *SystemDLogger) Setup() { @@ -29,6 +29,10 @@ func (l *SystemDLogger) setPrefix(prefix string) { l.Prefix = prefix } +func (l *SystemDLogger) GetPrefix() string { + return l.Prefix +} + func (l *SystemDLogger) Alert(s string, v ...interface{}) (err error) { var msg string @@ -80,7 +84,6 @@ func (l *SystemDLogger) Debug(s string, v ...interface{}) (err error) { func (l *SystemDLogger) Emerg(s string, v ...interface{}) (err error) { - var msg string if v != nil { @@ -141,7 +144,6 @@ func (l *SystemDLogger) Notice(s string, v ...interface{}) (err error) { func (l *SystemDLogger) Warning(s string, v ...interface{}) (err error) { - var msg string if v != nil { @@ -162,9 +164,9 @@ func (l *SystemDLogger) renderWrite(msg string, prio journal.Priority) { var err error vars := map[string]string{ - "DOCUMENTATION": "https://git.r00t2.io/Go_GoUtils/", + "DOCUMENTATION": "https://git.r00t2.io/Go_GoUtils/", "SYSLOG_IDENTIFIER": l.Prefix, - "SYSLOG_FACILITY": "1", // USER + "SYSLOG_FACILITY": "1", // USER } if err = journal.Send(msg, prio, vars); err != nil { diff --git a/logging/funcs_syslog.go b/logging/funcs_syslog.go index 522c553..b1358ad 100644 --- a/logging/funcs_syslog.go +++ b/logging/funcs_syslog.go @@ -1,37 +1,37 @@ package logging import ( - `fmt` - `log` - `log/syslog` + "fmt" + "log" + "log/syslog" ) func (l *SyslogLogger) Setup() { var err error - if l.alert, err = syslog.New(syslog.LOG_ALERT | syslogFacility, l.Prefix); err != nil { + if l.alert, err = syslog.New(syslog.LOG_ALERT|syslogFacility, l.Prefix); err != nil { log.Panicln("could not open log for Alert") } - if l.crit, err = syslog.New(syslog.LOG_CRIT | syslogFacility, l.Prefix); err != nil { + if l.crit, err = syslog.New(syslog.LOG_CRIT|syslogFacility, l.Prefix); err != nil { log.Panicln("could not open log for Crit") } - if l.debug, err = syslog.New(syslog.LOG_DEBUG | syslogFacility, l.Prefix); err != nil { + if l.debug, err = syslog.New(syslog.LOG_DEBUG|syslogFacility, l.Prefix); err != nil { log.Panicln("could not open log for Debug") } - if l.emerg, err = syslog.New(syslog.LOG_EMERG | syslogFacility, l.Prefix); err != nil { + if l.emerg, err = syslog.New(syslog.LOG_EMERG|syslogFacility, l.Prefix); err != nil { log.Panicln("could not open log for Emerg") } - if l.err, err = syslog.New(syslog.LOG_ERR | syslogFacility, l.Prefix); err != nil { + if l.err, err = syslog.New(syslog.LOG_ERR|syslogFacility, l.Prefix); err != nil { log.Panicln("could not open log for Err") } - if l.info, err = syslog.New(syslog.LOG_INFO | syslogFacility, l.Prefix); err != nil { + if l.info, err = syslog.New(syslog.LOG_INFO|syslogFacility, l.Prefix); err != nil { log.Panicln("could not open log for Info") } - if l.notice, err = syslog.New(syslog.LOG_NOTICE | syslogFacility, l.Prefix); err != nil { + if l.notice, err = syslog.New(syslog.LOG_NOTICE|syslogFacility, l.Prefix); err != nil { log.Panicln("could not open log for Notice") } - if l.warning, err = syslog.New(syslog.LOG_WARNING | syslogFacility, l.Prefix); err != nil { + if l.warning, err = syslog.New(syslog.LOG_WARNING|syslogFacility, l.Prefix); err != nil { log.Panicln("could not open log for Warning") } @@ -58,6 +58,10 @@ func (l *SyslogLogger) setPrefix(prefix string) { l.Setup() } +func (l *SyslogLogger) GetPrefix() string { + return l.Prefix +} + func (l *SyslogLogger) Alert(s string, v ...interface{}) (err error) { var msg string diff --git a/logging/types.go b/logging/types.go index a157414..77fb599 100644 --- a/logging/types.go +++ b/logging/types.go @@ -1,9 +1,9 @@ package logging import ( - `log` - `log/syslog` - `os` + "log" + "log/syslog" + "os" ) type Logger interface { @@ -17,6 +17,7 @@ type Logger interface { Warning(string, ...interface{}) error doDebug(bool) setPrefix(string) + GetPrefix() string Setup() Shutdown() }