package capnslog
import (
"bufio"
"bytes"
"io"
"os"
"runtime"
"strconv"
"strings"
"time"
)
var pid = os .Getpid ()
type GlogFormatter struct {
StringFormatter
}
func NewGlogFormatter (w io .Writer ) *GlogFormatter {
g := &GlogFormatter {}
g .w = bufio .NewWriter (w )
return g
}
func (g GlogFormatter ) Format (pkg string , level LogLevel , depth int , entries ...interface {}) {
g .w .Write (GlogHeader (level , depth +1 ))
g .StringFormatter .Format (pkg , level , depth +1 , entries ...)
}
func GlogHeader (level LogLevel , depth int ) []byte {
now := time .Now ().UTC ()
_ , file , line , ok := runtime .Caller (depth )
if !ok {
file = "???"
line = 1
} else {
slash := strings .LastIndex (file , "/" )
if slash >= 0 {
file = file [slash +1 :]
}
}
if line < 0 {
line = 0
}
buf := &bytes .Buffer {}
buf .Grow (30 )
_ , month , day := now .Date ()
hour , minute , second := now .Clock ()
buf .WriteString (level .Char ())
twoDigits (buf , int (month ))
twoDigits (buf , day )
buf .WriteByte (' ' )
twoDigits (buf , hour )
buf .WriteByte (':' )
twoDigits (buf , minute )
buf .WriteByte (':' )
twoDigits (buf , second )
buf .WriteByte ('.' )
buf .WriteString (strconv .Itoa (now .Nanosecond () / 1000 ))
buf .WriteByte ('Z' )
buf .WriteByte (' ' )
buf .WriteString (strconv .Itoa (pid ))
buf .WriteByte (' ' )
buf .WriteString (file )
buf .WriteByte (':' )
buf .WriteString (strconv .Itoa (line ))
buf .WriteByte (']' )
buf .WriteByte (' ' )
return buf .Bytes ()
}
const digits = "0123456789"
func twoDigits(b *bytes .Buffer , d int ) {
c2 := digits [d %10 ]
d /= 10
c1 := digits [d %10 ]
b .WriteByte (c1 )
b .WriteByte (c2 )
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .