Skip to content

Commit 6b8a5a9

Browse files
authored
[patch] add timestamp disable/enable functionality (#86)
Signed-off-by: kpango <[email protected]>
1 parent c8e491a commit 6b8a5a9

File tree

5 files changed

+403
-18
lines changed

5 files changed

+403
-18
lines changed

example/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ func main() {
148148
glg.Printf("%s : %s", "printf", "formatted")
149149
glg.CustomLog(customTag, "custom logging")
150150
glg.CustomLog(customErrTag, "custom error logging")
151+
152+
glg.Info("kpango's glg supports disable timestamp for logging")
153+
glg.Get().DisableTimestamp()
154+
glg.Info("timestamp disabled")
155+
glg.Warn("timestamp disabled")
156+
glg.Log("timestamp disabled")
157+
glg.Get().EnableTimestamp()
158+
glg.Info("timestamp enabled")
159+
glg.Warn("timestamp enabled")
160+
glg.Log("timestamp enabled")
161+
151162
glg.Info("kpango's glg support json logging")
152163
glg.Get().EnableJSON()
153164
err := glg.Warn("kpango's glg", "support", "json", "logging")

glg.go

+68-14
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ type LEVEL uint8
6666
type wMode uint8
6767

6868
type logger struct {
69-
tag string
70-
rawtag []byte
71-
writer io.Writer
72-
std io.Writer
73-
color func(string) string
74-
isColor bool
75-
mode MODE
76-
writeMode wMode
69+
tag string
70+
rawtag []byte
71+
writer io.Writer
72+
std io.Writer
73+
color func(string) string
74+
isColor bool
75+
mode MODE
76+
writeMode wMode
77+
disableTimestamp bool
7778
}
7879

7980
const (
@@ -127,9 +128,10 @@ const (
127128
rc = "\n"
128129
rcl = len(rc)
129130

130-
lsep = "\t["
131+
tab = "\t"
132+
lsep = tab + "["
131133
lsepl = len(lsep)
132-
sep = "]:\t"
134+
sep = "]:" + tab
133135
sepl = len(sep)
134136
)
135137

@@ -479,6 +481,50 @@ func (g *Glg) AddErrLevel(tag string, mode MODE, isColor bool) *Glg {
479481
return g
480482
}
481483

484+
// EnableTimestamp enables timestamp output
485+
func (g *Glg) EnableTimestamp() *Glg {
486+
487+
g.logger.Range(func(lev LEVEL, l *logger) bool {
488+
l.disableTimestamp = false
489+
g.logger.Store(lev, l)
490+
return true
491+
})
492+
493+
return g
494+
}
495+
496+
// DisableTimestamp disables timestamp output
497+
func (g *Glg) DisableTimestamp() *Glg {
498+
499+
g.logger.Range(func(lev LEVEL, l *logger) bool {
500+
l.disableTimestamp = true
501+
g.logger.Store(lev, l)
502+
return true
503+
})
504+
505+
return g
506+
}
507+
508+
// EnableLevelTimestamp enables timestamp output
509+
func (g *Glg) EnableLevelTimestamp(lv LEVEL) *Glg {
510+
l, ok := g.logger.Load(lv)
511+
if ok {
512+
l.disableTimestamp = false
513+
g.logger.Store(lv, l)
514+
}
515+
return g
516+
}
517+
518+
// DisableLevelTimestamp disables timestamp output
519+
func (g *Glg) DisableLevelTimestamp(lv LEVEL) *Glg {
520+
l, ok := g.logger.Load(lv)
521+
if ok {
522+
l.disableTimestamp = true
523+
g.logger.Store(lv, l)
524+
}
525+
return g
526+
}
527+
482528
// EnableColor enables color output
483529
func (g *Glg) EnableColor() *Glg {
484530

@@ -685,7 +731,6 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
685731
return fmt.Errorf("error:\tLog Level %d Not Found", level)
686732
}
687733

688-
fn := fastime.FormattedNow()
689734
if g.enableJSON {
690735
var w io.Writer
691736
switch log.writeMode {
@@ -706,8 +751,13 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
706751
} else {
707752
detail = val[0]
708753
}
754+
var timestamp string
755+
if !log.disableTimestamp {
756+
fn := fastime.FormattedNow()
757+
timestamp = *(*string)(unsafe.Pointer(&fn))
758+
}
709759
return json.NewEncoder(w).Encode(JSONFormat{
710-
Date: *(*string)(unsafe.Pointer(&fn)),
760+
Date: timestamp,
711761
Level: log.tag,
712762
Detail: detail,
713763
})
@@ -719,8 +769,12 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
719769
b = g.buffer.Get().(*bytes.Buffer)
720770
)
721771

722-
b.Write(fn)
723-
b.Write(log.rawtag)
772+
if log.disableTimestamp {
773+
b.Write(log.rawtag[len(tab):])
774+
} else {
775+
b.Write(fastime.FormattedNow())
776+
b.Write(log.rawtag)
777+
}
724778
b.WriteString(format)
725779

726780
switch {

0 commit comments

Comments
 (0)