Golang: Setting up Logging in Echo Web Framework

For logging Echo`s default format is JSON, which can be changed by modifying the header.

Logging

The default format for logging is JSON, which can be changed by modifying the header.

Log Header

Echo#Logger.SetHeader(io.Writer) can be used to set the header for the logger. Default value:
{"time":"${time_rfc3339_nano}","level":"${level}","prefix":"${prefix}","file":"${short_file}","line":"${line}"}

Example
import "github.com/labstack/gommon/log" /* ... */ if l, ok := e.Logger.(*log.Logger); ok { l.SetHeader("${time_rfc3339} ${level}") }

Added Log in Header
2018-05-08T20:30:06-07:00 INFO info

Available Tags

  • time_rfc3339
  • time_rfc3339_nano
  • level
  • prefix
  • long_file
  • short_file
  • line

Log Output

Echo#Logger.SetOutput(io.Writer) can be used to set the output destination for the logger. Default value is os.Stdout

To completely disable logs use Echo#Logger.SetOutput(ioutil.Discard) or Echo#Logger.SetLevel(log.OFF)

Log Level

Echo#Logger.SetLevel(log.Lvl) can be used to set the log level for the logger. Default value is ERROR. Possible values:
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • OFF

Custom Logger

Logging is implemented using echo.Logger interface which allows you to register a custom logger using Echo#Logger.
Example
s := &http.Server{ Addr: ":1323", ReadTimeout: 20 * time.Minute, WriteTimeout: 20 * time.Minute, } e.Logger.Fatal(e.StartServer(s))

Disable HTTP/2

Echo#DisableHTTP2 can be used disable HTTP/2 protocol.

Read Timeout

Echo#*Server#ReadTimeout can be used to set the maximum duration before timing out read of the request.

Write Timeout

Echo#*Server#WriteTimeout can be used to set the maximum duration before timing out write of the response.

Never miss a post from Chris Gregori, when you sign up for Ednsquare.