plugindemo icon indicating copy to clipboard operation
plugindemo copied to clipboard

Add logging example for the different levels.

Open sethlinnkuleuven opened this issue 1 year ago • 3 comments

I think it would be nice if this module contained examples on the proper/ cleanest way to log messages at different levels(DEBUG, INFO, etc...) I noticed that even the standard Printf() turns into an error level in debug.log

log.Printf("Hello World") results in {"level":"error","module":.....................................

Thank you

sethlinnkuleuven avatar Aug 16 '24 10:08 sethlinnkuleuven

Did you figure out a way to achieve this?

Edit:

I'm only able to get 'debug' or 'error' logs and have done so with this:

package logger

import (
	"fmt"
	"log"
	"strings"
)

type Logger struct {
	_info   func(args ...interface{})
	_debug  func(args ...interface{})
	_error  func(args ...interface{})
	_infof  func(format string, args ...interface{})
	_debugf func(format string, args ...interface{})
	_errorf func(format string, args ...interface{})
}

func NewLogger(_logLevel string) *Logger {
	logLevel := strings.ToLower(_logLevel)
	// This globally sets the flags for the standard logger which is generally
	// a bad practice, however, since Traefik is capturing the output of the
	// logger and redirecting it to its own logger, this is the only way to
	// ensure that the error logs are not prefixed by the date and time, and
	// has no other side effects.
	log.SetFlags(0)

	logger := &Logger{
		_debug: func(args ...interface{}) {
			fmt.Println(args...)
		},
		_info: func(args ...interface{}) {
			fmt.Println(args...)
		},
		_error: func(args ...interface{}) {
			log.Println(args...)
		},
		_debugf: func(format string, args ...interface{}) {
			fmt.Printf(format+"\n", args...)
		},
		_infof: func(format string, args ...interface{}) {
			fmt.Printf(format+"\n", args...)
		},
		_errorf: func(format string, args ...interface{}) {
			log.Printf(format+"\n", args...)
		},
	}

	noopLog := func(args ...interface{}) {}
	noopLogf := func(format string, args ...interface{}) {}

	switch logLevel {
	default:
	case "error":
		logger._debug = noopLog
		logger._debugf = noopLogf
		logger._info = noopLog
		logger._infof = noopLogf
	case "info":
		logger._debug = noopLog
		logger._debugf = noopLogf
	case "debug":
		break
	}

	return logger
}

func (l *Logger) Debug(args ...interface{}) {
	l._debug(args...)
}

func (l *Logger) Info(args ...interface{}) {
	l._info(args...)
}

func (l *Logger) Error(args ...interface{}) {
	l._error(args...)
}

func (l *Logger) Debugf(format string, args ...interface{}) {
	l._debugf(format, args...)
}

func (l *Logger) Infof(format string, args ...interface{}) {
	l._infof(format, args...)
}

func (l *Logger) Errorf(format string, args ...interface{}) {
	l._errorf(format, args...)
}

sebinsua avatar Sep 04 '24 17:09 sebinsua

Wow! That is allot farther than I have gotten. I just comment or uncomment for nw. Was hoping someone at Traefik would pick this up.

sethlinnkuleuven avatar Sep 05 '24 11:09 sethlinnkuleuven

Hey @sethlinnkuleuven,

Thanks for reaching out.

We agree that it makes a lot of sense to add this information in the README.

Unfortunately, this would not make it to our roadmap for a while as we are focused elsewhere.

If you or another community member would like to build it, let us know, and we will work with you to make sure you have all the information needed so that it can be merged.

nmengin avatar Oct 10 '24 13:10 nmengin