Browse Source

Time format (#5)

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/beaver/pulls/5
Co-authored-by: John Olheiser <john.olheiser@gmail.com>
Co-committed-by: John Olheiser <john.olheiser@gmail.com>
main v1.1.1
John Olheiser 8 months ago
parent
commit
0b1a1e4bd0
  1. 36
      level.go
  2. 2
      logger.go

36
level.go

@ -3,7 +3,6 @@ package beaver
import (
"fmt"
"strings"
"time"
"go.jolheiser.com/beaver/color"
)
@ -105,38 +104,3 @@ func ParseLevel(level string) Level {
}
return INFO
}
func timePrefix(t time.Time) string {
var buf = &[]byte{}
year, month, day := t.Date()
itoa(buf, int(month), 2)
*buf = append(*buf, '/')
itoa(buf, day, 2)
*buf = append(*buf, '/')
itoa(buf, year, 4)
*buf = append(*buf, ' ')
hour, min, sec := t.Clock()
itoa(buf, hour, 2)
*buf = append(*buf, ':')
itoa(buf, min, 2)
*buf = append(*buf, ':')
itoa(buf, sec, 2)
return string(*buf)
}
func itoa(buf *[]byte, i int, wid int) {
var b [20]byte
bp := len(b) - 1
for i >= 10 || wid > 1 {
wid--
q := i / 10
b[bp] = byte('0' + i - q*10)
bp--
i = q
}
// i < 10
b[bp] = byte('0' + i)
*buf = append(*buf, b[bp:]...)
}

2
logger.go

@ -42,7 +42,7 @@ func (l *Logger) write(level Level, text string) {
if l.Level <= level {
var message string
if l.Format.TimePrefix {
message += color.Time.Format(timePrefix(time.Now())) + " "
message += color.Time.Format(time.Now().Format("01/02/2006 15:04:05")) + " "
}
if l.Format.StackPrefix {
_, file, line, _ := runtime.Caller(l.skip)

Loading…
Cancel
Save