Skip to content

Commit

Permalink
[patch] update fastime usage (#38)
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <[email protected]>
  • Loading branch information
Yusuke Kato authored Apr 28, 2019
1 parent 52de62a commit 8f2fce6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
37 changes: 19 additions & 18 deletions glg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package glg

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
Expand All @@ -34,7 +33,6 @@ import (
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/kpango/fastime"
Expand All @@ -46,7 +44,6 @@ type Glg struct {
levelCounter *uint32
levelMap sync.Map
buffer sync.Pool
ft *fastime.Fastime
}

// MODE is logging mode (std only, writer only, std & writer)
Expand Down Expand Up @@ -184,63 +181,62 @@ func New() *Glg {
return bytes.NewBuffer(make([]byte, 0, bufferSize))
},
},
ft: fastime.New().SetFormat(timeFormat).StartTimerD(context.Background(), 100*time.Millisecond),
}

atomic.StoreUint32(g.levelCounter, uint32(FATAL))

for lev, log := range map[LEVEL]*logger{
// standard out
PRINT: &logger{
PRINT: {
std: os.Stdout,
color: Colorless,
isColor: true,
mode: STD,
},
LOG: &logger{
LOG: {
std: os.Stdout,
color: Colorless,
isColor: true,
mode: STD,
},
INFO: &logger{
INFO: {
std: os.Stdout,
color: Green,
isColor: true,
mode: STD,
},
DEBG: &logger{
DEBG: {
std: os.Stdout,
color: Purple,
isColor: true,
mode: STD,
},
OK: &logger{
OK: {
std: os.Stdout,
color: Cyan,
isColor: true,
mode: STD,
},
WARN: &logger{
WARN: {
std: os.Stdout,
color: Orange,
isColor: true,
mode: STD,
},
// error out
ERR: &logger{
ERR: {
std: os.Stderr,
color: Red,
isColor: true,
mode: STD,
},
FAIL: &logger{
FAIL: {
std: os.Stderr,
color: Red,
isColor: true,
mode: STD,
},
FATAL: &logger{
FATAL: {
std: os.Stderr,
color: Red,
isColor: true,
Expand All @@ -258,6 +254,7 @@ func New() *Glg {
// Get returns singleton glg instance
func Get() *Glg {
once.Do(func() {
fastime.SetFormat(timeFormat)
glg = New()
})
return glg
Expand Down Expand Up @@ -570,12 +567,12 @@ func (g *Glg) HTTPLogger(name string, handler http.Handler) http.Handler {
// HTTPLoggerFunc is simple http access logger
func (g *Glg) HTTPLoggerFunc(name string, hf http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := g.ft.Now()
start := fastime.Now()

hf(w, r)

err := g.Logf("Method: %s\tURI: %s\tName: %s\tTime: %s",
r.Method, r.RequestURI, name, g.ft.Now().Sub(start).String())
r.Method, r.RequestURI, name, fastime.Now().Sub(start).String())

if err != nil {
err = g.Error(err)
Expand Down Expand Up @@ -664,7 +661,7 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
log = l.(*logger)
)

b.Write(g.ft.FormattedNow())
b.Write(fastime.FormattedNow())
b.WriteString("\t[")
b.WriteString(log.tag)
b.WriteString(sep)
Expand All @@ -686,7 +683,9 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error {
buf = b.Bytes()
var str = *(*string)(unsafe.Pointer(&buf))
_, err = fmt.Fprintf(log.std, log.color(str)+rc, val...)
_, err = fmt.Fprintf(log.writer, str+rc, val...)
if err == nil {
_, err = fmt.Fprintf(log.writer, str+rc, val...)
}
case log.writeMode^writeBoth == 0:
b.WriteString(rc)
buf = b.Bytes()
Expand Down Expand Up @@ -1085,7 +1084,9 @@ func Fatalln(val ...interface{}) {
glg.Fatalln(val...)
}

// ReplaceExitFunc replaces exit function. If you do not want to start os.Exit at glg.Fatal error, use this function to register arbitrary function
// ReplaceExitFunc replaces exit function.
// If you do not want to start os.Exit at glg.Fatal error,
// use this function to register arbitrary function
func ReplaceExitFunc(fn func(i int)) {
exit = fn
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/kpango/glg

go 1.12

require github.com/kpango/fastime v1.0.8
require github.com/kpango/fastime v1.0.9
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/kpango/fastime v1.0.8 h1:Wif5eocdsIXmMG+8HHfRP/jD6UUl+/OVTJ+sMzvA1+E=
github.com/kpango/fastime v1.0.8/go.mod h1:Y5XY5bLG5yc7g2XmMUzc22XYV1XaH+KgUOHkDvLp4SA=
github.com/kpango/fastime v1.0.9 h1:GZ7WtFFpTCq8aPXAM+AxKZ1MtNYc22fTpEp4tPKuiKI=
github.com/kpango/fastime v1.0.9/go.mod h1:lVqUTcXmQnk1wriyvq5DElbRSRDC0XtqbXQRdz0Eo+g=

0 comments on commit 8f2fce6

Please sign in to comment.