1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-19 11:21:27 +02:00

Code cleanups

This commit is contained in:
Jordi Boggiano
2022-03-05 14:03:57 +01:00
parent b586dbe8e6
commit 5eb9b8ed93
109 changed files with 418 additions and 549 deletions

View File

@@ -140,7 +140,7 @@ write a processor adding some dummy data in the record:
<?php
$logger->pushProcessor(function ($record) {
$record['extra']['dummy'] = 'Hello world!';
$record->extra['dummy'] = 'Hello world!';
return $record;
});
@@ -190,23 +190,23 @@ $securityLogger = $logger->withName('security');
## Customizing the log format
In Monolog it's easy to customize the format of the logs written into files,
sockets, mails, databases and other handlers; by the use of "Formatters".
sockets, mails, databases and other handlers; by the use of "Formatters".
As mentioned before, a *Formatter* is attached to a *Handler*, and as a general convention, most of the handlers use the
```php
$record['formatted']
$record->formatted
```
field in the log record to store its formatted value. Again, this field depends on the implementation of the *Handler* but is a good idea to **stick into the good practices and conventions of the project**.
You can choose between predefined formatter classes or write your own (e.g. a multiline text file for human-readable output).
> Note:
>
>
> A very useful formatter to look at, is the `LineFormatter`.
>
>
> This formatter, as its name might indicate, is able to return a lineal string representation of the log record provided.
>
> It is also capable to interpolate values from the log record, into the output format template used by the formatter to generate the final result, and in order to do it, you need to provide the log record values you are interested in, in the output template string using the form %value%, e.g: "'%context.foo% => %extra.foo%'", in this example the values $record["context"]["foo"] and $record["extra"]["foo"] will be rendered as part of the final result.
> It is also capable to interpolate values from the log record, into the output format template used by the formatter to generate the final result, and in order to do it, you need to provide the log record values you are interested in, in the output template string using the form %value%, e.g: "'%context.foo% => %extra.foo%'" , in this example the values $record->context["foo"] and $record->extra["foo"] will be rendered as part of th final result.
In the following example, we demonstrate how to:
1. Create a `LineFormatter` instance and set a custom output format template.

View File

@@ -43,10 +43,10 @@ class PDOHandler extends AbstractProcessingHandler
}
$this->statement->execute(array(
'channel' => $record['channel'],
'level' => $record['level'],
'message' => $record['formatted'],
'time' => $record['datetime']->format('U'),
'channel' => $record->channel,
'level' => $record->level,
'message' => $record->formatted,
'time' => $record->datetime->format('U'),
));
}
@@ -78,6 +78,6 @@ $logger->info('My logger is now ready');
The `Monolog\Handler\AbstractProcessingHandler` class provides most of the
logic needed for the handler, including the use of processors and the formatting
of the record (which is why we use ``$record['formatted']`` instead of ``$record['message']``).
of the record (which is why we use ``$record->formatted`` instead of ``$record->message``).
&larr; [Utility classes](03-utilities.md)