Logging
Kocha provides a level-based logger.
Basics
Logging feature is provided by Application.Logger.
Usage:
In controller:
func (r *Root) GET(c *kocha.Context) error {
c.App.Logger.Info("This is a root")
return c.Render(nil)
}
Configurations
Logger configurations is in config/app.go
.
// Logger settings.
Logger: &kocha.LoggerConfig{
Writer: os.Stdout,
Formatter: &log.LTSVFormatter{},
Level: log.INFO,
}
The definition of LoggerConfig is following.
// LoggerConfig represents the configuration of the logger.
type LoggerConfig struct {
Writer io.Writer // output destination for the logger.
Formatter log.Formatter // formatter for log entry.
Level log.Level // log level.
}
Log levels
The log levels are following.
- log.NONE (godoc)
- log.DEBUG (godoc)
- log.INFO (godoc)
- log.WARN (godoc)
- log.ERROR (godoc)
- log.FATAL (godoc)
- log.PANIC (godoc)
You can set a log level to Logger by some ways below.
- Set log level to LoggerConfig.Level in
config/app.go
configuration file. - Use log.Logger.SetLevel API
Suppress the output by log level
For example, when log level set to log.INFO.
Logger.Debug("debug log")
It won't be output because log level of Logger.Debug() has a log.DEBUG, and lower than log.INFO.
Logger.Info("info log")
Logger.Error("error log")
It will be output because Logger.Info() and Logger.Error have a log level which equal or upper than log.INFO.
Formatter
Logger has a formatter that format to specific log format.
Built-in formatter
Kocha provides formatter below.
LTSVFormatter (godoc)
A formatter of Labeled Tab-separated Values (LTSV).
This is the default formatter of Kocha if you haven't specified the formatter.
See http://ltsv.org/ for more details of LTSV.
level:INFO time:2014-07-30T17:45:40.419347835+09:00 method:GET protocol:HTTP/1.1 status:200 uri:/
level:INFO time:2014-07-30T17:45:48.238892704+09:00 method:GET protocol:HTTP/1.1 status:404 uri:/user
Custom formatter
You can define your own custom formatter.
- Implements the log.Format interface.
- It set to
AppConfig.Logger.Formatter
inconfig/app.go
.