mirror of https://github.com/sirupsen/logrus.git
add replace hook method and remove lock method
This commit is contained in:
parent
7207a92697
commit
08bbc96fbf
12
exported.go
12
exported.go
|
@ -268,15 +268,3 @@ func Panicln(args ...interface{}) {
|
||||||
func Fatalln(args ...interface{}) {
|
func Fatalln(args ...interface{}) {
|
||||||
std.Fatalln(args...)
|
std.Fatalln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Lock() {
|
|
||||||
if !std.mu.disabled {
|
|
||||||
std.mu.Lock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Unlock() {
|
|
||||||
if !std.mu.disabled {
|
|
||||||
std.mu.Unlock()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
18
logger.go
18
logger.go
|
@ -406,6 +406,24 @@ func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks {
|
||||||
return oldHooks
|
return oldHooks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReplaceHook replaces the logger hook and returns hook exists or not
|
||||||
|
func (logger *Logger) ReplaceHook(hook Hook) bool {
|
||||||
|
logger.mu.Lock()
|
||||||
|
oldHooks := logger.Hooks
|
||||||
|
hasReplaced := false
|
||||||
|
for _, level := range hook.Levels() {
|
||||||
|
for index, h := range oldHooks[level] {
|
||||||
|
if h == hook {
|
||||||
|
oldHooks[level] = append(oldHooks[level][:index], oldHooks[level][index+1:]...)
|
||||||
|
hasReplaced = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.mu.Unlock()
|
||||||
|
return hasReplaced
|
||||||
|
}
|
||||||
|
|
||||||
// SetBufferPool sets the logger buffer pool.
|
// SetBufferPool sets the logger buffer pool.
|
||||||
func (logger *Logger) SetBufferPool(pool BufferPool) {
|
func (logger *Logger) SetBufferPool(pool BufferPool) {
|
||||||
logger.mu.Lock()
|
logger.mu.Lock()
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -133,11 +132,7 @@ func TestLogger_concurrentLock(t *testing.T) {
|
||||||
time.Sleep(1 * time.Minute)
|
time.Sleep(1 * time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
var traceLock = &sync.Mutex{}
|
|
||||||
|
|
||||||
func AddTraceIdHook(traceId string) Hook {
|
func AddTraceIdHook(traceId string) Hook {
|
||||||
defer traceLock.Unlock()
|
|
||||||
traceLock.Lock()
|
|
||||||
traceHook := newTraceIdHook(traceId)
|
traceHook := newTraceIdHook(traceId)
|
||||||
if StandardLogger().Hooks == nil {
|
if StandardLogger().Hooks == nil {
|
||||||
hooks := new(LevelHooks)
|
hooks := new(LevelHooks)
|
||||||
|
@ -148,23 +143,7 @@ func AddTraceIdHook(traceId string) Hook {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveTraceHook(hook Hook) {
|
func RemoveTraceHook(hook Hook) {
|
||||||
allHooks := StandardLogger().Hooks
|
StandardLogger().ReplaceHook(hook)
|
||||||
func() {
|
|
||||||
defer Unlock()
|
|
||||||
Lock()
|
|
||||||
for key, hooks := range allHooks {
|
|
||||||
replaceHooks := hooks
|
|
||||||
for index, h := range hooks {
|
|
||||||
if h == hook {
|
|
||||||
replaceHooks = append(hooks[:index], hooks[index:]...)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
allHooks[key] = replaceHooks
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
StandardLogger().ReplaceHooks(allHooks)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TraceIdHook struct {
|
type TraceIdHook struct {
|
||||||
|
@ -192,7 +171,6 @@ func (t TraceIdHook) Fire(entry *Entry) error {
|
||||||
|
|
||||||
type LogFormatter struct{}
|
type LogFormatter struct{}
|
||||||
|
|
||||||
|
|
||||||
func (s *LogFormatter) Format(entry *Entry) ([]byte, error) {
|
func (s *LogFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||||
var file string
|
var file string
|
||||||
|
|
Loading…
Reference in New Issue