BAMS initial project structure
This commit is contained in:
60
backend/internal/logger/logger.go
Normal file
60
backend/internal/logger/logger.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
level string
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
func New(level string) *Logger {
|
||||
return &Logger{
|
||||
level: level,
|
||||
log: log.New(os.Stdout, "[BAMS] ", log.LstdFlags|log.Lshortfile),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Info(msg string, fields ...interface{}) {
|
||||
if l.shouldLog("info") {
|
||||
l.log.Printf("[INFO] %s %v", msg, fields)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Error(msg string, fields ...interface{}) {
|
||||
if l.shouldLog("error") {
|
||||
l.log.Printf("[ERROR] %s %v", msg, fields)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Debug(msg string, fields ...interface{}) {
|
||||
if l.shouldLog("debug") {
|
||||
l.log.Printf("[DEBUG] %s %v", msg, fields)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Warn(msg string, fields ...interface{}) {
|
||||
if l.shouldLog("warn") {
|
||||
l.log.Printf("[WARN] %s %v", msg, fields)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) shouldLog(level string) bool {
|
||||
levels := map[string]int{
|
||||
"debug": 0,
|
||||
"info": 1,
|
||||
"warn": 2,
|
||||
"error": 3,
|
||||
}
|
||||
currentLevel, ok := levels[l.level]
|
||||
if !ok {
|
||||
currentLevel = 1
|
||||
}
|
||||
msgLevel, ok := levels[level]
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
return msgLevel >= currentLevel
|
||||
}
|
||||
Reference in New Issue
Block a user