Ejemplo n.º 1
0
// Log lines have this form:
//         Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
// where the fields are defined as follows:
//         L                A single character, representing the log level (eg 'I' for INFO)
//         mm               The month (zero padded; ie May is '05')
//         dd               The day (zero padded)
//         hh:mm:ss.uuuuuu  Time in hours, minutes and fractional seconds
//         threadid         The space-padded thread ID as returned by GetTID()
//         file             The file name
//         line             The line number
//         msg              The user-supplied message
func (f *GlogFormatter) Format(context log.LogContext) []byte {
	res := &bytes.Buffer{}

	file := context.File
	slash := len(file) - 1
	for ; slash >= 0; slash-- {
		if file[slash] == filepath.Separator {
			break
		}
	}
	if slash >= 0 {
		file = file[slash+1:]
	}

	_, month, day := context.Time.Date()
	hour, minute, second := context.Time.Clock()
	f.tmp[0] = log.UcShortestSeverityStrings[log.SeverityToIndex(context.Severity)][0]
	log.TwoDigits(&f.tmp, 1, int(month))
	log.TwoDigits(&f.tmp, 3, day)
	f.tmp[5] = ' '
	log.TwoDigits(&f.tmp, 6, hour)
	f.tmp[8] = ':'
	log.TwoDigits(&f.tmp, 9, minute)
	f.tmp[11] = ':'
	log.TwoDigits(&f.tmp, 12, second)
	f.tmp[14] = '.'
	log.NDigits(&f.tmp, 6, 15, context.Time.Nanosecond()/1000)
	f.tmp[21] = ' '
	log.NDigits(&f.tmp, 5, 22, context.Pid)
	f.tmp[27] = ' '
	res.Write(f.tmp[:28])
	res.WriteString(file)
	f.tmp[0] = ':'
	n := log.Itoa(&f.tmp, 1, context.Line)
	f.tmp[n+1] = ']'
	f.tmp[n+2] = ' '
	res.Write(f.tmp[:n+3])
	message := ""
	if context.Format != nil {
		message = fmt.Sprintf(*context.Format, context.Args...)
	} else {
		message = fmt.Sprint(context.Args...)
	}

	res.WriteString(message)

	l := len(message)
	if l > 0 && message[l-1] != '\n' {
		res.WriteRune('\n')
	}

	return res.Bytes()
}
Ejemplo n.º 2
0
func (f *CustomFormatter) Format(context log.LogContext) []byte {
	buf := &bytes.Buffer{}

	t := time.Now()

	year, month, day := t.Date()
	hour, min, sec := t.Clock()
	// Write the year in 2006/01/02 format
	log.NDigits(&f.tmp, 4, 0, year)
	f.tmp[4] = '/'
	log.TwoDigits(&f.tmp, 5, int(month))
	f.tmp[7] = '/'
	log.TwoDigits(&f.tmp, 8, day)

	f.tmp[10] = ' '

	// Write the time in 15:04:05 format
	log.TwoDigits(&f.tmp, 11, hour)
	f.tmp[13] = ':'
	log.TwoDigits(&f.tmp, 14, min)
	f.tmp[16] = ':'
	log.TwoDigits(&f.tmp, 17, sec)

	f.tmp[19] = ' '

	// Write what we have thus far in tmp to our buffer
	buf.Write(f.tmp[:20])

	message := ""
	if context.Format != nil {
		message = fmt.Sprintf(*context.Format, context.Args...)
	} else {
		message = fmt.Sprint(context.Args...)
	}

	// Write the message to our buffer
	buf.WriteString(message)

	// If we don't have a newline, put one. All formatters must
	// do this.
	l := len(message)
	if l > 0 && message[l-1] != '\n' {
		buf.WriteRune('\n')
	}

	return buf.Bytes()
}