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 logging example for the different levels. #22

Open
sethlinnkuleuven opened this issue Aug 16, 2024 · 3 comments
Open

Add logging example for the different levels. #22

sethlinnkuleuven opened this issue Aug 16, 2024 · 3 comments
Labels
area/documentation Improvements or additions to documentation kind/enhancement a new or improved feature. status/2-needs-review

Comments

@sethlinnkuleuven
Copy link

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

@sebinsua
Copy link

sebinsua commented Sep 4, 2024

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...)
}

@sethlinnkuleuven
Copy link
Author

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.

@nmengin nmengin added kind/enhancement a new or improved feature. status/2-needs-review area/documentation Improvements or additions to documentation labels Oct 10, 2024
@nmengin
Copy link

nmengin commented Oct 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/documentation Improvements or additions to documentation kind/enhancement a new or improved feature. status/2-needs-review
Projects
None yet
Development

No branches or pull requests

3 participants