Echo's Logger interface

Echo provides us with a Logger interface, which facilitates logging both within Echo itself and within this mechanism, which will allow you to access this logger from within your middleware and application handlers. The echo.Logger interface is extremely flexible. The default logger that Echo uses is the labstack/gommon/log logger defined within the https://github.com/labstack/gommon/ repository, but, if you so choose, you can insert your own custom logging by merely implementing the Logger interface. The following is a full listing of the capabilities of the Logger interface, which, when implemented, will give the following capabilities:

type Logger interface {
    Output() io.Writer
    SetOutput(w io.Writer)
    Prefix() string
    SetPrefix(p string)
    Level() log.Lvl
    SetLevel(v log.Lvl)
    Print(i ...interface{})
    Printf(format string, args ...interface{})
    Printj(j log.JSON)
    Debug(i ...interface{})
    Debugf(format string, args ...interface{})
    Debugj(j log.JSON)
    Info(i ...interface{})
    Infof(format string, args ...interface{})
    Infoj(j log.JSON)
    Warn(i ...interface{})
    Warnf(format string, args ...interface{})
    Warnj(j log.JSON)
    Error(i ...interface{})
    Errorf(format string, args ...interface{})
    Errorj(j log.JSON)
    Fatal(i ...interface{})
    Fatalj(j log.JSON)
    Fatalf(format string, args ...interface{})
    Panic(i ...interface{})
    Panicj(j log.JSON)
    Panicf(format string, args ...interface{})
}

As you can see, echo.Logger implementations will provide six log-level logging functions: Debug, Info, Warn, Error, Fatal, and Panic. The logger also provides a Print, Printf, and Printj capability if the developer does not want to specify the log level and just wants to have the log message output regardless of the log level. Each level provides a plain log message, a formatted log message, and a JSON implementation of logging messages. This gives the developer the flexibility to provide log messages as strings, formatted strings, or a structured logging, such as JSON.

As you can see in the preceding interface, the nonformatted log method calls allow the developer to specify a variadic parameter list to pass in any type of object they wish, which will result in the printing of a list of parameters that are passed in their string representation. For the formatted log methods, the format string is used together with the variadic parameters to populate the log message in the format specified by the developer. The JSON log methods are marshalled with the encodings/json standard library marshalling facilities.

The Logger interface also ensures that implementations implement to SetOutput, SetPrefix, and SetLevel as well. These methods allow the developer to specify where and how the logging output is to be written, set any prefix that the logs should be prefixed with, and set the application logging level (debug, info, warn, error, fatal, panic). This is very helpful for our web application development as we are able to change the log level within the application, as shown in the following code, which is in our $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter6/cmd/service/main.go file:

func main() {
        // create a new echo instance
        e := echo.New()
        e.Logger.SetLevel(log.DEBUG)
        e.Validator = new(bindings.Validator)

As you can see in the preceding example, we are able to set the Echo Logger to a log level of log.DEBUG, thereby allowing our application to log all messages at the debug level.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.135.225