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

能否增加logrus #207

Closed
ihuanglei opened this issue Oct 9, 2020 · 1 comment
Closed

能否增加logrus #207

ihuanglei opened this issue Oct 9, 2020 · 1 comment

Comments

@ihuanglei
Copy link

@unknwon

@unknwon
Copy link
Contributor

unknwon commented Oct 9, 2020

Hi, you can make a logrus object yourself and inject it using m.Map:

m.Map(m.logger)

Then use it like this:

macaron/logger.go

Lines 45 to 73 in d229aed

func Logger() Handler {
return func(ctx *Context, log *log.Logger) {
start := time.Now()
log.Printf("%s: Started %s %s for %s", time.Now().Format(LogTimeFormat), ctx.Req.Method, ctx.Req.RequestURI, ctx.RemoteAddr())
rw := ctx.Resp.(ResponseWriter)
ctx.Next()
content := fmt.Sprintf("%s: Completed %s %s %v %s in %v", time.Now().Format(LogTimeFormat), ctx.Req.Method, ctx.Req.RequestURI, rw.Status(), http.StatusText(rw.Status()), time.Since(start))
if ColorLog {
switch rw.Status() {
case 200, 201, 202:
content = fmt.Sprintf("\033[1;32m%s\033[0m", content)
case 301, 302:
content = fmt.Sprintf("\033[1;37m%s\033[0m", content)
case 304:
content = fmt.Sprintf("\033[1;33m%s\033[0m", content)
case 401, 403:
content = fmt.Sprintf("\033[4;31m%s\033[0m", content)
case 404:
content = fmt.Sprintf("\033[1;31m%s\033[0m", content)
case 500:
content = fmt.Sprintf("\033[1;36m%s\033[0m", content)
}
}
log.Println(content)
}
}

@unknwon unknwon closed this as completed Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants