Add flag to enable debug logging
This commit is contained in:
parent
40eb1b10fe
commit
24a2f472ec
2 changed files with 64 additions and 7 deletions
|
@ -8,6 +8,8 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/miniflux/miniflux/logger"
|
||||||
|
|
||||||
"github.com/miniflux/miniflux/config"
|
"github.com/miniflux/miniflux/config"
|
||||||
"github.com/miniflux/miniflux/daemon"
|
"github.com/miniflux/miniflux/daemon"
|
||||||
"github.com/miniflux/miniflux/storage"
|
"github.com/miniflux/miniflux/storage"
|
||||||
|
@ -22,6 +24,7 @@ func Parse() {
|
||||||
flagFlushSessions := flag.Bool("flush-sessions", false, "Flush all sessions (disconnect users)")
|
flagFlushSessions := flag.Bool("flush-sessions", false, "Flush all sessions (disconnect users)")
|
||||||
flagCreateAdmin := flag.Bool("create-admin", false, "Create admin user")
|
flagCreateAdmin := flag.Bool("create-admin", false, "Create admin user")
|
||||||
flagResetPassword := flag.Bool("reset-password", false, "Reset user password")
|
flagResetPassword := flag.Bool("reset-password", false, "Reset user password")
|
||||||
|
flagDebugMode := flag.Bool("debug", false, "Enable debug mode (more verbose output)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
cfg := config.NewConfig()
|
cfg := config.NewConfig()
|
||||||
|
@ -60,5 +63,10 @@ func Parse() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *flagDebugMode {
|
||||||
|
logger.EnableDebug()
|
||||||
|
}
|
||||||
|
|
||||||
|
// start daemon
|
||||||
daemon.Run(cfg, store)
|
daemon.Run(cfg, store)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,28 +10,77 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var requestedLevel = InfoLevel
|
||||||
|
|
||||||
|
// LogLevel type
|
||||||
|
type LogLevel uint32
|
||||||
|
|
||||||
|
const (
|
||||||
|
// FatalLevel should be used in fatal situations, the app will exit
|
||||||
|
FatalLevel LogLevel = iota
|
||||||
|
|
||||||
|
// ErrorLevel should be used when someone should really look at the error
|
||||||
|
ErrorLevel
|
||||||
|
|
||||||
|
// InfoLevel should be used during normal operations
|
||||||
|
InfoLevel
|
||||||
|
|
||||||
|
// DebugLevel should be used only during development
|
||||||
|
DebugLevel
|
||||||
|
)
|
||||||
|
|
||||||
|
// Convert the Level to a string.
|
||||||
|
func (level LogLevel) String() string {
|
||||||
|
switch level {
|
||||||
|
case DebugLevel:
|
||||||
|
return "DEBUG"
|
||||||
|
case InfoLevel:
|
||||||
|
return "INFO"
|
||||||
|
case ErrorLevel:
|
||||||
|
return "ERROR"
|
||||||
|
case FatalLevel:
|
||||||
|
return "FATAL"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "UNKNOWN"
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableDebug increases logging, more verbose (debug)
|
||||||
|
func EnableDebug() {
|
||||||
|
requestedLevel = DebugLevel
|
||||||
|
formatMessage(InfoLevel, "Debug mode enabled")
|
||||||
|
}
|
||||||
|
|
||||||
// Debug sends a debug log message.
|
// Debug sends a debug log message.
|
||||||
func Debug(format string, v ...interface{}) {
|
func Debug(format string, v ...interface{}) {
|
||||||
formatMessage("DEBUG", format, v...)
|
if requestedLevel >= DebugLevel {
|
||||||
|
formatMessage(DebugLevel, format, v...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info sends an info log message.
|
// Info sends an info log message.
|
||||||
func Info(format string, v ...interface{}) {
|
func Info(format string, v ...interface{}) {
|
||||||
formatMessage("INFO", format, v...)
|
if requestedLevel >= InfoLevel {
|
||||||
|
formatMessage(InfoLevel, format, v...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error sends an error log message.
|
// Error sends an error log message.
|
||||||
func Error(format string, v ...interface{}) {
|
func Error(format string, v ...interface{}) {
|
||||||
formatMessage("ERROR", format, v...)
|
if requestedLevel >= ErrorLevel {
|
||||||
|
formatMessage(ErrorLevel, format, v...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal sends a fatal log message and stop the execution of the program.
|
// Fatal sends a fatal log message and stop the execution of the program.
|
||||||
func Fatal(format string, v ...interface{}) {
|
func Fatal(format string, v ...interface{}) {
|
||||||
formatMessage("FATAL", format, v...)
|
if requestedLevel >= FatalLevel {
|
||||||
os.Exit(1)
|
formatMessage(FatalLevel, format, v...)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatMessage(level, format string, v ...interface{}) {
|
func formatMessage(level LogLevel, format string, v ...interface{}) {
|
||||||
prefix := fmt.Sprintf("[%s] [%s] ", time.Now().Format("2006-01-02T15:04:05"), level)
|
prefix := fmt.Sprintf("[%s] [%s] ", time.Now().Format("2006-01-02T15:04:05"), level.String())
|
||||||
fmt.Fprintf(os.Stderr, prefix+format+"\n", v...)
|
fmt.Fprintf(os.Stderr, prefix+format+"\n", v...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue