From 2c892f0d339b0635134f886b6d15b1c5baa64f1e Mon Sep 17 00:00:00 2001 From: Kevin Offet Date: Thu, 7 Jul 2022 11:22:28 -0400 Subject: [PATCH] first crack. good to go??? --- .gitignore | 2 +- go.mod | 4 +- go.sum | 2 + levellogger.go | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 9 --- types.go | 40 +++++++++++++ 6 files changed, 196 insertions(+), 11 deletions(-) create mode 100644 go.sum create mode 100644 levellogger.go delete mode 100644 main.go create mode 100644 types.go diff --git a/.gitignore b/.gitignore index a742d05..1e96589 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -levelinglogger +levellogger .env .env.toml env.toml diff --git a/go.mod b/go.mod index bf67364..510bd58 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ -module article12apps.net/levelinglogger +module article12apps.net/levellogger go 1.18 + +require github.com/go-errors/errors v1.4.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..66ab7d8 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= +github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= diff --git a/levellogger.go b/levellogger.go new file mode 100644 index 0000000..1246786 --- /dev/null +++ b/levellogger.go @@ -0,0 +1,150 @@ +package levellogger + +import ( + "io" + "log" + + "github.com/go-errors/errors" +) + +var traceable = errors.Errorf("Stack trace request") + +func Crash() error { + return errors.New(traceable) +} + +func NewLevelLogger(out io.Writer, level LogLevel) LevelLogger { + var ll LevelLogger + + ll.CurrentLevel = level + + ll.panicLog = log.New(out, "[PANIC]\t", log.Ldate|log.Ltime|log.Llongfile) + 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.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) + + return ll +} + +// It's a log -- line output +// so only use Println versions +func (ll LevelLogger) Panic(v ...any) { + ll.panicLog.Panicln(v...) +} + +func (ll LevelLogger) Panicf(format string, v ...any) { + ll.panicLog.Panicf(format, v...) +} + +func (ll LevelLogger) Trace(v ...any) { + err := Crash() + ll.traceLog.Println(err.(*errors.Error).ErrorStack()) + ll.traceLog.Println(v...) +} + +func (ll LevelLogger) Tracef(format string, v ...any) { + err := Crash() + ll.traceLog.Println(err.(*errors.Error).ErrorStack()) + ll.traceLog.Printf(format, v...) +} + +func (ll LevelLogger) Debug(v ...any) { + ll.debugLog.Println(v...) +} + +func (ll LevelLogger) Debugf(format string, v ...any) { + ll.debugLog.Printf(format, v...) +} + +func (ll LevelLogger) Info(v ...any) { + ll.infoLog.Println(v...) +} + +func (ll LevelLogger) Infof(format string, v ...any) { + ll.infoLog.Printf(format, v...) +} + +func (ll LevelLogger) Warn(v ...any) { + ll.warnLog.Println(v...) +} + +func (ll LevelLogger) Warnf(format string, v ...any) { + ll.warnLog.Printf(format, v...) +} + +func (ll LevelLogger) Error(v ...any) { + ll.errorLog.Println(v...) +} + +func (ll LevelLogger) Errorf(format string, v ...any) { + ll.errorLog.Printf(format, v...) +} + +func (ll LevelLogger) Fatal(v ...any) { + ll.fatalLog.Fatalln(v...) +} + +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) { + switch { + case logname == "Panic": + ll.panicLog.SetPrefix(prefix) + case logname == "Trace": + ll.traceLog.SetPrefix(prefix) + case logname == "Debug": + ll.debugLog.SetPrefix(prefix) + case logname == "Info": + ll.infoLog.SetPrefix(prefix) + case logname == "Warn": + ll.warnLog.SetPrefix(prefix) + case logname == "Error": + ll.errorLog.SetPrefix(prefix) + case logname == "Fatal": + ll.fatalLog.SetPrefix(prefix) + } +} + +func (ll LevelLogger) SetOutput(logname string, newout io.Writer) { + switch { + case logname == "Panic": + ll.panicLog.SetOutput(newout) + case logname == "Trace": + ll.traceLog.SetOutput(newout) + case logname == "Debug": + ll.debugLog.SetOutput(newout) + case logname == "Info": + ll.infoLog.SetOutput(newout) + case logname == "Warn": + ll.warnLog.SetOutput(newout) + case logname == "Error": + ll.errorLog.SetOutput(newout) + case logname == "Fatal": + ll.fatalLog.SetOutput(newout) + } +} + +func (ll LevelLogger) SetFlags(logname string, newFlags int) { + switch { + case logname == "Panic": + ll.panicLog.SetFlags(newFlags) + case logname == "Trace": + ll.traceLog.SetFlags(newFlags) + case logname == "Debug": + ll.debugLog.SetFlags(newFlags) + case logname == "Info": + ll.infoLog.SetFlags(newFlags) + case logname == "Warn": + ll.warnLog.SetFlags(newFlags) + case logname == "Error": + ll.errorLog.SetFlags(newFlags) + case logname == "Fatal": + ll.fatalLog.SetFlags(newFlags) + } +} diff --git a/main.go b/main.go deleted file mode 100644 index 97e0ccc..0000000 --- a/main.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import ( - "fmt" -) - -func main() { - fmt.Println("New Project ready") -} diff --git a/types.go b/types.go new file mode 100644 index 0000000..aead43d --- /dev/null +++ b/types.go @@ -0,0 +1,40 @@ +package levellogger + +import ( + "log" +) + +// Log Level Importance +// Fatal One or more key business functionalities are not working and the whole system doesn’t 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. +// 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. + +type LogLevel int + +const ( + Panic LogLevel = iota + Trace + Debug + Info + Warn + Error + Fatal +) + +// type LevelLogger interface { +// NewLevelLogger(out io.Writer, prefix string, flag int) *log.Logger +// } + +type LevelLogger struct { + CurrentLevel LogLevel + panicLog *log.Logger + traceLog *log.Logger + debugLog *log.Logger + infoLog *log.Logger + warnLog *log.Logger + errorLog *log.Logger + fatalLog *log.Logger +}