add write mutex && improve init flag

master v0.1.12
guohao 5 years ago
parent 1e6497dff8
commit 3fe2761b40
  1. 3
      filelog.go
  2. 19
      gokit.go
  3. 2
      gokit_test.go

@ -115,10 +115,9 @@ func (w *mFileLogger) flush() (err error) {
func (w *mFileLogger) Write(msg []byte) (n int, err error) {
buf := w.getBuffer()
buf.Write(msg)
w.mu.Lock()
defer w.mu.Unlock()
buf.Write(msg)
writer := w.writer
if writer == nil {

@ -172,6 +172,15 @@ func init() {
flag.Uint64Var(&fileSplitSize, "log.split", uint64(1204*1024*1800), "log fail split on bytes")
}
//SetDebug set logger debug output
func SetDebug(debug bool) {
if debug {
alsoStdout = true
withCaller = BasePathFunc
logLevel = LevelDebug
}
}
func Init() (writer io.Writer, err error) {
if inited {
return nil, fmt.Errorf("logkit has been inited")
@ -215,9 +224,9 @@ func format(level Level, msg string) string {
pc, file, line, _ = runtime.Caller(3)
switch withCaller {
case FullPATHFunc:
context = fmt.Sprintf("%s:%03d::%30s", file, line, path.Base(runtime.FuncForPC(pc).Name()))
context = fmt.Sprintf("%s:%03d::%-30s", file, line, path.Base(runtime.FuncForPC(pc).Name()))
case BasePathFunc:
context = fmt.Sprintf("%s:%03d::%30s", path.Base(file), line, path.Base(runtime.FuncForPC(pc).Name()))
context = fmt.Sprintf("%s:%03d::%-15s", path.Base(file), line, path.Base(runtime.FuncForPC(pc).Name()))
case BasePath:
context = fmt.Sprintf("%s:%03d", path.Base(file), line)
default:
@ -231,6 +240,12 @@ func format(level Level, msg string) string {
}
func write(level Level, msg string) (err error) {
if !flag.Parsed() {
return fmt.Errorf("logkit write must been flag parsed")
}
if auto && !inited {
Init()
}
if !inited {
return fmt.Errorf("logkit has been inited")
}

@ -10,7 +10,7 @@ import (
func BenchmarkGoKit(b *testing.B) {
defer Exit()
SetPath("/")
_, err := Init(FIlE, "test", LevelDebug, true, BasePath)
_, err := Init()
fmt.Printf("===%s", err)
b.ResetTimer()
for i := 0; i < b.N; i++ {

Loading…
Cancel
Save