2017-12-15 21:55:57 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package logger // import "miniflux.app/logger"
|
2017-12-15 21:55:57 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-01-29 23:34:00 -05:00
|
|
|
var requestedLevel = InfoLevel
|
|
|
|
|
2018-01-29 23:46:01 -05:00
|
|
|
// LogLevel type.
|
2018-01-29 23:34:00 -05:00
|
|
|
type LogLevel uint32
|
|
|
|
|
|
|
|
const (
|
2018-01-29 23:46:01 -05:00
|
|
|
// FatalLevel should be used in fatal situations, the app will exit.
|
2018-01-29 23:34:00 -05:00
|
|
|
FatalLevel LogLevel = iota
|
|
|
|
|
2018-01-29 23:46:01 -05:00
|
|
|
// ErrorLevel should be used when someone should really look at the error.
|
2018-01-29 23:34:00 -05:00
|
|
|
ErrorLevel
|
|
|
|
|
2018-01-29 23:46:01 -05:00
|
|
|
// InfoLevel should be used during normal operations.
|
2018-01-29 23:34:00 -05:00
|
|
|
InfoLevel
|
|
|
|
|
2018-01-29 23:46:01 -05:00
|
|
|
// DebugLevel should be used only during development.
|
2018-01-29 23:34:00 -05: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-29 23:46:01 -05:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
2018-01-29 23:34:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableDebug increases logging, more verbose (debug)
|
|
|
|
func EnableDebug() {
|
|
|
|
requestedLevel = DebugLevel
|
|
|
|
formatMessage(InfoLevel, "Debug mode enabled")
|
|
|
|
}
|
|
|
|
|
2017-12-15 21:55:57 -05:00
|
|
|
// Debug sends a debug log message.
|
|
|
|
func Debug(format string, v ...interface{}) {
|
2018-01-29 23:34:00 -05:00
|
|
|
if requestedLevel >= DebugLevel {
|
|
|
|
formatMessage(DebugLevel, format, v...)
|
|
|
|
}
|
2017-12-15 21:55:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info sends an info log message.
|
|
|
|
func Info(format string, v ...interface{}) {
|
2018-01-29 23:34:00 -05:00
|
|
|
if requestedLevel >= InfoLevel {
|
|
|
|
formatMessage(InfoLevel, format, v...)
|
|
|
|
}
|
2017-12-15 21:55:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error sends an error log message.
|
|
|
|
func Error(format string, v ...interface{}) {
|
2018-01-29 23:34:00 -05:00
|
|
|
if requestedLevel >= ErrorLevel {
|
|
|
|
formatMessage(ErrorLevel, format, v...)
|
|
|
|
}
|
2017-12-15 21:55:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal sends a fatal log message and stop the execution of the program.
|
|
|
|
func Fatal(format string, v ...interface{}) {
|
2018-01-29 23:34:00 -05:00
|
|
|
if requestedLevel >= FatalLevel {
|
|
|
|
formatMessage(FatalLevel, format, v...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-12-15 21:55:57 -05:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:34:00 -05:00
|
|
|
func formatMessage(level LogLevel, format string, v ...interface{}) {
|
|
|
|
prefix := fmt.Sprintf("[%s] [%s] ", time.Now().Format("2006-01-02T15:04:05"), level.String())
|
2017-12-15 21:55:57 -05:00
|
|
|
fmt.Fprintf(os.Stderr, prefix+format+"\n", v...)
|
|
|
|
}
|