Skip to content

Commit 6123840

Browse files
lunnytechknowlogick
authored andcommitted
Remove macaron dependent on modules/log (#6933)
1 parent 3957b40 commit 6123840

File tree

3 files changed

+101
-104
lines changed

3 files changed

+101
-104
lines changed

modules/log/colors_router.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package log
6+
7+
import (
8+
"time"
9+
)
10+
11+
var statusToColor = map[int][]byte{
12+
100: ColorBytes(Bold),
13+
200: ColorBytes(FgGreen),
14+
300: ColorBytes(FgYellow),
15+
304: ColorBytes(FgCyan),
16+
400: ColorBytes(Bold, FgRed),
17+
401: ColorBytes(Bold, FgMagenta),
18+
403: ColorBytes(Bold, FgMagenta),
19+
500: ColorBytes(Bold, BgRed),
20+
}
21+
22+
// ColoredStatus addes colors for HTTP status
23+
func ColoredStatus(status int, s ...string) *ColoredValue {
24+
color, ok := statusToColor[status]
25+
if !ok {
26+
color, ok = statusToColor[(status/100)*100]
27+
}
28+
if !ok {
29+
color = fgBoldBytes
30+
}
31+
if len(s) > 0 {
32+
return NewColoredValueBytes(s[0], &color)
33+
}
34+
return NewColoredValueBytes(status, &color)
35+
}
36+
37+
var methodToColor = map[string][]byte{
38+
"GET": ColorBytes(FgBlue),
39+
"POST": ColorBytes(FgGreen),
40+
"DELETE": ColorBytes(FgRed),
41+
"PATCH": ColorBytes(FgCyan),
42+
"PUT": ColorBytes(FgYellow, Faint),
43+
"HEAD": ColorBytes(FgBlue, Faint),
44+
}
45+
46+
// ColoredMethod addes colors for HtTP methos on log
47+
func ColoredMethod(method string) *ColoredValue {
48+
color, ok := methodToColor[method]
49+
if !ok {
50+
return NewColoredValueBytes(method, &fgBoldBytes)
51+
}
52+
return NewColoredValueBytes(method, &color)
53+
}
54+
55+
var (
56+
durations = []time.Duration{
57+
10 * time.Millisecond,
58+
100 * time.Millisecond,
59+
1 * time.Second,
60+
5 * time.Second,
61+
10 * time.Second,
62+
}
63+
64+
durationColors = [][]byte{
65+
ColorBytes(FgGreen),
66+
ColorBytes(Bold),
67+
ColorBytes(FgYellow),
68+
ColorBytes(FgRed, Bold),
69+
ColorBytes(BgRed),
70+
}
71+
72+
wayTooLong = ColorBytes(BgMagenta)
73+
)
74+
75+
// ColoredTime addes colors for time on log
76+
func ColoredTime(duration time.Duration) *ColoredValue {
77+
for i, k := range durations {
78+
if duration < k {
79+
return NewColoredValueBytes(duration, &durationColors[i])
80+
}
81+
}
82+
return NewColoredValueBytes(duration, &wayTooLong)
83+
}

modules/log/router.go

Lines changed: 0 additions & 103 deletions
This file was deleted.

routers/routes/routes.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ func setupAccessLogger(m *macaron.Macaron) {
9494
})
9595
}
9696

97+
// RouterHandler is a macaron handler that will log the routing to the default gitea log
98+
func RouterHandler(level log.Level) func(ctx *macaron.Context) {
99+
return func(ctx *macaron.Context) {
100+
start := time.Now()
101+
102+
log.GetLogger("router").Log(0, level, "Started %s %s for %s", log.ColoredMethod(ctx.Req.Method), ctx.Req.RequestURI, ctx.RemoteAddr())
103+
104+
rw := ctx.Resp.(macaron.ResponseWriter)
105+
ctx.Next()
106+
107+
status := rw.Status()
108+
log.GetLogger("router").Log(0, level, "Completed %s %s %v %s in %v", log.ColoredMethod(ctx.Req.Method), ctx.Req.RequestURI, log.ColoredStatus(status), log.ColoredStatus(status, http.StatusText(rw.Status())), log.ColoredTime(time.Since(start)))
109+
}
110+
}
111+
97112
// NewMacaron initializes Macaron instance.
98113
func NewMacaron() *macaron.Macaron {
99114
gob.Register(&u2f.Challenge{})
@@ -102,7 +117,9 @@ func NewMacaron() *macaron.Macaron {
102117
loggerAsWriter := log.NewLoggerAsWriter("INFO", log.GetLogger("macaron"))
103118
m = macaron.NewWithLogger(loggerAsWriter)
104119
if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE {
105-
log.SetupRouterLogger(m, setting.RouterLogLevel)
120+
if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel {
121+
m.Use(RouterHandler(setting.RouterLogLevel))
122+
}
106123
}
107124
} else {
108125
m = macaron.New()

0 commit comments

Comments
 (0)