ncDocConverter/pkg/logger/logger.go

199 lines
4.2 KiB
Go
Raw Normal View History

2022-09-15 12:54:38 +00:00
package logger
import (
"fmt"
"log"
"os"
2022-09-15 12:54:38 +00:00
"runtime"
"strconv"
"strings"
"time"
)
// Level of the log message
2022-09-15 12:54:38 +00:00
type Level uint8
2022-09-15 12:54:38 +00:00
const (
LevelDebug Level = iota
2022-09-15 12:54:38 +00:00
LevelInfo
LevelWarning
LevelError
LevelFatal
)
type Logger struct {
PrintLevel Level
LogLevel Level
LogFilePath string
PrintSource bool
consoleLogger *log.Logger
consoleLoggerErr *log.Logger
fileLogger *log.Logger
logFile *os.File
2022-09-15 12:54:38 +00:00
}
var dLogger Logger
func init() {
dLogger = Logger{
PrintLevel: LevelDebug,
LogLevel: LevelInfo,
2022-09-15 12:54:38 +00:00
LogFilePath: "",
PrintSource: false,
}
dLogger.setup()
}
func (l Logger) Log(level Level, message string, parameters ...any) {
// this function is needed that runtime.Caller(2) is always correct (even on direct call)
l.log(level, message, parameters...)
}
func (l Logger) log(level Level, message string, parameters ...any) {
pc, file, line, ok := runtime.Caller(3)
if !ok {
2022-09-15 12:54:38 +00:00
file = "#unknown"
line = 0
}
// get the name of the level
var levelName string
switch level {
case LevelDebug:
levelName = "DEBUG"
case LevelInfo:
levelName = "INFO "
case LevelWarning:
levelName = "WARN "
case LevelError:
levelName = "ERROR"
case LevelFatal:
levelName = "FATAL"
2022-09-15 12:54:38 +00:00
}
if levelName == "" {
2022-09-15 12:54:38 +00:00
message = fmt.Sprintf("Invalid level value given: %d. Original message: ", level) + message
levelName = "WARN "
level = LevelWarning
}
printMessage := "[" + levelName + "] " + time.Now().Local().Format("2006-01-02 15:04:05") +
getSourceMessage(file, line, pc, l) +
2022-09-15 12:54:38 +00:00
fmt.Sprintf(message, parameters...)
if l.LogLevel <= level && l.fileLogger != nil {
2022-09-15 12:54:38 +00:00
l.fileLogger.Println(printMessage)
l.logFile.Sync()
if level == LevelFatal {
2022-09-15 12:54:38 +00:00
l.CloseFile()
}
}
if l.PrintLevel <= level {
if level == LevelError {
2022-09-15 12:54:38 +00:00
l.consoleLoggerErr.Println(printMessage)
} else if level == LevelFatal {
2022-09-15 12:54:38 +00:00
l.consoleLoggerErr.Fatal(printMessage)
} else {
l.consoleLogger.Println(printMessage)
}
}
}
func getSourceMessage(file string, line int, pc uintptr, l Logger) string {
if !l.PrintSource {
2022-09-15 12:54:38 +00:00
return " - "
}
fileName := file[strings.LastIndex(file, "/")+1:] + ":" + strconv.Itoa(line)
return " (" + fileName + ") - "
}
func (l *Logger) setup() {
// log.Ldate|log.Ltime|log.Lshortfile
2022-09-15 12:54:38 +00:00
l.consoleLogger = log.New(os.Stdout, "", 0)
l.consoleLoggerErr = log.New(os.Stderr, "", 0)
if strings.TrimSpace(l.LogFilePath) != "" {
2022-09-15 12:54:38 +00:00
file, err := os.OpenFile(l.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err == nil {
l.fileLogger = log.New(file, "", 0)
l.logFile = file
} else {
l.Log(LevelError, fmt.Sprintf("Cannot access the log file '%s'\n%s", l.LogFilePath, err.Error()))
}
} else {
l.fileLogger = nil
if l.logFile != nil {
2022-09-15 12:54:38 +00:00
l.logFile.Close()
l.logFile = nil
}
}
}
func (l *Logger) CloseFile() {
if l.logFile != nil {
l.logFile.Close()
l.logFile = nil
l.fileLogger = nil
2022-09-15 12:54:38 +00:00
}
}
func SetGlobalLogger(l *Logger) {
dLogger = *l
dLogger.setup()
}
func GetGlobalLogger() *Logger {
return &dLogger
}
2022-09-15 12:54:38 +00:00
func Debug(message string, parameters ...any) {
dLogger.Log(LevelDebug, message, parameters...)
}
func Info(message string, parameters ...any) {
dLogger.Log(LevelInfo, message, parameters...)
}
func Warning(message string, parameters ...any) {
dLogger.Log(LevelWarning, message, parameters...)
}
func Error(message string, parameters ...any) {
dLogger.Log(LevelError, message, parameters...)
}
func Fatal(message string, parameters ...any) {
dLogger.Log(LevelFatal, message, parameters...)
}
func CloseFile() {
dLogger.CloseFile()
}
// Tries to convert the given level name to the corresponding level code.
// Allowed values are: 'debug', 'info', 'warn', 'warning', 'error', 'panic' and 'fatal'
// If an incorrect level name was given an warning is logged and info will be returned
func GetLevelByName(levelName string) Level {
levelName = strings.ToLower(levelName)
switch levelName {
case "debug":
return LevelDebug
case "info":
return LevelInfo
case "warn", "warning":
return LevelWarning
case "error":
return LevelError
case "panic", "fatal":
return LevelFatal
default:
{
Warning("Unable to parse the level name '%s'. Expected 'debug', 'info', 'warn', 'error' or 'fatal'", levelName)
return LevelInfo
}
}
}