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 LoggerWithFormatter method #1677

Merged
merged 7 commits into from
Dec 12, 2018

Conversation

sairoutine
Copy link
Contributor

@sairoutine sairoutine commented Dec 1, 2018

Problems

I want to define gin's log format.
Currently, instead of using the default Logger, I can define the log format by defining my own Logger function.

However, my own Logger function must include redundant codes similar to the default Logger.

ref. #1581

Proposal

This PR solves the problem by adding new methods, LoggerWithFormatter and LoggerWithConfig.

example code

r := gin.New()
r.Use(Recovery())
r.Use(LoggerWithFormatter(func(param gin.LogFormatterParams) string {
	// my own format
	return fmt.Sprintf("[GIN] %v | %3d | %13v | %15s | %-7s  %s | %s\n%s",
		param.TimeStamp.Format("2006/01/02 - 15:04:05"),
		param.StatusCode,
		param.Latency,
		param.ClientIP,
		param.Method,
		param.Path,
		SomeMyOwnParam(param.Request), // username or payload information
		param.ErrorMessage,
	)
}))

refs.
gorilla's LogFormatter
http://www.gorillatoolkit.org/pkg/handlers#LogFormatter

echo's LoggerWithConfig
https://echo.labstack.com/middleware/logger

At end

If you prefers to this proposal, I will add unit tests and documents.

Thanks.

@codecov
Copy link

codecov bot commented Dec 1, 2018

Codecov Report

Merging #1677 into master will increase coverage by 0.2%.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #1677     +/-   ##
========================================
+ Coverage   99.19%   99.4%   +0.2%     
========================================
  Files          41      41             
  Lines        1995    2017     +22     
========================================
+ Hits         1979    2005     +26     
+ Misses         12       9      -3     
+ Partials        4       3      -1
Impacted Files Coverage Δ
logger.go 100% <100%> (+5.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cce4958...10b5f41. Read the comment docs.

@thinkerou
Copy link
Member

@sairoutine Hi, can you see the log middleware https://github.com/gin-contrib/logger ? thanks!

@sairoutine
Copy link
Contributor Author

@thinkerou
Yes, I've seen it but it is specialiced in rs/zerolog. this pull request provides a more general logging that the web application framework should have.

Also, by this pull request, Your suggested middleware will be the simpler code and it's redundant code will be eliminated.

Thanks.

@thinkerou
Copy link
Member

@sairoutine please commit the whole code which includes unit test and example code, thanks, and then we review.

@sairoutine
Copy link
Contributor Author

@thinkerou
Added unit test and README 👍
Please review. Thanks.

logger.go Show resolved Hide resolved
logger.go Outdated Show resolved Hide resolved
@thinkerou thinkerou added this to the 1.4 milestone Dec 9, 2018
@sairoutine sairoutine changed the title [Proposal] Add LoggerWithFormatter method Add LoggerWithFormatter method Dec 9, 2018
@thinkerou thinkerou merged commit f76ccb2 into gin-gonic:master Dec 12, 2018
@sairoutine sairoutine deleted the add_logger_with_config branch December 12, 2018 03:56
@sairoutine
Copy link
Contributor Author

Thank you for review and merge!

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.

3 participants