|
6 | 6 |
|
7 | 7 | 
|
8 | 8 |
|
| 9 | +# Discussion: Compositional Loggers |
| 10 | + |
| 11 | +LoggingExtras is designs around allowing you to build arbitrarily complicated |
| 12 | +systems for "log plumbing". That is to say basically routing logged information to different places. |
| 13 | +It is built around the idea of simple parts which are composed together, |
| 14 | +to allow for powerful and flexible definition of your logging system. |
| 15 | +Without having to define any custom loggers by subtyping `AbstractLogger`. |
| 16 | +When we talk about composability we mean to say that the composition of any set of Loggers is itself a Logger. |
| 17 | +LoggingExtras is a composable logging system. |
| 18 | + |
| 19 | +Loggers can be broken down into 4 types: |
| 20 | + - *Sinks*: Sinks are the final end point of a log messages journey. They write it to file, or display it on the console, or set off a red flashing light in the laboratory. A Sink should never decide what to accept, only what to do with it. |
| 21 | + - *Filters*: Filters wrap around other loggers and decide wether or not to pass on a message. Thery can further be broken down by when that decision occurs (See `ActiveFilteredLogger` vs `EarlyFilteredLogger`). |
| 22 | + - *Transformers*: Transformers modify the content of log messages, before passing them on. This includes the metadata like severity level. Unlike Filters they can't block a log message, but they could drop its level down to say `Debug` so that normally noone would see it. |
| 23 | + - *Demux*: There is only one possible Demux Logger. and it is central to log routing. It acts as a hub that recieves 1 log message, and then sends copies of it to all its child loggers. Like iin the diagram above, it can be composed with Filters to control what goes where. |
| 24 | + |
| 25 | +This is a basically full taxonomy of all compositional loggers. |
| 26 | +Other than `Sinks`, this package implements the full set. So you shouldn't need to build your own routing components, just configure the ones included in this package. |
| 27 | + |
| 28 | +It is worth understanding the idea of logging purity. |
| 29 | +The loggers defined in this package are all pure. |
| 30 | +The Filters, only filter, the Sinks only sink, the transformers only Transform. |
| 31 | + |
| 32 | +We can contrast this to the the `ConsoleLogger` (the standard logger in the REPL). |
| 33 | +The `ConsoleLogger` is an in-pure sink. |
| 34 | +As well as displaying logs to the user (as a Sink); |
| 35 | +it uses the log content, in the form of the `max_log` kwarg to decide if a log should be displayed (Active Filtering); |
| 36 | +and it has a min_enabled_level setting, that controls if it will accept a message at all |
| 37 | +(Early Filtering, in particular see `MinLevelLogger`). |
| 38 | +If it was to be defined in a compositional way, |
| 39 | +we would write; |
| 40 | +``` |
| 41 | +
|
| 42 | +const ConsoleLogger(stream, min_level) = |
| 43 | + MinLevelLogger( |
| 44 | + ActiveFilteredLogger(max_log_filter, |
| 45 | + PureConsoleLogger(stream) |
| 46 | + ), |
| 47 | + min_level |
| 48 | + ) |
| 49 | +``` |
9 | 50 |
|
10 |
| -## Usage |
| 51 | + |
| 52 | +# Usage |
11 | 53 | Load the package with `using LoggingExtras`.
|
12 | 54 | You likely also want to load the `Logging` standard lib.
|
13 | 55 | Loggers can be constructed and used like normal.
|
|
277 | 319 | global_logger(transformer_logger)
|
278 | 320 | ```
|
279 | 321 |
|
| 322 | + |
0 commit comments