From 7a43c47f30c29668a7bd0ef0c43bc21931ffc312 Mon Sep 17 00:00:00 2001 From: Javier Marcos <1271349+javuto@users.noreply.github.com> Date: Thu, 27 Jan 2022 16:21:42 +0100 Subject: [PATCH] Adding logger that does not log anything --- logging/logging.go | 23 +++++++++++++++++++++ logging/none.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 logging/none.go diff --git a/logging/logging.go b/logging/logging.go index 55ecd188..4b3fd50b 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -66,6 +66,13 @@ func CreateLoggerTLS(logging, loggingFile string, mgr *settings.Settings, nodes } d.Settings(mgr) l.Logger = d + case settings.LoggingNone: + d, err := CreateLoggerNone() + if err != nil { + return nil, err + } + d.Settings(mgr) + l.Logger = d } // Initialize the DB logger anyway return l, nil @@ -114,6 +121,14 @@ func (logTLS *LoggerTLS) Log(logType string, data []byte, environment, uuid stri if l.Enabled { l.Log(logType, data, environment, uuid, debug) } + case settings.LoggingNone: + l, ok := logTLS.Logger.(LoggerNone) + if !ok { + log.Printf("error casting logger to %s", settings.LoggingNone) + } + if l.Enabled { + l.Log(logType, data, environment, uuid, debug) + } } } @@ -160,5 +175,13 @@ func (logTLS *LoggerTLS) QueryLog(logType string, data []byte, environment, uuid if l.Enabled { l.Query(data, environment, uuid, name, status, debug) } + case settings.LoggingNone: + l, ok := logTLS.Logger.(LoggerNone) + if !ok { + log.Printf("error casting logger to %s", settings.LoggingNone) + } + if l.Enabled { + l.Query(data, environment, uuid, name, status, debug) + } } } diff --git a/logging/none.go b/logging/none.go new file mode 100644 index 00000000..c7e2e243 --- /dev/null +++ b/logging/none.go @@ -0,0 +1,51 @@ +package logging + +import ( + "log" + + "github.com/jmpsec/osctrl/settings" + "github.com/jmpsec/osctrl/types" +) + +// LoggerNone will be used to not log any data +type LoggerNone struct { + Enabled bool +} + +// CreateLoggerNone to initialize the logger +func CreateLoggerNone() (*LoggerNone, error) { + return &LoggerNone{Enabled: true}, nil +} + +// Settings - Function to prepare settings for the logger +func (logNone *LoggerNone) Settings(mgr *settings.Settings) { + log.Printf("No none logging settings\n") +} + +// Log - Function that sends JSON result/status/query logs to stdout +func (logNone *LoggerNone) Log(logType string, data []byte, environment, uuid string, debug bool) { + if debug { + log.Printf("Sending %d bytes to none for %s - %s", len(data), environment, uuid) + } + switch logType { + case types.StatusLog: + logNone.Status(data, environment, uuid, debug) + case types.ResultLog: + logNone.Result(data, environment, uuid, debug) + } +} + +// Status - Function that sends JSON status logs to stdout +func (logNone *LoggerNone) Status(data []byte, environment, uuid string, debug bool) { + log.Printf("Skipping to log %d bytes of status from %s/%s", len(data), environment, uuid) +} + +// Result - Function that sends JSON result logs to stdout +func (logNone *LoggerNone) Result(data []byte, environment, uuid string, debug bool) { + log.Printf("Skipping to log %d bytes of result from %s/%s", len(data), environment, uuid) +} + +// Query - Function that sends JSON query logs to stdout +func (logNone *LoggerNone) Query(data []byte, environment, uuid, name string, status int, debug bool) { + log.Printf("Skipping to log %d bytes of query from %s/%s for query %s and status %d", len(data), environment, uuid, name, status) +}