GolangNote

Golang笔记

关掉goji 的log 输出

Permalink

goji 默认对每个请求都输出请求时间,在项目刚起步时可以作为性能参考,但在实际生产环境里,可以关掉log

关掉goji 的log 输出

有两种方式可以关掉goji log 输出:

第一种,使用 ioutil.Discard

Go: ioutil.Discard
1
2
3
4
5
6
7
8
import (
    "io/ioutil"
    "log"
)

func init() {
    log.SetOutput(ioutil.Discard)
}

第二种,使用 middleware

Go: goji middleware
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
    "fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
    "github.com/zenazn/goji/web/middleware" // add line 1
)

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}

func main() {
    goji.Abandon(middleware.Logger) // add line 2
    goji.Get("/hello/:name", hello)
    goji.Serve()
}

本文网址: https://golangnote.com/topic/26.html 转摘请注明来源

Related articles

goji + SSDB 构建的应用

已经过了学生的年纪,学习新东西不那么快,尝试从python 转向golang,就从博客程序开始。...

Write a Comment to "关掉goji 的log 输出"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms