Skip to content

Commit

Permalink
Adding logger that does not log anything
Browse files Browse the repository at this point in the history
  • Loading branch information
javuto committed Jan 27, 2022
1 parent b3d435f commit 7a43c47
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
23 changes: 23 additions & 0 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -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)
}
}
}
51 changes: 51 additions & 0 deletions logging/none.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 7a43c47

Please sign in to comment.