@@ -44,13 +44,19 @@ func GetLogger(name string, logger MultiLogger) (l *log.Logger) {
44
44
func InitializeFromConfig (basePath string , config * config.Context ) (c * CompositeMultiHandler ) {
45
45
// If the configuration has an all option we can skip some
46
46
c , _ = NewCompositeMultiHandler ()
47
+
48
+ // Filters are assigned first, non filtered items override filters
47
49
initAllLog (c , basePath , config )
48
50
initLogLevels (c , basePath , config )
49
- initRequestLog (c , basePath , config )
50
- // Check to see if critical handler needs to be assigned to the error handler
51
51
if c .CriticalHandler == nil && c .ErrorHandler != nil {
52
52
c .CriticalHandler = c .ErrorHandler
53
53
}
54
+ initFilterLog (c , basePath , config )
55
+ if c .CriticalHandler == nil && c .ErrorHandler != nil {
56
+ c .CriticalHandler = c .ErrorHandler
57
+ }
58
+ initRequestLog (c , basePath , config )
59
+
54
60
return c
55
61
}
56
62
@@ -62,19 +68,57 @@ func initAllLog(c *CompositeMultiHandler, basePath string, config *config.Contex
62
68
log .Printf ("Adding standard handler for levels to >%s< " , output )
63
69
initHandlerFor (c , output , basePath , NewLogOptions (config , true , nil , LvlAllList ... ))
64
70
}
65
- optionList := config .Options ("log.all.filter" )
66
- for _ , option := range optionList {
67
- splitOptions := strings .Split (option , "." )
68
- keyMap := map [string ]interface {}{}
69
- for x := 3 ; x < len (splitOptions ); x += 2 {
70
- keyMap [splitOptions [x ]] = splitOptions [x + 1 ]
71
- }
71
+ }
72
+ }
72
73
73
- log .Printf ("Adding key map handler to all %s output %s" , option , config .StringDefault (option , "" ))
74
- phandler := NewParentLogHandler (func (child LogHandler ) LogHandler {
75
- return MatchMapHandler (keyMap , child )
76
- })
77
- initHandlerFor (c , config .StringDefault (option , "" ), basePath , NewLogOptions (config , false , phandler ))
74
+ // Init the filter options
75
+ // log.all.filter ....
76
+ // log.error.filter ....
77
+ func initFilterLog (c * CompositeMultiHandler , basePath string , config * config.Context ) {
78
+ if config != nil {
79
+ // The commands to use
80
+ logFilterList := []struct {
81
+ LogPrefix , LogSuffix string
82
+ parentHandler func (map [string ]interface {}) ParentLogHandler
83
+ }{{
84
+ "log." , ".filter" ,
85
+ func (keyMap map [string ]interface {}) ParentLogHandler {
86
+ return NewParentLogHandler (func (child LogHandler ) LogHandler {
87
+ return MatchMapHandler (keyMap , child )
88
+ })
89
+
90
+ },
91
+ }, {
92
+ "log." , ".nfilter" ,
93
+ func (keyMap map [string ]interface {}) ParentLogHandler {
94
+ return NewParentLogHandler (func (child LogHandler ) LogHandler {
95
+ return NotMatchMapHandler (keyMap , child )
96
+ })
97
+ },
98
+ }}
99
+
100
+ for _ , logFilter := range logFilterList {
101
+ // Init for all filters
102
+ for _ , name := range []string {"all" , "debug" , "info" , "warn" , "error" , "crit" ,
103
+ "trace" , // TODO trace is deprecated
104
+ } {
105
+ optionList := config .Options (logFilter .LogPrefix + name + logFilter .LogSuffix )
106
+ for _ , option := range optionList {
107
+ splitOptions := strings .Split (option , "." )
108
+ keyMap := map [string ]interface {}{}
109
+ for x := 3 ; x < len (splitOptions ); x += 2 {
110
+ keyMap [splitOptions [x ]] = splitOptions [x + 1 ]
111
+ }
112
+ phandler := logFilter .parentHandler (keyMap )
113
+ log .Printf ("Adding key map handler %s %s output %s" , option , name , config .StringDefault (option , "" ))
114
+
115
+ if name == "all" {
116
+ initHandlerFor (c , config .StringDefault (option , "" ), basePath , NewLogOptions (config , false , phandler ))
117
+ } else {
118
+ initHandlerFor (c , config .StringDefault (option , "" ), basePath , NewLogOptions (config , false , phandler , toLevel [name ]))
119
+ }
120
+ }
121
+ }
78
122
}
79
123
}
80
124
}
@@ -90,23 +134,6 @@ func initLogLevels(c *CompositeMultiHandler, basePath string, config *config.Con
90
134
log .Printf ("Adding standard handler %s output %s" , name , output )
91
135
initHandlerFor (c , output , basePath , NewLogOptions (config , true , nil , toLevel [name ]))
92
136
}
93
- // Now check to see if we have any module specific loggers
94
- // Names should be module.name or module.name.context.name
95
- optionList := config .Options ("log." + name + ".filter" )
96
- for _ , option := range optionList {
97
- splitOptions := strings .Split (option , "." )
98
- keyMap := map [string ]interface {}{}
99
- for x := 3 ; x < len (splitOptions ); x += 2 {
100
- keyMap [splitOptions [x ]] = splitOptions [x + 1 ]
101
- }
102
-
103
- phandler := NewParentLogHandler (func (child LogHandler ) LogHandler {
104
- return MatchMapHandler (keyMap , child )
105
- })
106
- log .Printf ("Adding key map handler %s %s output %s" , option , name , config .StringDefault (option , "" ))
107
- initHandlerFor (c , config .StringDefault (option , "" ), basePath , NewLogOptions (config , false , phandler , toLevel [name ]))
108
- }
109
-
110
137
// Gets the list of options with said prefix
111
138
} else {
112
139
initHandlerFor (c , "stderr" , basePath , NewLogOptions (config , true , nil , toLevel [name ]))
0 commit comments