From 22e3f242a396aa9cf54e9426077816f97a0da36f Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 19 Feb 2022 17:42:32 +0100 Subject: [PATCH] fix: disabling logging completely now works --- pkg/config/config.go | 2 +- pkg/log/logging.go | 29 ++++++++++++++++------------- pkg/log/noop.go | 28 ++++++++++++++++++++++++++++ pkg/log/watermill_logger.go | 9 +++++++-- 4 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 pkg/log/noop.go diff --git a/pkg/config/config.go b/pkg/config/config.go index 678b6efd1..0c139249b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -389,10 +389,10 @@ func InitConfig() { MetricsEnabled.Set(true) } + err = viper.ReadInConfig() if viper.ConfigFileUsed() != "" { log.Printf("Using config file: %s", viper.ConfigFileUsed()) - err = viper.ReadInConfig() if err != nil { log.Println(err.Error()) log.Println("Using default config.") diff --git a/pkg/log/logging.go b/pkg/log/logging.go index 3fb4701ba..5c5f9d7b9 100644 --- a/pkg/log/logging.go +++ b/pkg/log/logging.go @@ -63,23 +63,26 @@ func InitLogger() { } } - // We define our two backends - if config.LogStandard.GetString() != "off" { + // The backend is the part which actually handles logging the log entries somewhere. + cf := config.LogStandard.GetString() + var backend logging.Backend + backend = &NoopBackend{} + if cf != "off" && cf != "false" { stdWriter := GetLogWriter("standard") - level, err := logging.LogLevel(strings.ToUpper(config.LogLevel.GetString())) - 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) + backend = logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(Fmt+"\n")) } + + level, err := logging.LogLevel(strings.ToUpper(config.LogLevel.GetString())) + if err != nil { + Fatalf("Error setting database log level: %s", err.Error()) + } + + backendLeveled := logging.AddModuleLevel(backend) + backendLeveled.SetLevel(level, logModule) + + logInstance.SetBackend(backendLeveled) } // GetLogWriter returns the writer to where the normal log goes, depending on the config diff --git a/pkg/log/noop.go b/pkg/log/noop.go new file mode 100644 index 000000000..2824c40e6 --- /dev/null +++ b/pkg/log/noop.go @@ -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 . + +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 +} diff --git a/pkg/log/watermill_logger.go b/pkg/log/watermill_logger.go index 8f50c8586..1f49829cd 100644 --- a/pkg/log/watermill_logger.go +++ b/pkg/log/watermill_logger.go @@ -45,8 +45,13 @@ func NewWatermillLogger() *WatermillLogger { logger: logging.MustGetLogger(watermillLogModule), } - logBackend := logging.NewLogBackend(GetLogWriter("events"), "", 0) - backend := logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(watermillFmt+"\n")) + cf := config.LogEvents.GetString() + var backend logging.Backend + backend = &NoopBackend{} + if cf != "off" && cf != "false" { + logBackend := logging.NewLogBackend(GetLogWriter("events"), "", 0) + backend = logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(watermillFmt+"\n")) + } backendLeveled := logging.AddModuleLevel(backend) backendLeveled.SetLevel(level, watermillLogModule)