added SetLoggingLevel func, and SecurityLevel log

This commit is contained in:
2022-12-10 20:44:33 -05:00
parent 4db3de09b4
commit 253e6a20d8
2 changed files with 70 additions and 17 deletions

View File

@@ -16,6 +16,7 @@ func NewLevelLogger(out io.Writer, level LogLevel) LevelLogger {
ll.traceLog = log.New(out, "[TRACE]\t", log.Ldate|log.Ltime|log.Llongfile)
ll.debugLog = log.New(out, "[DEBUG]\t", log.Ldate|log.Ltime|log.Llongfile)
ll.infoLog = log.New(out, "[INFO]\t", log.Ldate|log.Ltime)
ll.securityLog = log.New(out, "[SECURITY]\t", log.Ldate|log.Ltime)
ll.warnLog = log.New(out, "[WARN]\t", log.Ldate|log.Ltime)
ll.errorLog = log.New(out, "[ERROR]\t", log.Ldate|log.Ltime|log.Llongfile)
ll.fatalLog = log.New(out, "[FATAL]\t", log.Ldate|log.Ltime|log.Llongfile)
@@ -28,88 +29,102 @@ func NewLevelLogger(out io.Writer, level LogLevel) LevelLogger {
// so only use Println versions
// always print panic and fatal log messages
func (ll LevelLogger) Panic(v ...any) {
func (ll *LevelLogger) Panic(v ...any) {
ll.panicLog.Panicln(v...)
}
func (ll LevelLogger) Panicf(format string, v ...any) {
func (ll *LevelLogger) Panicf(format string, v ...any) {
ll.panicLog.Panicf(format, v...)
}
func (ll LevelLogger) Trace(v ...any) {
func (ll *LevelLogger) Trace(v ...any) {
if TraceLevel >= ll.CurrentLevel {
ll.traceLog.Println(v...)
ll.traceLog.Println(debug.Stack())
}
}
func (ll LevelLogger) Tracef(format string, v ...any) {
func (ll *LevelLogger) Tracef(format string, v ...any) {
if TraceLevel >= ll.CurrentLevel {
ll.traceLog.Printf(format, v...)
ll.traceLog.Println(debug.Stack())
}
}
func (ll LevelLogger) Debug(v ...any) {
func (ll *LevelLogger) Debug(v ...any) {
if DebugLevel >= ll.CurrentLevel {
ll.debugLog.Println(v...)
}
}
func (ll LevelLogger) Debugf(format string, v ...any) {
func (ll *LevelLogger) Debugf(format string, v ...any) {
if DebugLevel >= ll.CurrentLevel {
ll.debugLog.Printf(format, v...)
}
}
func (ll LevelLogger) Info(v ...any) {
func (ll *LevelLogger) Info(v ...any) {
if InfoLevel >= ll.CurrentLevel {
ll.infoLog.Println(v...)
}
}
func (ll LevelLogger) Infof(format string, v ...any) {
func (ll *LevelLogger) Infof(format string, v ...any) {
if InfoLevel >= ll.CurrentLevel {
ll.infoLog.Printf(format, v...)
}
}
func (ll LevelLogger) Warn(v ...any) {
func (ll *LevelLogger) Secure(v ...any) {
if SecurityLevel >= ll.CurrentLevel {
ll.securityLog.Println(v...)
}
}
func (ll *LevelLogger) Securef(format string, v ...any) {
if SecurityLevel >= ll.CurrentLevel {
ll.securityLog.Printf(format, v...)
}
}
func (ll *LevelLogger) Warn(v ...any) {
if WarnLevel >= ll.CurrentLevel {
ll.warnLog.Println(v...)
}
}
func (ll LevelLogger) Warnf(format string, v ...any) {
func (ll *LevelLogger) Warnf(format string, v ...any) {
if WarnLevel >= ll.CurrentLevel {
ll.warnLog.Printf(format, v...)
}
}
func (ll LevelLogger) Error(v ...any) {
func (ll *LevelLogger) Error(v ...any) {
if ErrorLevel >= ll.CurrentLevel {
ll.errorLog.Println(v...)
}
}
func (ll LevelLogger) Errorf(format string, v ...any) {
func (ll *LevelLogger) Errorf(format string, v ...any) {
if ErrorLevel >= ll.CurrentLevel {
ll.errorLog.Printf(format, v...)
}
}
func (ll LevelLogger) Fatal(v ...any) {
func (ll *LevelLogger) Fatal(v ...any) {
ll.fatalLog.Fatalln(v...)
}
func (ll LevelLogger) Fatalf(format string, v ...any) {
func (ll *LevelLogger) Fatalf(format string, v ...any) {
ll.fatalLog.Fatalf(format, v...)
}
// spelling: Panic, Trace, Debug, Info, Warn, Error, Fatal
func (ll LevelLogger) SetPrefix(logname string, prefix string) {
func (ll *LevelLogger) SetPrefix(logname string, prefix string) {
switch {
case logname == "Panic":
ll.panicLog.SetPrefix(prefix)
@@ -119,6 +134,8 @@ func (ll LevelLogger) SetPrefix(logname string, prefix string) {
ll.debugLog.SetPrefix(prefix)
case logname == "Info":
ll.infoLog.SetPrefix(prefix)
case logname == "Security":
ll.securityLog.SetPrefix(prefix)
case logname == "Warn":
ll.warnLog.SetPrefix(prefix)
case logname == "Error":
@@ -128,7 +145,7 @@ func (ll LevelLogger) SetPrefix(logname string, prefix string) {
}
}
func (ll LevelLogger) SetOutput(logname string, newout io.Writer) {
func (ll *LevelLogger) SetOutput(logname string, newout io.Writer) {
switch {
case logname == "Panic":
ll.panicLog.SetOutput(newout)
@@ -138,6 +155,8 @@ func (ll LevelLogger) SetOutput(logname string, newout io.Writer) {
ll.debugLog.SetOutput(newout)
case logname == "Info":
ll.infoLog.SetOutput(newout)
case logname == "Security":
ll.securityLog.SetOutput(newout)
case logname == "Warn":
ll.warnLog.SetOutput(newout)
case logname == "Error":
@@ -147,7 +166,7 @@ func (ll LevelLogger) SetOutput(logname string, newout io.Writer) {
}
}
func (ll LevelLogger) SetFlags(logname string, newFlags int) {
func (ll *LevelLogger) SetFlags(logname string, newFlags int) {
switch {
case logname == "Panic":
ll.panicLog.SetFlags(newFlags)
@@ -157,6 +176,8 @@ func (ll LevelLogger) SetFlags(logname string, newFlags int) {
ll.debugLog.SetFlags(newFlags)
case logname == "Info":
ll.infoLog.SetFlags(newFlags)
case logname == "Security":
ll.securityLog.SetFlags(newFlags)
case logname == "Warn":
ll.warnLog.SetFlags(newFlags)
case logname == "Error":
@@ -166,6 +187,35 @@ func (ll LevelLogger) SetFlags(logname string, newFlags int) {
}
}
func (ll *LevelLogger) SetLoggingLevel(l int) {
if l < 0 || l > 8 {
ll.CurrentLevel = 4 // default
} else {
switch l {
case 0:
ll.CurrentLevel = LevelLoggerOff
case 1:
ll.CurrentLevel = PanicLevel
case 2:
ll.CurrentLevel = TraceLevel
case 3:
ll.CurrentLevel = DebugLevel
case 4:
ll.CurrentLevel = InfoLevel
case 5:
ll.CurrentLevel = SecurityLevel
case 6:
ll.CurrentLevel = WarnLevel
case 7:
ll.CurrentLevel = ErrorLevel
case 8:
ll.CurrentLevel = FatalLevel
default:
ll.CurrentLevel = InfoLevel
}
}
}
func (ll LevelLogger) Blank(n int) {
outs := " "
if n < 1 {

View File

@@ -8,6 +8,7 @@ import (
// Fatal One or more key business functionalities are not working and the whole system doesnt fulfill the business functionalities.
// Error One or more functionalities are not working, preventing some functionalities from working correctly.
// Warn Unexpected behavior happened inside the application, but it is continuing its work and the key business features are operating as expected.
// Security A violation of the app's security practises
// Info An event happened, the event is purely informative and can be ignored during normal operations.
// Debug A log level used for events considered to be useful during software debugging when more granular information is needed.
// Trace A log level describing events showing step by step execution of your code that can be ignored during the standard operation, but may be useful during extended debugging sessions.
@@ -20,6 +21,7 @@ const (
TraceLevel
DebugLevel
InfoLevel
SecurityLevel
WarnLevel
ErrorLevel
FatalLevel
@@ -35,6 +37,7 @@ type LevelLogger struct {
traceLog *log.Logger
debugLog *log.Logger
infoLog *log.Logger
securityLog *log.Logger
warnLog *log.Logger
errorLog *log.Logger
fatalLog *log.Logger