Пример #1
0
func toLog(err error, level logs.Level) {
	if e, ok := err.(*errs.EntryError); ok {
		logs.LogEntry(&logs.Entry{
			Message: e.Message,
			Fields:  e.Fields,
			Level:   level})
		if e.Err != nil { // TODO this sux
			toLog(e.Err, level)
		}
	} else {
		logs.LogEntry(&logs.Entry{
			Message: err.Error(),
			Level:   level,
		})
	}
}
Пример #2
0
func Logger() macaron.Handler {
	var reqCounter int64
	return func(ctx *macaron.Context, log *log.Logger) {
		start := time.Now()

		fields := data.WithField("method", ctx.Req.Method).
			WithField("uri", ctx.Req.RequestURI).
			WithField("ip", ctx.RemoteAddr()).
			WithField("id", atomic.AddInt64(&reqCounter, 1))
		if logs.IsDebugEnabled() {
			logs.WithF(fields).Trace("Request received")
		}

		rw := ctx.Resp.(macaron.ResponseWriter)
		ctx.Next()

		if logs.IsInfoEnabled() {
			fields = fields.WithField("duration", time.Since(start)).WithField("status", rw.Status())
			var lvl logs.Level
			if rw.Status() >= 500 && rw.Status() < 600 {
				lvl = logs.ERROR
			} else {
				lvl = logs.DEBUG
			}

			logs.LogEntry(&logs.Entry{
				Fields:  fields,
				Level:   lvl,
				Message: "Request completed",
			})

		}
	}
}