123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package prettylog
- import (
- "fmt"
- "log"
- "runtime"
- "strings"
- "time"
- )
- // Writer struct that implements writer
- type Writer struct {
- lastTime time.Time
- counter int64
- }
- // New writer
- func New() *Writer {
- return &Writer{time.Now(), 0}
- }
- // Write writer implementation
- func (w *Writer) Write(b []byte) (int, error) {
- ptr, _, line, _ := runtime.Caller(3)
- tname := runtime.FuncForPC(ptr).Name()
- li := strings.LastIndex(tname, "/")
- fname := tname[li+1:]
- timeDiff := time.Since(w.lastTime)
- var fduration = float64(timeDiff.Nanoseconds()) / 1000000.0
- msg := fmt.Sprintf("[%d:\033[34m%s\033[0m (\033[33m%s:%d\033[0m) \033[90m+%.2f/ms\033[0m]: %s",
- w.counter,
- time.Now().Format("2006-01-02 15:04:05"),
- fname,
- line,
- fduration,
- string(b),
- )
- w.lastTime = time.Now()
- w.counter++
- return fmt.Print(msg)
- }
- //CreateLogger creates a new log.Logger
- func CreateLogger() *log.Logger {
- return log.New(New(), "", 0)
- }
|