mirror of
https://github.com/safing/portbase
synced 2025-09-04 19:50:18 +00:00
Switch from file levels logging to pkg levels
This commit is contained in:
parent
33fbe790c7
commit
3890bf68f3
3 changed files with 23 additions and 23 deletions
|
@ -4,10 +4,10 @@ import "flag"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logLevelFlag string
|
logLevelFlag string
|
||||||
fileLogLevelsFlag string
|
pkgLogLevelsFlag string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&logLevelFlag, "log", "info", "set log level to [trace|debug|info|warning|error|critical]")
|
flag.StringVar(&logLevelFlag, "log", "info", "set log level to [trace|debug|info|warning|error|critical]")
|
||||||
flag.StringVar(&fileLogLevelsFlag, "flog", "", "set log level of files: database=trace,firewall=debug")
|
flag.StringVar(&pkgLogLevelsFlag, "plog", "", "set log level of packages: database=trace,firewall=debug")
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func fastcheck(level severity) bool {
|
func fastcheck(level severity) bool {
|
||||||
if fileLevelsActive.IsSet() {
|
if pkgLevelsActive.IsSet() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if uint32(level) < atomic.LoadUint32(logLevel) {
|
if uint32(level) < atomic.LoadUint32(logLevel) {
|
||||||
|
@ -31,7 +31,7 @@ func log(level severity, msg string, trace *ContextTracer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if level is enabled
|
// check if level is enabled
|
||||||
if !fileLevelsActive.IsSet() && uint32(level) < atomic.LoadUint32(logLevel) {
|
if !pkgLevelsActive.IsSet() && uint32(level) < atomic.LoadUint32(logLevel) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,12 +52,12 @@ func log(level severity, msg string, trace *ContextTracer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if level is enabled for file or generally
|
// check if level is enabled for file or generally
|
||||||
if fileLevelsActive.IsSet() {
|
if pkgLevelsActive.IsSet() {
|
||||||
fileOnly := strings.Split(file, "/")
|
fileOnly := strings.Split(file, "/")
|
||||||
if len(fileOnly) < 2 {
|
if len(fileOnly) < 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sev, ok := fileLevels[fileOnly[len(fileOnly)-2]]
|
sev, ok := pkgLevels[fileOnly[len(fileOnly)-2]]
|
||||||
if ok {
|
if ok {
|
||||||
if level < sev {
|
if level < sev {
|
||||||
return
|
return
|
||||||
|
|
|
@ -73,9 +73,9 @@ var (
|
||||||
logLevelInt = uint32(3)
|
logLevelInt = uint32(3)
|
||||||
logLevel = &logLevelInt
|
logLevel = &logLevelInt
|
||||||
|
|
||||||
fileLevelsActive = abool.NewBool(false)
|
pkgLevelsActive = abool.NewBool(false)
|
||||||
fileLevels = make(map[string]severity)
|
pkgLevels = make(map[string]severity)
|
||||||
fileLevelsLock sync.Mutex
|
pkgLevelsLock sync.Mutex
|
||||||
|
|
||||||
logsWaiting = make(chan bool, 1)
|
logsWaiting = make(chan bool, 1)
|
||||||
logsWaitingFlag = abool.NewBool(false)
|
logsWaitingFlag = abool.NewBool(false)
|
||||||
|
@ -90,15 +90,15 @@ var (
|
||||||
testErrors = abool.NewBool(false)
|
testErrors = abool.NewBool(false)
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetFileLevels(levels map[string]severity) {
|
func SetPkgLevels(levels map[string]severity) {
|
||||||
fileLevelsLock.Lock()
|
pkgLevelsLock.Lock()
|
||||||
fileLevels = levels
|
pkgLevels = levels
|
||||||
fileLevelsLock.Unlock()
|
pkgLevelsLock.Unlock()
|
||||||
fileLevelsActive.Set()
|
pkgLevelsActive.Set()
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnSetFileLevels() {
|
func UnSetPkgLevels() {
|
||||||
fileLevelsActive.UnSet()
|
pkgLevelsActive.UnSet()
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetLogLevel(level severity) {
|
func SetLogLevel(level severity) {
|
||||||
|
@ -141,10 +141,10 @@ func Start() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get and set file loglevels
|
// get and set file loglevels
|
||||||
fileLogLevels := fileLogLevelsFlag
|
pkgLogLevels := pkgLogLevelsFlag
|
||||||
if len(fileLogLevels) > 0 {
|
if len(pkgLogLevels) > 0 {
|
||||||
newFileLevels := make(map[string]severity)
|
newPkgLevels := make(map[string]severity)
|
||||||
for _, pair := range strings.Split(fileLogLevels, ",") {
|
for _, pair := range strings.Split(pkgLogLevels, ",") {
|
||||||
splitted := strings.Split(pair, "=")
|
splitted := strings.Split(pair, "=")
|
||||||
if len(splitted) != 2 {
|
if len(splitted) != 2 {
|
||||||
err = fmt.Errorf("log warning: invalid file log level \"%s\", ignoring", pair)
|
err = fmt.Errorf("log warning: invalid file log level \"%s\", ignoring", pair)
|
||||||
|
@ -157,9 +157,9 @@ func Start() (err error) {
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
newFileLevels[splitted[0]] = fileLevel
|
newPkgLevels[splitted[0]] = fileLevel
|
||||||
}
|
}
|
||||||
SetFileLevels(newFileLevels)
|
SetPkgLevels(newPkgLevels)
|
||||||
}
|
}
|
||||||
|
|
||||||
startWriter()
|
startWriter()
|
||||||
|
|
Loading…
Add table
Reference in a new issue