예제 #1
0
// LogRequest creates a request logger middleware.
// This middleware is aware of the RequestID middleware and if registered after it leverages the
// request ID for logging.
func LogRequest() goa.Middleware {
	return func(h goa.Handler) goa.Handler {
		return func(ctx *goa.Context) error {
			reqID := ctx.Value(ReqIDKey)
			if reqID == nil {
				reqID = shortID()
			}
			ctx.Logger = ctx.Logger.New("id", reqID)
			startedAt := time.Now()
			r := ctx.Request()
			ctx.Info("started", r.Method, r.URL.String())
			params := ctx.AllParams()
			if len(params) > 0 {
				logCtx := make(log.Ctx, len(params))
				for k, v := range params {
					logCtx[k] = interface{}(v)
				}
				ctx.Debug("params", logCtx)
			}
			payload := ctx.RawPayload()
			if r.ContentLength > 0 {
				if mp, ok := payload.(map[string]interface{}); ok {
					ctx.Debug("payload", log.Ctx(mp))
				} else {
					ctx.Debug("payload", "raw", payload)
				}
			}
			err := h(ctx)
			ctx.Info("completed", "status", ctx.ResponseStatus(),
				"bytes", ctx.ResponseLength(), "time", time.Since(startedAt).String())
			return err
		}
	}
}
예제 #2
0
파일: log.go 프로젝트: jfbus/trace
func (w *log15Wrapper) Event(e trace.Event) {
	lvl := trace.LvlDebug
	if ce, ok := e.(trace.LevelEvent); ok {
		lvl = ce.Level()
	}

	switch lvl {
	case trace.LvlCrit:
		w.log.Crit(e.Message(), log.Ctx(e.Context()))
	case trace.LvlErr:
		w.log.Error(e.Message(), log.Ctx(e.Context()))
	case trace.LvlWarn:
		w.log.Warn(e.Message(), log.Ctx(e.Context()))
	case trace.LvlInfo:
		w.log.Info(e.Message(), log.Ctx(e.Context()))
	default:
		w.log.Debug(e.Message(), log.Ctx(e.Context()))
	}
}