mirror of
https://github.com/gin-gonic/gin.git
synced 2025-05-22 20:30:44 +08:00
Merge 36920321ce6da29b65c5cb0123332f54a95d29c1 into ef68fa032c0e6ce637db56e89ec734c0de0a9f5e
This commit is contained in:
commit
76fd5c74b4
@ -37,7 +37,7 @@ var consoleColorMode = autoColor
|
|||||||
|
|
||||||
// LoggerConfig defines the config for Logger middleware.
|
// LoggerConfig defines the config for Logger middleware.
|
||||||
type LoggerConfig struct {
|
type LoggerConfig struct {
|
||||||
// Optional. Default value is gin.defaultLogFormatter
|
// Optional. Default value is gin.DefaultLogFormatter
|
||||||
Formatter LogFormatter
|
Formatter LogFormatter
|
||||||
|
|
||||||
// Output is a writer where logs are written.
|
// Output is a writer where logs are written.
|
||||||
@ -137,8 +137,8 @@ func (p *LogFormatterParams) IsOutputColor() bool {
|
|||||||
return consoleColorMode == forceColor || (consoleColorMode == autoColor && p.isTerm)
|
return consoleColorMode == forceColor || (consoleColorMode == autoColor && p.isTerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultLogFormatter is the default log format function Logger middleware uses.
|
// DefaultLogFormatter is the default log format function Logger middleware uses.
|
||||||
var defaultLogFormatter = func(param LogFormatterParams) string {
|
var DefaultLogFormatter = func(param LogFormatterParams) string {
|
||||||
var statusColor, methodColor, resetColor string
|
var statusColor, methodColor, resetColor string
|
||||||
if param.IsOutputColor() {
|
if param.IsOutputColor() {
|
||||||
statusColor = param.StatusCodeColor()
|
statusColor = param.StatusCodeColor()
|
||||||
@ -212,7 +212,7 @@ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
|||||||
func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
||||||
formatter := conf.Formatter
|
formatter := conf.Formatter
|
||||||
if formatter == nil {
|
if formatter == nil {
|
||||||
formatter = defaultLogFormatter
|
formatter = DefaultLogFormatter
|
||||||
}
|
}
|
||||||
|
|
||||||
out := conf.Output
|
out := conf.Output
|
||||||
|
@ -277,11 +277,11 @@ func TestDefaultLogFormatter(t *testing.T) {
|
|||||||
isTerm: false,
|
isTerm: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 5s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseParam))
|
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 5s | 20.20.20.20 | GET \"/\"\n", DefaultLogFormatter(termFalseParam))
|
||||||
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 2743h29m3s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseLongDurationParam))
|
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 2743h29m3s | 20.20.20.20 | GET \"/\"\n", DefaultLogFormatter(termFalseLongDurationParam))
|
||||||
|
|
||||||
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 5s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueParam))
|
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 5s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", DefaultLogFormatter(termTrueParam))
|
||||||
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 2743h29m3s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueLongDurationParam))
|
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 2743h29m3s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", DefaultLogFormatter(termTrueLongDurationParam))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestColorForMethod(t *testing.T) {
|
func TestColorForMethod(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user