Add configuration options for log level

This commit is contained in:
kolaente 2020-04-12 22:32:21 +02:00
parent 9559a68416
commit 0bfb3a4709
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 40 additions and 43 deletions

View File

@ -106,14 +106,14 @@ log:
path: <rootpath>logs path: <rootpath>logs
# Whether to show any logging at all or none # Whether to show any logging at all or none
enabled: true enabled: true
# Where the error log should go. Possible values are stdout, stderr, file or off to disable error logging.
errors: "stdout"
# Where the normal log should go. Possible values are stdout, stderr, file or off to disable standard logging. # Where the normal log should go. Possible values are stdout, stderr, file or off to disable standard logging.
standard: "stdout" standard: "stdout"
# Change the log level. Possible values (case-insensitive) are CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG.
level: "INFO"
# Whether or not to log database queries. Useful for debugging. Possible values are stdout, stderr, file or off to disable database logging. # Whether or not to log database queries. Useful for debugging. Possible values are stdout, stderr, file or off to disable database logging.
database: "off" database: "off"
# The log level for database log messages. Possible values are CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG. # The log level for database log messages. Possible values (case-insensitive) are CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG.
databaselevel: "DEBUG" databaselevel: "WARNING"
# Whether to log http requests or not. Possible values are stdout, stderr, file or off to disable http logging. # Whether to log http requests or not. Possible values are stdout, stderr, file or off to disable http logging.
http: "stdout" http: "stdout"
# Echo has its own logging which usually is unnessecary, which is why it is disabled by default. Possible values are stdout, stderr, file or off to disable standard logging. # Echo has its own logging which usually is unnessecary, which is why it is disabled by default. Possible values are stdout, stderr, file or off to disable standard logging.

View File

@ -149,14 +149,14 @@ log:
path: <rootpath>logs path: <rootpath>logs
# Whether to show any logging at all or none # Whether to show any logging at all or none
enabled: true enabled: true
# Where the error log should go. Possible values are stdout, stderr, file or off to disable error logging.
errors: "stdout"
# Where the normal log should go. Possible values are stdout, stderr, file or off to disable standard logging. # Where the normal log should go. Possible values are stdout, stderr, file or off to disable standard logging.
standard: "stdout" standard: "stdout"
# Change the log level. Possible values (case-insensitive) are CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG.
level: "INFO"
# Whether or not to log database queries. Useful for debugging. Possible values are stdout, stderr, file or off to disable database logging. # Whether or not to log database queries. Useful for debugging. Possible values are stdout, stderr, file or off to disable database logging.
database: "off" database: "off"
# The log level for database log messages. Possible values are CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG. # The log level for database log messages. Possible values (case-insensitive) are CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG.
databaselevel: "DEBUG" databaselevel: "WARNING"
# Whether to log http requests or not. Possible values are stdout, stderr, file or off to disable http logging. # Whether to log http requests or not. Possible values are stdout, stderr, file or off to disable http logging.
http: "stdout" http: "stdout"
# Echo has its own logging which usually is unnessecary, which is why it is disabled by default. Possible values are stdout, stderr, file or off to disable standard logging. # Echo has its own logging which usually is unnessecary, which is why it is disabled by default. Possible values are stdout, stderr, file or off to disable standard logging.

View File

@ -79,8 +79,8 @@ const (
RedisDB Key = `redis.db` RedisDB Key = `redis.db`
LogEnabled Key = `log.enabled` LogEnabled Key = `log.enabled`
LogErrors Key = `log.errors`
LogStandard Key = `log.standard` LogStandard Key = `log.standard`
LogLevel Key = `log.level`
LogDatabase Key = `log.database` LogDatabase Key = `log.database`
LogDatabaseLevel Key = `log.databaselevel` LogDatabaseLevel Key = `log.databaselevel`
LogHTTP Key = `log.http` LogHTTP Key = `log.http`
@ -212,10 +212,10 @@ func InitDefaultConfig() {
RedisDB.setDefault(0) RedisDB.setDefault(0)
// Logger // Logger
LogEnabled.setDefault(true) LogEnabled.setDefault(true)
LogErrors.setDefault("stdout")
LogStandard.setDefault("stdout") LogStandard.setDefault("stdout")
LogLevel.setDefault("INFO")
LogDatabase.setDefault("off") LogDatabase.setDefault("off")
LogDatabaseLevel.setDefault("DEBUG") LogDatabaseLevel.setDefault("WARNING")
LogHTTP.setDefault("stdout") LogHTTP.setDefault("stdout")
LogEcho.setDefault("off") LogEcho.setDefault("off")
LogPath.setDefault(ServiceRootpath.GetString() + "/logs") LogPath.setDefault(ServiceRootpath.GetString() + "/logs")

View File

@ -21,8 +21,8 @@ import (
"github.com/op/go-logging" "github.com/op/go-logging"
"github.com/spf13/viper" "github.com/spf13/viper"
"io" "io"
"log"
"os" "os"
"strings"
"time" "time"
) )
@ -35,14 +35,15 @@ const WebFmt = `${time_rfc3339_nano}: WEB ` + "\t" + `▶ ${remote_ip} ${id} ${m
// Fmt is the general log format // Fmt is the general log format
const Fmt = `%{color}%{time:` + time.RFC3339Nano + `}: %{level}` + "\t" + `▶ %{shortpkg}/%{shortfunc} %{id:03x}%{color:reset} %{message}` const Fmt = `%{color}%{time:` + time.RFC3339Nano + `}: %{level}` + "\t" + `▶ %{shortpkg}/%{shortfunc} %{id:03x}%{color:reset} %{message}`
const logModule = `vikunja`
// loginstance is the instance of the logger which is used under the hood to log // loginstance is the instance of the logger which is used under the hood to log
var logInstance = logging.MustGetLogger("vikunja") var logInstance = logging.MustGetLogger(logModule)
// InitLogger initializes the global log handler // InitLogger initializes the global log handler
func InitLogger() { func InitLogger() {
if !config.LogEnabled.GetBool() { if !config.LogEnabled.GetBool() {
// Disable all logging when loggin in general is disabled, overwriting everything a user might have set. // Disable all logging when loggin in general is disabled, overwriting everything a user might have set.
config.LogErrors.Set("off")
config.LogStandard.Set("off") config.LogStandard.Set("off")
config.LogDatabase.Set("off") config.LogDatabase.Set("off")
config.LogHTTP.Set("off") config.LogHTTP.Set("off")
@ -53,36 +54,30 @@ func InitLogger() {
// This show correct caller functions // This show correct caller functions
logInstance.ExtraCalldepth = 1 logInstance.ExtraCalldepth = 1
if config.LogErrors.GetString() == "file" || config.LogStandard.GetString() == "file" { if config.LogStandard.GetString() == "file" {
err := os.Mkdir(config.LogPath.GetString(), 0744) err := os.Mkdir(config.LogPath.GetString(), 0744)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
log.Fatal("Could not create log folder: ", err.Error()) Fatalf("Could not create log folder: %s", err.Error())
} }
} }
var logBackends []logging.Backend
// We define our two backends // We define our two backends
if config.LogStandard.GetString() != "off" { if config.LogStandard.GetString() != "off" {
stdWriter := GetLogWriter("standard") stdWriter := GetLogWriter("standard")
stdBackend := logging.NewLogBackend(stdWriter, "", 0)
// Set the standard backend level, err := logging.LogLevel(strings.ToUpper(config.LogLevel.GetString()))
logBackends = append(logBackends, logging.NewBackendFormatter(stdBackend, logging.MustStringFormatter(Fmt+"\n"))) if err != nil {
Fatalf("Error setting database log level: %s", err.Error())
}
logBackend := logging.NewLogBackend(stdWriter, "", 0)
backend := logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(Fmt+"\n"))
backendLeveled := logging.AddModuleLevel(backend)
backendLeveled.SetLevel(level, logModule)
logInstance.SetBackend(backendLeveled)
} }
if config.LogErrors.GetString() != "off" {
errWriter := GetLogWriter("error")
errBackend := logging.NewLogBackend(errWriter, "", 0)
// Only warnings and more severe messages should go to the error backend
errBackendLeveled := logging.AddModuleLevel(errBackend)
errBackendLeveled.SetLevel(logging.WARNING, "")
logBackends = append(logBackends, errBackendLeveled)
}
// Set our backends
logging.SetBackend(logBackends...)
} }
// GetLogWriter returns the writer to where the normal log goes, depending on the config // GetLogWriter returns the writer to where the normal log goes, depending on the config
@ -90,9 +85,10 @@ func GetLogWriter(logfile string) (writer io.Writer) {
writer = os.Stdout // Set the default case to prevent nil pointer panics writer = os.Stdout // Set the default case to prevent nil pointer panics
switch viper.GetString("log." + logfile) { switch viper.GetString("log." + logfile) {
case "file": case "file":
f, err := os.OpenFile(config.LogPath.GetString()+"/"+logfile+".log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) fullLogFilePath := config.LogPath.GetString() + "/" + logfile + ".log"
f, err := os.OpenFile(fullLogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
log.Fatal(err) Fatalf("Could not create logfile %s: %s", fullLogFilePath, err.Error())
} }
writer = f writer = f
case "stderr": case "stderr":
@ -114,7 +110,7 @@ func GetLogger() *logging.Logger {
// Debug is for debug messages // Debug is for debug messages
func Debug(args ...interface{}) { func Debug(args ...interface{}) {
logInstance.Debug(args) logInstance.Debug(args...)
} }
// Debugf is for debug messages // Debugf is for debug messages
@ -124,7 +120,7 @@ func Debugf(format string, args ...interface{}) {
// Info is for info messages // Info is for info messages
func Info(args ...interface{}) { func Info(args ...interface{}) {
logInstance.Info(args) logInstance.Info(args...)
} }
// Infof is for info messages // Infof is for info messages
@ -134,7 +130,7 @@ func Infof(format string, args ...interface{}) {
// Error is for error messages // Error is for error messages
func Error(args ...interface{}) { func Error(args ...interface{}) {
logInstance.Error(args) logInstance.Error(args...)
} }
// Errorf is for error messages // Errorf is for error messages
@ -144,7 +140,7 @@ func Errorf(format string, args ...interface{}) {
// Warning is for warning messages // Warning is for warning messages
func Warning(args ...interface{}) { func Warning(args ...interface{}) {
logInstance.Warning(args) logInstance.Warning(args...)
} }
// Warningf is for warning messages // Warningf is for warning messages
@ -154,7 +150,7 @@ func Warningf(format string, args ...interface{}) {
// Critical is for critical messages // Critical is for critical messages
func Critical(args ...interface{}) { func Critical(args ...interface{}) {
logInstance.Critical(args) logInstance.Critical(args...)
} }
// Criticalf is for critical messages // Criticalf is for critical messages
@ -164,7 +160,7 @@ func Criticalf(format string, args ...interface{}) {
// Fatal is for fatal messages // Fatal is for fatal messages
func Fatal(args ...interface{}) { func Fatal(args ...interface{}) {
logInstance.Fatal(args) logInstance.Fatal(args...)
} }
// Fatalf is for fatal messages // Fatalf is for fatal messages

View File

@ -19,6 +19,7 @@ package log
import ( import (
"code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/config"
"github.com/op/go-logging" "github.com/op/go-logging"
"strings"
"time" "time"
"xorm.io/core" "xorm.io/core"
"xorm.io/xorm/log" "xorm.io/xorm/log"
@ -38,7 +39,7 @@ type XormLogger struct {
// NewXormLogger creates and initializes a new xorm logger // NewXormLogger creates and initializes a new xorm logger
func NewXormLogger() *XormLogger { func NewXormLogger() *XormLogger {
level, err := logging.LogLevel(config.LogDatabaseLevel.GetString()) level, err := logging.LogLevel(strings.ToUpper(config.LogDatabaseLevel.GetString()))
if err != nil { if err != nil {
Critical("Error setting database log level: %s", err.Error()) Critical("Error setting database log level: %s", err.Error())
} }