Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an interface for DebugLogger to be able to replace the logging me… #337

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,63 @@ func (level LogLevel) Invalid() bool {
return level < LogLevelInfo || level > LogLevelTrace
}

// DebugLogger is used to log SecDebugLog messages
type DebugLogger interface {
// Info logs an info message
Info(message string, args ...interface{})
// Warn logs a warning message
Warn(message string, args ...interface{})
// Error logs an error message
Error(message string, args ...interface{})
// Debug logs a debug message
Debug(message string, args ...interface{})
// Trace logs a trace message
Trace(message string, args ...interface{})
// SetLevel sets the log level
SetLevel(level LogLevel)
// SetOutput sets the output for the logger
SetOutput(w io.Writer)
}

// DebugLogger is a logger that logs to the standard logger
type DebugLogger struct {
type stdDebugLogger struct {
logger *log.Logger
Level LogLevel
}

func (l *DebugLogger) formatLog(level LogLevel, message string, args ...interface{}) {
func (l *stdDebugLogger) formatLog(level LogLevel, message string, args ...interface{}) {
if l.Level >= level {
l.logger.Printf("[%s] %s", level.String(), fmt.Sprintf(message, args...))
}
}

// Info logs an info message
func (l *DebugLogger) Info(message string, args ...interface{}) {
func (l *stdDebugLogger) Info(message string, args ...interface{}) {
l.formatLog(LogLevelInfo, message, args...)
}

// Warn logs a warning message
func (l *DebugLogger) Warn(message string, args ...interface{}) {
func (l *stdDebugLogger) Warn(message string, args ...interface{}) {
l.formatLog(LogLevelWarn, message, args...)
}

// Error logs an error message
func (l *DebugLogger) Error(message string, args ...interface{}) {
func (l *stdDebugLogger) Error(message string, args ...interface{}) {
l.formatLog(LogLevelError, message, args...)
}

// Debug logs a debug message
func (l *DebugLogger) Debug(message string, args ...interface{}) {
func (l *stdDebugLogger) Debug(message string, args ...interface{}) {
l.formatLog(LogLevelDebug, message, args...)
}

// Trace logs a trace message
func (l *DebugLogger) Trace(message string, args ...interface{}) {
func (l *stdDebugLogger) Trace(message string, args ...interface{}) {
l.formatLog(LogLevelTrace, message, args...)
}

// SetLevel sets the log level
func (l *DebugLogger) SetLevel(level LogLevel) {
func (l *stdDebugLogger) SetLevel(level LogLevel) {
if level.Invalid() {
l.Info("Invalid log level, defaulting to INFO")
level = LogLevelInfo
Expand All @@ -107,12 +125,12 @@ func (l *DebugLogger) SetLevel(level LogLevel) {
}

// SetOutput sets the output for the logger
func (l *DebugLogger) SetOutput(w io.Writer) {
func (l *stdDebugLogger) SetOutput(w io.Writer) {
l.logger.SetOutput(w)
}

// Close closes the logger
func (l *DebugLogger) Close() error {
func (l *stdDebugLogger) Close() error {
// TODO
return nil
}
4 changes: 2 additions & 2 deletions waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ type Waf struct {
ProducerConnectorVersion string

// Used for the debug logger
Logger *DebugLogger
Logger DebugLogger

errorLogCb ErrorLogCallback

Expand Down Expand Up @@ -439,7 +439,7 @@ func (w *Waf) SetDebugLogPath(path string) error {

// NewWaf creates a new WAF instance with default variables
func NewWaf() *Waf {
logger := &DebugLogger{
logger := &stdDebugLogger{
logger: &log.Logger{},
Level: LogLevelInfo,
}
Expand Down