@@ -66,7 +66,9 @@ Or you can do, by wrapping them in a filtered logger as discussed below.
66
66
67
67
The ` FileLogger ` does logging to file.
68
68
It is really simple.
69
- It takes a filename.
69
+ It takes a filename,
70
+ - a kwarg to check if should ` always_flush ` (default: ` true ` ).
71
+ - a kwarg to ` append ` rather than overwrite (default ` false ` . i.e. overwrite by default)
70
72
71
73
### Demo
72
74
We are going to log info and above to one file,
@@ -76,18 +78,18 @@ and warnings and above to another.
76
78
julia> using Logging; using LoggingExtras;
77
79
78
80
julia> demux_logger = DemuxLogger(
79
- MinLevelLogger(FileLogger("info.log"), Logging.Info),
80
- MinLevelLogger(FileLogger("warn.log"), Logging.Warn),
81
- include_current_global=false
82
- );
81
+ MinLevelLogger(FileLogger("info.log"), Logging.Info),
82
+ MinLevelLogger(FileLogger("warn.log"), Logging.Warn),
83
+ include_current_global=false
84
+ );
83
85
84
86
85
87
julia> with_logger(demux_logger) do
86
- @warn("It is bad")
87
- @info("normal stuff")
88
- @error("THE WORSE THING")
89
- @debug("it is chill")
90
- end
88
+ @warn("It is bad")
89
+ @info("normal stuff")
90
+ @error("THE WORSE THING")
91
+ @debug("it is chill")
92
+ end
91
93
92
94
shell> cat warn.log
93
95
┌ Warning: It is bad
@@ -104,51 +106,65 @@ shell> cat info.log
104
106
└ @ Main REPL[34]:4
105
107
```
106
108
107
- ## ` FilteredLogger `
109
+ ## ` ActiveFilteredLogger `
108
110
109
- The ` FilteredLogger ` exists to give more control over which messages should be logged.
111
+ The ` ActiveFilteredLogger ` exists to give more control over which messages should be logged.
110
112
It warps any logger, and before sending messages to the logger to log,
111
113
checks them against a filter function.
112
114
The filter function takes the full set of parameters of the message.
113
- (See it's docstring with ` ?FilteredLogger ` for more details.)
115
+ (See it's docstring with ` ?ActiveFilteredLogger ` for more details.)
114
116
115
117
### Demo
116
118
We want to filter to only log strings staring with ` "Yo Dawg!" ` .
117
119
118
120
```
119
- julia> function yodawg_filter(level, message, _module, group, id, file, line; kwargs... )
120
- startswith(msg , "Yo Dawg!")
121
+ julia> function yodawg_filter(log_args )
122
+ startswith(log_args.message , "Yo Dawg!")
121
123
end
122
124
yodawg_filter (generic function with 1 method)
123
125
124
- julia> filtered_logger = FilteredLogger (yodawg_filter, global_logger());
126
+ julia> filtered_logger = ActiveFilteredLogger (yodawg_filter, global_logger());
125
127
126
128
julia> with_logger(filtered_logger) do
127
- @info "Boring message"
128
- @warn "Yo Dawg! it is bad"
129
- @info "Another boring message"
130
- @info "Yo Dawg! it is all good"
131
- end
129
+ @info "Boring message"
130
+ @warn "Yo Dawg! it is bad"
131
+ @info "Another boring message"
132
+ @info "Yo Dawg! it is all good"
133
+ end
132
134
┌ Warning: Yo Dawg! it is bad
133
135
└ @ Main REPL[28]:3
134
136
[ Info: Yo Dawg! it is all good
135
137
```
136
138
139
+ ## ` EarlyFilteredLogger `
140
+
141
+ The ` EarlyFilteredLogger ` is similar to the ` ActiveFilteredLogger ` ,
142
+ but it runs earlier in the logging pipeline.
143
+ In particular it runs before the message is computed.
144
+ It is (theoretically) useful to filter things early if creating the log message is expective.
145
+ E.g. if it includes summary statistics of the error.
146
+ The filter function for early filter logging only has access to the
147
+ ` level ` , ` _module ` , ` id ` and ` group ` fields of the log message.
148
+ The most notable use of it is to filter based on modules,
149
+ see the HTTP example below.
137
150
151
+ ## ` MinLevelLogger `
152
+ This is basically a special case of the early filtered logger,
153
+ that just checks if the level of the message is above the level specified when it was created.
138
154
139
- # Examples
155
+ # More Examples
140
156
141
157
## Filter out any overly long messages
142
158
143
159
```
144
160
using LoggingExtras
145
161
using Logging
146
162
147
- function sensible_message_filter(level, message, _module, group, id, file, line; kwargs... )
148
- length(message) < 1028
163
+ function sensible_message_filter(log )
164
+ length(log. message) < 1028
149
165
end
150
166
151
- global_logger(FilteredLogger (sensible_message_filter, global_logger()))
167
+ global_logger(ActiveFilteredLogger (sensible_message_filter, global_logger()))
152
168
```
153
169
154
170
@@ -159,10 +175,10 @@ using LoggingExtras
159
175
using Logging
160
176
using HTTP
161
177
162
- function not_HTTP_message_filter(level, message, _module, group, id, file, line; kwargs... )
163
- _module != HTTP
178
+ function not_HTTP_message_filter(log )
179
+ log. _module != HTTP
164
180
end
165
181
166
- global_logger(FilteredLogger (not_HTTP_message_filter, global_logger()))
182
+ global_logger(EarlyFilteredLogger (not_HTTP_message_filter, global_logger()))
167
183
```
168
184
0 commit comments