forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.go
50 lines (44 loc) · 1.36 KB
/
dev.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2017 Gerasimos Maropoulos, ΓΜ. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logger
import (
"fmt"
"io"
"strings"
"sync"
"time"
)
// NewDevLogger returns a new logger of io.Writer which
// formats its log message input and writes it
// to the os.Stdout.
func NewDevLogger(omitTimeFor ...string) io.Writer {
mu := &sync.Mutex{} // for now and last log
lastLog := time.Now()
distanceDuration := 850 * time.Millisecond
return writerFunc(func(p []byte) (int, error) {
logMessage := string(p)
for _, s := range omitTimeFor {
if strings.Contains(logMessage, s) {
n, err := fmt.Print(logMessage)
lastLog = time.Now()
return n, err
}
}
mu.Lock()
defer mu.Unlock() // "slow" but we don't care here.
nowLog := time.Now()
if nowLog.Before(lastLog.Add(distanceDuration)) {
// don't use the log.Logger to print this message
// if the last one was printed before some seconds.
n, err := fmt.Println(logMessage) // fmt because we don't want the time, dev is dev so console.
lastLog = nowLog
return n, err
}
// begin with new line in order to have the time once at the top
// and the child logs below it.
n, err := fmt.Printf("%s \u2192\n%s\n", nowLog.Format("01/02/2006 03:04:05"), logMessage)
lastLog = nowLog
return n, err
})
}