first code version
This commit is contained in:
parent
7c9497f17e
commit
abc42ac847
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module git.bjphoster.com/b.pedini/go-basic-logger
|
||||
|
||||
go 1.17
|
82
logger.go
Normal file
82
logger.go
Normal file
@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2022 Bryan Joshua Pedini
|
||||
// License: GPL-3 · See LICENSE for more details
|
||||
package gobasiclogger
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
logLevel int
|
||||
}
|
||||
|
||||
func (l *Logger) Initialize(level *string) {
|
||||
if level == nil {
|
||||
*level = os.Getenv("LOGLEVEL")
|
||||
}
|
||||
if _stringInArray(*level, []string{"debug", "DEBUG", "dbg", "DBG"}) {
|
||||
l.logLevel = 7
|
||||
} else if _stringInArray(*level, []string{"notice", "NOTICE", "not", "NOT"}) {
|
||||
l.logLevel = 6
|
||||
} else if _stringInArray(*level, []string{"info", "INFO", "inf", "INF"}) {
|
||||
l.logLevel = 5
|
||||
} else if _stringInArray(*level, []string{"warning", "WARNING", "warn", "WARN", "wrn", "WRN"}) {
|
||||
l.logLevel = 4
|
||||
} else if _stringInArray(*level, []string{"error", "ERROR", "err", "ERR"}) {
|
||||
l.logLevel = 3
|
||||
} else if _stringInArray(*level, []string{"critical", "CRITICAL", "crt", "CRT"}) {
|
||||
l.logLevel = 2
|
||||
} else if _stringInArray(*level, []string{"fatal", "FATAL", "fat", "FAT"}) {
|
||||
l.logLevel = 1
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Debug(i ...interface{}) {
|
||||
i = append([]interface{}{"[DBG]"}, i...)
|
||||
if l.logLevel > 6 {
|
||||
log.Println(i...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Notice(i ...interface{}) {
|
||||
i = append([]interface{}{"[NOT]"}, i...)
|
||||
if l.logLevel > 5 {
|
||||
log.Println(i...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Info(i ...interface{}) {
|
||||
i = append([]interface{}{"[INF]"}, i...)
|
||||
if l.logLevel > 4 {
|
||||
log.Println(i...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Warning(i ...interface{}) {
|
||||
i = append([]interface{}{"[WRN]"}, i...)
|
||||
if l.logLevel > 3 {
|
||||
log.Println(i...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Error(i ...interface{}) {
|
||||
i = append([]interface{}{"[ERR]"}, i...)
|
||||
if l.logLevel > 2 {
|
||||
log.Println(i...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Fatal(i ...interface{}) {
|
||||
i = append([]interface{}{"[FAT]"}, i...)
|
||||
if l.logLevel > 1 {
|
||||
log.Println(i...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Critical(i ...interface{}) {
|
||||
i = append([]interface{}{"[CRT]"}, i...)
|
||||
if l.logLevel > 0 {
|
||||
log.Println(i...)
|
||||
}
|
||||
}
|
14
stringinarray.go
Normal file
14
stringinarray.go
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright (c) 2022 Bryan Joshua Pedini
|
||||
// License: GPL-3 · See LICENSE for more details
|
||||
package gobasiclogger
|
||||
|
||||
import "strings"
|
||||
|
||||
func _stringInArray(s string, l []string) bool {
|
||||
for _, e := range l {
|
||||
if strings.Compare(s, e) == 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Loading…
Reference in New Issue
Block a user