ncDocConverter/pkg/logger/logger.go

199 lines
4.2 KiB
Go
Raw Permalink Normal View History

2022-09-15 12:54:38 +00:00
package logger
import (
"fmt"
"log"
2023-01-02 10:06:13 +00:00
"os"
2022-09-15 12:54:38 +00:00
"runtime"
"strconv"
"strings"
"time"
)
2022-09-22 13:22:58 +00:00
// Level of the log message
2022-09-15 12:54:38 +00:00
type Level uint8
2023-01-02 10:06:13 +00:00
2022-09-15 12:54:38 +00:00
const (
2023-01-02 10:06:13 +00:00
LevelDebug Level = iota
2022-09-15 12:54:38 +00:00
LevelInfo
LevelWarning
LevelError
LevelFatal
)
type Logger struct {
2023-01-02 10:06:13 +00:00
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() {
2023-01-02 10:06:13 +00:00
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) {
2022-09-22 13:22:58 +00:00
pc, file, line, ok := runtime.Caller(3)
2023-01-02 10:06:13 +00:00
if !ok {
2022-09-15 12:54:38 +00:00
file = "#unknown"
line = 0
}
// get the name of the level
var levelName string
2023-01-02 10:06:13 +00:00
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
}
2023-01-02 10:06:13 +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
}
2023-01-02 10:06:13 +00:00
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...)
2023-01-02 10:06:13 +00:00
if l.LogLevel <= level && l.fileLogger != nil {
2022-09-15 12:54:38 +00:00
l.fileLogger.Println(printMessage)
l.logFile.Sync()
2023-01-02 10:06:13 +00:00
if level == LevelFatal {
2022-09-15 12:54:38 +00:00
l.CloseFile()
}
}
2023-01-02 10:06:13 +00:00
if l.PrintLevel <= level {
if level == LevelError {
2022-09-15 12:54:38 +00:00
l.consoleLoggerErr.Println(printMessage)
2023-01-02 10:06:13 +00:00
} else if level == LevelFatal {
2022-09-15 12:54:38 +00:00
l.consoleLoggerErr.Fatal(printMessage)
} else {
l.consoleLogger.Println(printMessage)
}
}
}
2023-01-02 10:06:13 +00:00
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() {
2022-09-22 13:22:58 +00:00
// 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)
2023-01-02 10:06:13 +00:00
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
2023-01-02 10:06:13 +00:00
if l.logFile != nil {
2022-09-15 12:54:38 +00:00
l.logFile.Close()
l.logFile = nil
}
}
}
func (l *Logger) CloseFile() {
2023-01-02 10:06:13 +00:00
if l.logFile != nil {
2022-09-22 13:22:58 +00:00
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()
}
2023-01-02 10:06:13 +00:00
func GetGlobalLogger() *Logger {
2022-09-22 13:22:58 +00:00
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()
2022-09-22 13:22:58 +00:00
}
// 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)
2023-01-02 10:06:13 +00:00
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:
{
2022-09-22 13:22:58 +00:00
Warning("Unable to parse the level name '%s'. Expected 'debug', 'info', 'warn', 'error' or 'fatal'", levelName)
return LevelInfo
}
}
2023-01-02 10:06:13 +00:00
}