Beispiel #1
0
//replace ResponseWriter with a monitorable one, return logger
func (m *monitorableWriter) Log() {
	duration := time.Now().Sub(m.t0)
	if m.Code == 0 {
		m.Code = 200
	}
	if m.opts.Filter != nil && !m.opts.Filter(m.r, m.Code, duration, m.Size) {
		return //skip
	}
	cc := m.colorCode()
	size := ""
	if m.Size > 0 {
		size = sizestr.ToString(m.Size)
	}
	buff := bytes.Buffer{}
	m.opts.formatTmpl.Execute(&buff, &struct {
		*Colors
		Timestamp, Method, Path, CodeColor string
		Code                               int
		Duration, Size, IP                 string
	}{
		m.opts.Colors,
		m.t0.Format(m.opts.TimeFormat), m.method, m.path, cc,
		m.Code,
		fmtDuration(duration), size, m.ip,
	})
	//fmt is threadsafe :)
	fmt.Fprint(m.opts.Writer, buff.String())
}
Beispiel #2
0
//replace ResponseWriter with a monitorable one, return logger
func (m *MonitorableWriter) Log() {
	now := time.Now()
	if m.Code == 0 {
		m.Code = 200
	}
	b := bytes.Buffer{}
	b.WriteString(now.Format(TimeFormat) + " ")
	b.WriteString(m.method + " ")
	b.WriteString(m.path + " ")
	b.WriteString(strconv.Itoa(m.Code) + " ")
	dur := integerRegexp.ReplaceAllString(now.Sub(m.t0).String(), "")
	b.WriteString(dur)
	if m.Size > 0 {
		b.WriteString(" " + sizestr.ToString(m.Size))
	}
	if m.ip != "::1" && m.ip != "127.0.0.1" {
		b.WriteString(" (" + m.ip + ")")
	}
	b.WriteString("\n")
	Writer.Write(b.Bytes())
}