Skip to content

Commit 9e0052d

Browse files
committed
update README
1 parent 1222f81 commit 9e0052d

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

README.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ Or you can do, by wrapping them in a filtered logger as discussed below.
6666

6767
The `FileLogger` does logging to file.
6868
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)
7072

7173
### Demo
7274
We are going to log info and above to one file,
@@ -76,18 +78,18 @@ and warnings and above to another.
7678
julia> using Logging; using LoggingExtras;
7779
7880
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+
);
8385
8486
8587
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
9193
9294
shell> cat warn.log
9395
┌ Warning: It is bad
@@ -104,51 +106,65 @@ shell> cat info.log
104106
└ @ Main REPL[34]:4
105107
```
106108

107-
## `FilteredLogger`
109+
## `ActiveFilteredLogger`
108110

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.
110112
It warps any logger, and before sending messages to the logger to log,
111113
checks them against a filter function.
112114
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.)
114116

115117
### Demo
116118
We want to filter to only log strings staring with `"Yo Dawg!"`.
117119

118120
```
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!")
121123
end
122124
yodawg_filter (generic function with 1 method)
123125
124-
julia> filtered_logger = FilteredLogger(yodawg_filter, global_logger());
126+
julia> filtered_logger = ActiveFilteredLogger(yodawg_filter, global_logger());
125127
126128
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
132134
┌ Warning: Yo Dawg! it is bad
133135
└ @ Main REPL[28]:3
134136
[ Info: Yo Dawg! it is all good
135137
```
136138

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.
137150

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.
138154

139-
# Examples
155+
# More Examples
140156

141157
## Filter out any overly long messages
142158

143159
```
144160
using LoggingExtras
145161
using Logging
146162
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
149165
end
150166
151-
global_logger(FilteredLogger(sensible_message_filter, global_logger()))
167+
global_logger(ActiveFilteredLogger(sensible_message_filter, global_logger()))
152168
```
153169

154170

@@ -159,10 +175,10 @@ using LoggingExtras
159175
using Logging
160176
using HTTP
161177
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
164180
end
165181
166-
global_logger(FilteredLogger(not_HTTP_message_filter, global_logger()))
182+
global_logger(EarlyFilteredLogger(not_HTTP_message_filter, global_logger()))
167183
```
168184

0 commit comments

Comments
 (0)