2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-16 03:55:57 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package logger // import "miniflux.app/v2/internal/logger"
|
2017-12-16 03:55:57 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-06-09 02:16:12 +02:00
|
|
|
"time"
|
2017-12-16 03:55:57 +01:00
|
|
|
)
|
|
|
|
|
2018-01-30 05:34:00 +01:00
|
|
|
var requestedLevel = InfoLevel
|
2019-06-09 02:16:12 +02:00
|
|
|
var displayDateTime = false
|
2018-01-30 05:34:00 +01:00
|
|
|
|
2018-01-30 05:46:01 +01:00
|
|
|
// LogLevel type.
|
2018-01-30 05:34:00 +01:00
|
|
|
type LogLevel uint32
|
|
|
|
|
|
|
|
const (
|
2018-01-30 05:46:01 +01:00
|
|
|
// FatalLevel should be used in fatal situations, the app will exit.
|
2018-01-30 05:34:00 +01:00
|
|
|
FatalLevel LogLevel = iota
|
|
|
|
|
2018-01-30 05:46:01 +01:00
|
|
|
// ErrorLevel should be used when someone should really look at the error.
|
2018-01-30 05:34:00 +01:00
|
|
|
ErrorLevel
|
|
|
|
|
2018-01-30 05:46:01 +01:00
|
|
|
// InfoLevel should be used during normal operations.
|
2018-01-30 05:34:00 +01:00
|
|
|
InfoLevel
|
|
|
|
|
2018-01-30 05:46:01 +01:00
|
|
|
// DebugLevel should be used only during development.
|
2018-01-30 05:34:00 +01:00
|
|
|
DebugLevel
|
|
|
|
)
|
|
|
|
|
|
|
|
func (level LogLevel) String() string {
|
|
|
|
switch level {
|
|
|
|
case DebugLevel:
|
|
|
|
return "DEBUG"
|
|
|
|
case InfoLevel:
|
|
|
|
return "INFO"
|
|
|
|
case ErrorLevel:
|
|
|
|
return "ERROR"
|
|
|
|
case FatalLevel:
|
|
|
|
return "FATAL"
|
2018-01-30 05:46:01 +01:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
2018-01-30 05:34:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 02:16:12 +02:00
|
|
|
// EnableDateTime enables date time in log messages.
|
|
|
|
func EnableDateTime() {
|
|
|
|
displayDateTime = true
|
|
|
|
}
|
|
|
|
|
2018-01-30 05:34:00 +01:00
|
|
|
// EnableDebug increases logging, more verbose (debug)
|
|
|
|
func EnableDebug() {
|
|
|
|
requestedLevel = DebugLevel
|
|
|
|
formatMessage(InfoLevel, "Debug mode enabled")
|
|
|
|
}
|
|
|
|
|
2017-12-16 03:55:57 +01:00
|
|
|
// Debug sends a debug log message.
|
|
|
|
func Debug(format string, v ...interface{}) {
|
2018-01-30 05:34:00 +01:00
|
|
|
if requestedLevel >= DebugLevel {
|
|
|
|
formatMessage(DebugLevel, format, v...)
|
|
|
|
}
|
2017-12-16 03:55:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info sends an info log message.
|
|
|
|
func Info(format string, v ...interface{}) {
|
2018-01-30 05:34:00 +01:00
|
|
|
if requestedLevel >= InfoLevel {
|
|
|
|
formatMessage(InfoLevel, format, v...)
|
|
|
|
}
|
2017-12-16 03:55:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error sends an error log message.
|
|
|
|
func Error(format string, v ...interface{}) {
|
2018-01-30 05:34:00 +01:00
|
|
|
if requestedLevel >= ErrorLevel {
|
|
|
|
formatMessage(ErrorLevel, format, v...)
|
|
|
|
}
|
2017-12-16 03:55:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal sends a fatal log message and stop the execution of the program.
|
|
|
|
func Fatal(format string, v ...interface{}) {
|
2018-01-30 05:34:00 +01:00
|
|
|
if requestedLevel >= FatalLevel {
|
|
|
|
formatMessage(FatalLevel, format, v...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-12-16 03:55:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 05:34:00 +01:00
|
|
|
func formatMessage(level LogLevel, format string, v ...interface{}) {
|
2019-06-09 02:16:12 +02:00
|
|
|
var prefix string
|
|
|
|
|
|
|
|
if displayDateTime {
|
|
|
|
prefix = fmt.Sprintf("[%s] [%s] ", time.Now().Format("2006-01-02T15:04:05"), level)
|
|
|
|
} else {
|
|
|
|
prefix = fmt.Sprintf("[%s] ", level)
|
|
|
|
}
|
|
|
|
|
2017-12-16 03:55:57 +01:00
|
|
|
fmt.Fprintf(os.Stderr, prefix+format+"\n", v...)
|
|
|
|
}
|