updating to have func to print prefix

This commit is contained in:
brent s. 2021-02-27 00:51:58 -05:00
parent c6d53a0cca
commit 80985d1084
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
6 changed files with 59 additions and 44 deletions

View File

@ -1,14 +1,14 @@
package logging package logging


import ( import (
native `log` native "log"
`os` "os"
`path` "path"


"r00t2.io/goutils/types" "r00t2.io/goutils/types"


sysd `github.com/coreos/go-systemd/journal` sysd "github.com/coreos/go-systemd/journal"
`r00t2.io/sysutils/paths` "r00t2.io/sysutils/paths"
) )


var ( var (
@ -39,9 +39,13 @@ func GetLogger(enableDebug bool, prefix string, logpaths ...string) (logger Logg
var stat os.FileInfo var stat os.FileInfo
var devlogPath string = devlog 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 exists bool
var success bool var success bool
var ckLogPaths []string var ckLogPaths []string
@ -112,7 +116,7 @@ func GetLogger(enableDebug bool, prefix string, logpaths ...string) (logger Logg
logger.setPrefix(prefix) 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 return
} }

View File

@ -1,10 +1,10 @@
package logging package logging


import ( import (
`fmt` "fmt"
`io` "io"
`log` "log"
`os` "os"
) )


func (l *FileLogger) Setup() { 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) { func (l *FileLogger) doDebug(d bool) {
l.EnableDebug = d 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) { func (l *FileLogger) Emerg(s string, v ...interface{}) (err error) {



var msg string var msg string


if v != nil { 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) { func (l *FileLogger) Warning(s string, v ...interface{}) (err error) {



var msg string var msg string


if v != nil { if v != nil {

View File

@ -1,8 +1,8 @@
package logging package logging


import ( import (
`fmt` "fmt"
`log` "log"
) )


func (l *StdLogger) Setup() { func (l *StdLogger) Setup() {
@ -27,6 +27,10 @@ func (l *StdLogger) setPrefix(prefix string) {
l.Logger.SetPrefix(prefix) l.Logger.SetPrefix(prefix)
} }


func (l *StdLogger) GetPrefix() string {
return l.Prefix
}

func (l *StdLogger) Alert(s string, v ...interface{}) (err error) { func (l *StdLogger) Alert(s string, v ...interface{}) (err error) {


var msg string 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) { func (l *StdLogger) Emerg(s string, v ...interface{}) (err error) {



var msg string var msg string


if v != nil { 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) { func (l *StdLogger) Warning(s string, v ...interface{}) (err error) {



var msg string var msg string


if v != nil { if v != nil {

View File

@ -1,10 +1,10 @@
package logging package logging


import ( import (
`fmt` "fmt"
`log` "log"


`github.com/coreos/go-systemd/journal` "github.com/coreos/go-systemd/journal"
) )


func (l *SystemDLogger) Setup() { func (l *SystemDLogger) Setup() {
@ -29,6 +29,10 @@ func (l *SystemDLogger) setPrefix(prefix string) {
l.Prefix = prefix l.Prefix = prefix
} }


func (l *SystemDLogger) GetPrefix() string {
return l.Prefix
}

func (l *SystemDLogger) Alert(s string, v ...interface{}) (err error) { func (l *SystemDLogger) Alert(s string, v ...interface{}) (err error) {


var msg string 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) { func (l *SystemDLogger) Emerg(s string, v ...interface{}) (err error) {



var msg string var msg string


if v != nil { 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) { func (l *SystemDLogger) Warning(s string, v ...interface{}) (err error) {



var msg string var msg string


if v != nil { if v != nil {

View File

@ -1,37 +1,37 @@
package logging package logging


import ( import (
`fmt` "fmt"
`log` "log"
`log/syslog` "log/syslog"
) )


func (l *SyslogLogger) Setup() { func (l *SyslogLogger) Setup() {


var err error 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") 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") 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") 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") 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") 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") 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") 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") log.Panicln("could not open log for Warning")
} }


@ -58,6 +58,10 @@ func (l *SyslogLogger) setPrefix(prefix string) {
l.Setup() l.Setup()
} }


func (l *SyslogLogger) GetPrefix() string {
return l.Prefix
}

func (l *SyslogLogger) Alert(s string, v ...interface{}) (err error) { func (l *SyslogLogger) Alert(s string, v ...interface{}) (err error) {


var msg string var msg string

View File

@ -1,9 +1,9 @@
package logging package logging


import ( import (
`log` "log"
`log/syslog` "log/syslog"
`os` "os"
) )


type Logger interface { type Logger interface {
@ -17,6 +17,7 @@ type Logger interface {
Warning(string, ...interface{}) error Warning(string, ...interface{}) error
doDebug(bool) doDebug(bool)
setPrefix(string) setPrefix(string)
GetPrefix() string
Setup() Setup()
Shutdown() Shutdown()
} }