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

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
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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

View File

@ -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()
}