fix: disabling logging completely now works

This commit is contained in:
kolaente 2022-02-19 17:42:32 +01:00
parent 8cb92b3924
commit 22e3f242a3
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
4 changed files with 52 additions and 16 deletions

View file

@ -389,10 +389,10 @@ func InitConfig() {
MetricsEnabled.Set(true) MetricsEnabled.Set(true)
} }
err = viper.ReadInConfig()
if viper.ConfigFileUsed() != "" { if viper.ConfigFileUsed() != "" {
log.Printf("Using config file: %s", viper.ConfigFileUsed()) log.Printf("Using config file: %s", viper.ConfigFileUsed())
err = viper.ReadInConfig()
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
log.Println("Using default config.") log.Println("Using default config.")

View file

@ -63,23 +63,26 @@ func InitLogger() {
} }
} }
// We define our two backends // The backend is the part which actually handles logging the log entries somewhere.
if config.LogStandard.GetString() != "off" { cf := config.LogStandard.GetString()
var backend logging.Backend
backend = &NoopBackend{}
if cf != "off" && cf != "false" {
stdWriter := GetLogWriter("standard") stdWriter := GetLogWriter("standard")
logBackend := logging.NewLogBackend(stdWriter, "", 0)
backend = logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(Fmt+"\n"))
}
level, err := logging.LogLevel(strings.ToUpper(config.LogLevel.GetString())) level, err := logging.LogLevel(strings.ToUpper(config.LogLevel.GetString()))
if err != nil { if err != nil {
Fatalf("Error setting database log level: %s", err.Error()) 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 := logging.AddModuleLevel(backend)
backendLeveled.SetLevel(level, logModule) backendLeveled.SetLevel(level, logModule)
logInstance.SetBackend(backendLeveled) logInstance.SetBackend(backendLeveled)
}
} }
// 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

28
pkg/log/noop.go Normal file
View file

@ -0,0 +1,28 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package log
import (
"github.com/op/go-logging"
)
// NoopBackend doesn't log anything. Used in cases where we want to disable logging completely.
type NoopBackend struct{}
func (n *NoopBackend) Log(level logging.Level, i int, record *logging.Record) error {
return nil
}

View file

@ -45,8 +45,13 @@ func NewWatermillLogger() *WatermillLogger {
logger: logging.MustGetLogger(watermillLogModule), logger: logging.MustGetLogger(watermillLogModule),
} }
cf := config.LogEvents.GetString()
var backend logging.Backend
backend = &NoopBackend{}
if cf != "off" && cf != "false" {
logBackend := logging.NewLogBackend(GetLogWriter("events"), "", 0) logBackend := logging.NewLogBackend(GetLogWriter("events"), "", 0)
backend := logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(watermillFmt+"\n")) backend = logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(watermillFmt+"\n"))
}
backendLeveled := logging.AddModuleLevel(backend) backendLeveled := logging.AddModuleLevel(backend)
backendLeveled.SetLevel(level, watermillLogModule) backendLeveled.SetLevel(level, watermillLogModule)