Original post

Hello @tuhao1020, @AlbinoGeek

All the features discussed can already implemented with the current golog exported functionality, even to write a file per log level (use the handler inside a struct which prints based on the golog.Level, e.g. map[golog.Level]io.Writer, it’s easy see next post comment).

// set output type such as text, json…, this setting will valid for all log levels

EDIT: see the next comment on this post issue, the answer has been updated, a new Formatter introduced, one level higher than the Handler below.

Example Code:

import "github.com/kataras/golog"

func jsonOutput(l *golog.Log) bool {
        enc := json.NewEncoder(l.Logger.GetLevelOutput(l.Level.String()))
        enc.SetIndent("", "    ")
        err := enc.Encode(l)
        return err == nil

// [...Register it] 

I also agree with separation of concerns, that’s why I think we can implement something in a totally new package. However, the implementation of different file per level, rotation and e.t.c cannot be implemented just for the request logger, the request logger has a single level (info) so I think @tuhao1020 asked that for the whole application’s logger, this feature is easy to design, as said before, manually but we can add helper functions inside the golog package itself (same for the JSON output and e.t.c.)

// supply some pre-defined field patterns
// for example: %time bind to request timestamp with format 2006/01/02 15:04:05, %header.referer bind to ctx.GetHeader(“referer”)
logger.SetAccessLogFields(“%time”, “%latency”, “%statusCode”, “%header.referer”)

I don’t like this, what that means?:

  • is it format ? no
  • what would be their title names if JSON output selected?
  • it binds? meaning that the | characters will not be visible on not-set fields?
  • what %header.referer means and why not configuration fields like: Referer bool or HeaderKeys []string{"Referer", ...}?

// access log is not enabled by default
logger.EnableAccessLog(“./log”, “access.log”)

This cannot be lived inside the app.Logger() because it is only useful for http/request logging, the golog package is a general purpose leveled logger. Instead, it should be registered like the current request logger, e.g accesslog.New(...).

// log file rotate setting
logger.EnableRotate(rotation.WithMaxAge(24 * time.Hour).WithMaxSize(100 * 1024 * 1024))

About log rotation, I am not an expert on that field, I can learn fast, I can design APIs to be as developer-friendly as possible but…I still believe that there are software out there (especially in unix world) that are tested for years and developers are already familiar with those. However, if @tuhao1020 insists that such a feature should live inside Iris ecosystem, I can give it a try, this will live in the golog package though, which can be developed and tested Iris-independently (app.Logger().EnableRotation(...).