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

🚀 Consistent way of logging and fix middleware log format #2432

Merged

Conversation

kousikmitra
Copy link
Contributor

@kousikmitra kousikmitra commented Apr 24, 2023

Description

  • Replace all fmt.Print* with log.Print*
  • Use [MIDDLEWARENAME] - [LOG-LEVEL] text in all middleware for logging

Fixes #2402

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

@welcome
Copy link

welcome bot commented Apr 24, 2023

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@kousikmitra kousikmitra changed the title Feature/consistent way of log 🚀 Consistent way of logging and fix middleware log format Apr 24, 2023
@efectn
Copy link
Member

efectn commented Apr 24, 2023

We should add interface like https://github.com/labstack/echo/blob/master/log.go#L11 to make it customizable (maybe smaller). For example i may want to use zerolog, zap with fiber

@kousikmitra
Copy link
Contributor Author

We should add interface like https://github.com/labstack/echo/blob/master/log.go#L11 to make it customizable (maybe smaller). For example i may want to use zerolog, zap with fiber

That would be a good addition. I can work on it. But I think a separate PR would be better instead of adding here.

@efectn
Copy link
Member

efectn commented Apr 24, 2023

We should add interface like https://github.com/labstack/echo/blob/master/log.go#L11 to make it customizable (maybe smaller). For example i may want to use zerolog, zap with fiber

That would be a good addition. I can work on it. But I think a separate PR would be better instead of adding here.

Ok to me

@leonklingele
Copy link
Member

leonklingele commented Apr 24, 2023

I'd prefer [LEVEL] [SCOPE, e.g. MIDDLEWARE_NAME] [MESSAGE] [DETAILS] over having [SCOPE] [LEVEL] ...

Why don't we use a proper logging library like zerolog or zap though?

@efectn
Copy link
Member

efectn commented Apr 24, 2023

I'd prefer [LEVEL] [SCOPE, e.g. MIDDLEWARE_NAME] [MESSAGE] [DETAILS] over having [SCOPE] [LEVEL] ...

Why don't we use a proper logging library like zerolog or zap though?

We shouldn't add extra dependencies for optional things

@kousikmitra
Copy link
Contributor Author

Totally agree with @efectn , I have opened an issue to track this. Please feel free to add additional context.

@gaby
Copy link
Member

gaby commented Apr 30, 2023

I agree with @leonklingele it should be [LEVEL] [MIDDLEWARE_NAME] [MESSAGE]. It looks weird to have the level after the middleware name

@leonklingele
Copy link
Member

leonklingele commented Apr 30, 2023 via email

@efectn
Copy link
Member

efectn commented Apr 30, 2023

ftr: this‘ll soon be part of the Go stdlib 🫨🥳🚀

https://pkg.go.dev/golang.org/x/exp/slog

Seems slower than zerolog and zap. At least for now https://github.com/uber-go/zap#performance

@kousikmitra
Copy link
Contributor Author

I agree with @leonklingele it should be [LEVEL] [MIDDLEWARE_NAME] [MESSAGE]. It looks weird to have the level after the middleware name

Ya agree, if others agree I will make the change here.

@ReneWerner87 ReneWerner87 merged commit a59d9ba into gofiber:master May 1, 2023
@welcome
Copy link

welcome bot commented May 1, 2023

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@leonklingele
Copy link
Member

leonklingele commented May 1, 2023 via email

@ReneWerner87
Copy link
Member

Missed it, will revert it

next time it is better to store a task in the pull request or to reject it first, then I usually check it first
but without this it is quite hard for me to keep track of the 10 repositories with all the communication in the pull requests and issues

ReneWerner87 added a commit that referenced this pull request May 1, 2023
@leonklingele
Copy link
Member

leonklingele commented May 1, 2023 via email

ReneWerner87 added a commit that referenced this pull request May 1, 2023
@ReneWerner87
Copy link
Member

#2444

ReneWerner87 added a commit that referenced this pull request May 1, 2023
@ReneWerner87
Copy link
Member

We should add interface like https://github.com/labstack/echo/blob/master/log.go#L11 to make it customizable (maybe smaller). For example i may want to use zerolog, zap with fiber

what do you think about this kind of solution

package main

import (
	"fmt"
	"log"
)

type Logger struct {
	topic string
}

func NewLogger(topic string) *Logger {
	return &Logger{topic: topic}
}

func (l *Logger) formatMessage(level, format string, args ...interface{}) string {
	message := fmt.Sprintf(format, args...)
	if l.topic != "" {
		return fmt.Sprintf("[%s] - [%s] %s", level, l.topic, message)
	}
	return fmt.Sprintf("[%s] - %s", level, message)
}

func (l *Logger) Info(format string, args ...interface{}) {
	log.Println(l.formatMessage("Info", format, args...))
}

func (l *Logger) Warning(format string, args ...interface{}) {
	log.Println(l.formatMessage("Warning", format, args...))
}

func (l *Logger) Error(format string, args ...interface{}) {
	log.Println(l.formatMessage("Error", format, args...))
}

func main() {
	cacheLogger := NewLogger("CACHE")
	cacheLogger.Warning("Key is deprecated, please use KeyGenerator")

	corsLogger := NewLogger("CORS")
	corsLogger.Warning("Both 'AllowOrigins' and 'AllowOriginsFunc' have been defined.")

	csrfLogger := NewLogger("CSRF")
	csrfLogger.Warning("TokenLookup is deprecated, please use KeyLookup")

	idempotencyLogger := NewLogger("IDEMPOTENCY")
	key := "example_key"
	err := fmt.Errorf("an error occurred")
	idempotencyLogger.Error("failed to unlock key %q: %v", key, err)

	generalLogger := NewLogger("")
	generalLogger.Warning("Parse() is deprecated, please use Load() instead.")
}

or we use the slog lib
https://pkg.go.dev/golang.org/x/exp/slog
the idea was from @leonklingele

@kousikmitra kousikmitra deleted the feature/consistent-way-of-log branch May 2, 2023 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 [Feature]: Consistent way of issuing fiber log messages / warnings / errors
5 participants