11package  log
22
33import  (
4+ 	"bufio" 
45	"context" 
6+ 	"io" 
7+ 	stdlog "log" 
58	"os" 
9+ 	"strings" 
610	"sync" 
711	"time" 
812
4448	}{
4549		name : "log" ,
4650	}
47- 	rw  sync.RWMutex 
51+ 	rw                sync.RWMutex 
52+ 	stdLogWriter      * io.PipeWriter 
53+ 	redirectComplete  chan  struct {}
4854)
4955
5056// Field is a single Field key and value 
@@ -53,6 +59,54 @@ type Field struct {
5359	Value  interface {} `json:"value"` 
5460}
5561
62+ // RedirectGoStdLog is used to redirect Go's internal std log output to this logger. 
63+ func  RedirectGoStdLog (redirect  bool ) {
64+ 	if  (redirect  &&  stdLogWriter  !=  nil ) ||  (! redirect  &&  stdLogWriter  ==  nil ) {
65+ 		// already redirected or already not redirected 
66+ 		return 
67+ 	}
68+ 	if  ! redirect  {
69+ 		stdlog .SetOutput (os .Stderr )
70+ 		// will stop scanner reading PipeReader 
71+ 		_  =  stdLogWriter .Close ()
72+ 		stdLogWriter  =  nil 
73+ 		<- redirectComplete 
74+ 		return 
75+ 	}
76+ 
77+ 	ready  :=  make (chan  struct {})
78+ 	redirectComplete  =  make (chan  struct {})
79+ 
80+ 	// last option is to redirect 
81+ 	go  func () {
82+ 		var  r  * io.PipeReader 
83+ 		r , stdLogWriter  =  io .Pipe ()
84+ 		defer  func () {
85+ 			_  =  r .Close ()
86+ 		}()
87+ 
88+ 		stdlog .SetOutput (stdLogWriter )
89+ 		defer  func () {
90+ 			close (redirectComplete )
91+ 			redirectComplete  =  nil 
92+ 		}()
93+ 
94+ 		scanner  :=  bufio .NewScanner (r )
95+ 		close (ready )
96+ 		for  scanner .Scan () {
97+ 			txt  :=  scanner .Text ()
98+ 			if  strings .Contains (txt , "error" ) {
99+ 				WithField ("stdlog" , true ).Error (txt )
100+ 			} else  if  strings .Contains (txt , "warning" ) {
101+ 				WithField ("stdlog" , true ).Warn (txt )
102+ 			} else  {
103+ 				WithField ("stdlog" , true ).Notice (txt )
104+ 			}
105+ 		}
106+ 	}()
107+ 	<- ready 
108+ }
109+ 
56110// SetExitFunc sets the provided function as the exit function used in Fatal(), 
57111// Fatalf(), Panic() and Panicf(). This is primarily used when wrapping this library, 
58112// you can set this to enable testing (with coverage) of your Fatal() and Fatalf() 
@@ -116,7 +170,6 @@ func AddHandler(h Handler, levels ...Level) {
116170	defer  rw .Unlock ()
117171	if  defaultHandler  !=  nil  {
118172		removeHandler (h )
119- 		_  =  defaultHandler .closeAlreadyLocked ()
120173		defaultHandler  =  nil 
121174	}
122175	for  _ , level  :=  range  levels  {
@@ -149,9 +202,9 @@ OUTER:
149202	}
150203}
151204
152- // RemoveHandlerLevels  removes the supplied levels, if no more levels exists for the handler 
205+ // removeHandlerLevels  removes the supplied levels, if no more levels exists for the handler 
153206// it will no longer be registered and need to added via AddHandler again. 
154- func  RemoveHandlerLevels (h  Handler , levels  ... Level ) {
207+ func  removeHandlerLevels (h  Handler , levels  ... Level ) {
155208	rw .Lock ()
156209	defer  rw .Unlock ()
157210OUTER:
0 commit comments