2015-04-25 21:33:51 +03:00
|
|
|
## Log Normalizer [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/interfasys/lognormalizer/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/interfasys/lognormalizer/?branch=master)
|
2015-04-25 00:54:21 +03:00
|
|
|
Parses variables and converts them to string so that they can be logged
|
|
|
|
|
2015-04-25 16:25:46 +03:00
|
|
|
Based on the [Monolog](https://github.com/Seldaek/monolog) normalizer.
|
|
|
|
|
|
|
|
## How to use
|
|
|
|
|
|
|
|
### Initialisation in your class
|
|
|
|
|
2015-04-25 22:13:06 +03:00
|
|
|
```php
|
2015-04-25 16:25:46 +03:00
|
|
|
use InterfaSys\LogNormalizer\Normalizer;
|
|
|
|
|
|
|
|
$normalizer = new Normalizer();
|
|
|
|
```
|
|
|
|
|
|
|
|
The constructor supports the following optional arguments
|
|
|
|
|
|
|
|
* `int $maxObjectDepth`: How deep do you want to go when inspecting objects
|
|
|
|
* `int $maxArrayItems`: The maximum number of Array elements you want to show, when parsing an array
|
|
|
|
* `string $dateFormat`: The format to apply to dates
|
|
|
|
|
|
|
|
### Normalize a log entry
|
|
|
|
|
2015-04-25 16:58:18 +03:00
|
|
|
This is what your logging function could look like
|
|
|
|
|
2015-04-25 22:13:06 +03:00
|
|
|
```php
|
2015-04-25 16:58:18 +03:00
|
|
|
/**
|
|
|
|
* Converts the variables in the received log message to string before
|
|
|
|
* sending everything to the real logger
|
|
|
|
*
|
|
|
|
* @param string $level
|
|
|
|
* @param string $message
|
|
|
|
* @param array $variables
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function log($level, $message, array $variables= []) {
|
|
|
|
array_walk($variables, [$this->normalizer, 'format']);
|
2015-04-25 16:25:46 +03:00
|
|
|
|
2015-04-25 16:58:18 +03:00
|
|
|
// Then use your current PSR-3 compatible logging system
|
|
|
|
$this->logger->log($level, $message, $variables);
|
2015-04-25 16:25:46 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-04-25 16:58:18 +03:00
|
|
|
And you would call it like this from another class
|
|
|
|
|
2015-04-25 22:13:06 +03:00
|
|
|
```php
|
2015-04-25 17:08:11 +03:00
|
|
|
$myLogger->log('debug',
|
2015-04-25 17:05:19 +03:00
|
|
|
'Logger test {var1}, {var2}',
|
|
|
|
[
|
|
|
|
'var1' => $var1,
|
|
|
|
'var2' => $var2
|
|
|
|
]
|
2015-04-25 17:08:11 +03:00
|
|
|
);
|
|
|
|
```
|
2015-04-25 16:58:18 +03:00
|
|
|
|
2015-04-25 16:25:46 +03:00
|
|
|
### Normalize a single variable
|
|
|
|
|
2015-04-25 22:13:06 +03:00
|
|
|
```php
|
2015-04-25 16:25:46 +03:00
|
|
|
$normalizedVariable = $this->normalizer->format($variable);
|
|
|
|
```
|