Skip to content

Commit

Permalink
[patch] add timestamp disable/enable functionality (#86)
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <[email protected]>
  • Loading branch information
kpango authored Mar 20, 2021
1 parent c8e491a commit 6b8a5a9
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 18 deletions.
11 changes: 11 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ func main() {
glg.Printf("%s : %s", "printf", "formatted")
glg.CustomLog(customTag, "custom logging")
glg.CustomLog(customErrTag, "custom error logging")

glg.Info("kpango's glg supports disable timestamp for logging")
glg.Get().DisableTimestamp()
glg.Info("timestamp disabled")
glg.Warn("timestamp disabled")
glg.Log("timestamp disabled")
glg.Get().EnableTimestamp()
glg.Info("timestamp enabled")
glg.Warn("timestamp enabled")
glg.Log("timestamp enabled")

glg.Info("kpango's glg support json logging")
glg.Get().EnableJSON()
err := glg.Warn("kpango's glg", "support", "json", "logging")
Expand Down
82 changes: 68 additions & 14 deletions glg.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ type LEVEL uint8
type wMode uint8

type logger struct {
tag string
rawtag []byte
writer io.Writer
std io.Writer
color func(string) string
isColor bool
mode MODE
writeMode wMode
tag string
rawtag []byte
writer io.Writer
std io.Writer
color func(string) string
isColor bool
mode MODE
writeMode wMode
disableTimestamp bool
}

const (
Expand Down Expand Up @@ -127,9 +128,10 @@ const (
rc = "\n"
rcl = len(rc)

lsep = "\t["
tab = "\t"
lsep = tab + "["
lsepl = len(lsep)
sep = "]:\t"
sep = "]:" + tab
sepl = len(sep)
)

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

// EnableTimestamp enables timestamp output
func (g *Glg) EnableTimestamp() *Glg {

g.logger.Range(func(lev LEVEL, l *logger) bool {
l.disableTimestamp = false
g.logger.Store(lev, l)
return true
})

return g
}

// DisableTimestamp disables timestamp output
func (g *Glg) DisableTimestamp() *Glg {

g.logger.Range(func(lev LEVEL, l *logger) bool {
l.disableTimestamp = true
g.logger.Store(lev, l)
return true
})

return g
}

// EnableLevelTimestamp enables timestamp output
func (g *Glg) EnableLevelTimestamp(lv LEVEL) *Glg {
l, ok := g.logger.Load(lv)
if ok {
l.disableTimestamp = false
g.logger.Store(lv, l)
}
return g
}

// DisableLevelTimestamp disables timestamp output
func (g *Glg) DisableLevelTimestamp(lv LEVEL) *Glg {
l, ok := g.logger.Load(lv)
if ok {
l.disableTimestamp = true
g.logger.Store(lv, l)
}
return g
}

// EnableColor enables color output
func (g *Glg) EnableColor() *Glg {

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

fn := fastime.FormattedNow()
if g.enableJSON {
var w io.Writer
switch log.writeMode {
Expand All @@ -706,8 +751,13 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
} else {
detail = val[0]
}
var timestamp string
if !log.disableTimestamp {
fn := fastime.FormattedNow()
timestamp = *(*string)(unsafe.Pointer(&fn))
}
return json.NewEncoder(w).Encode(JSONFormat{
Date: *(*string)(unsafe.Pointer(&fn)),
Date: timestamp,
Level: log.tag,
Detail: detail,
})
Expand All @@ -719,8 +769,12 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
b = g.buffer.Get().(*bytes.Buffer)
)

b.Write(fn)
b.Write(log.rawtag)
if log.disableTimestamp {
b.Write(log.rawtag[len(tab):])
} else {
b.Write(fastime.FormattedNow())
b.Write(log.rawtag)
}
b.WriteString(format)

switch {
Expand Down
Loading

0 comments on commit 6b8a5a9

Please sign in to comment.