Converts variables to String for easy logging
Перейти к файлу
Morris Jobke 14d8e17f52
Merge pull request #7 from nextcloud/enhancement/psalm
Add Psalm
2020-12-03 09:27:46 +01:00
src Add Psalm 2020-12-02 19:17:31 +01:00
tests Add strict typing and more type hints 2020-12-02 10:44:59 +01:00
.gitignore Add Nextcloud coding style 2020-12-02 15:29:11 +01:00
.php_cs.dist Add Nextcloud coding style 2020-12-02 15:29:11 +01:00
.travis.yml Add Psalm 2020-12-02 19:17:31 +01:00
CHANGELOG.md Initial commit 2015-04-24 23:30:38 +02:00
COPYING Initial commit 2015-04-24 23:30:38 +02:00
README.md Rebrand to Nextcloud 2020-12-02 09:52:41 +01:00
composer.json Add Psalm 2020-12-02 19:17:31 +01:00
phpunit.xml Listen to static analysers 2015-08-01 18:27:37 +02:00
psalm.xml Add Psalm 2020-12-02 19:17:31 +01:00

README.md

Log Normalizer

Parses variables and converts them to string so that they can be logged

Based on the Monolog formatter/normalizer.

How to use

Initialisation in your class

use Nextcloud\LogNormalizer\Normalizer;

$normalizer = new Normalizer();

The constructor supports the following optional arguments

  • int $maxRecursionDepth: The maximum depth at which you want to go in objects and arrays
  • int $maxArrayItems: The maximum number of elements you want to show, when parsing an array or an object
  • string $dateFormat: The format to apply to dates

Format variables before logging them

This is what your logging function could look like

/**
 * 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']);
	
	// Then use your current PSR-3 compatible logging system
	$this->logger->log($level, $message, $variables);
}

And you would call it like this from another class

$myLogger->log('debug',
	'Logger test {var1}, {var2}',
	[
		'var1' => $var1,
		'var2' => $var2
		]
);

Convert a single variable to a string

$normalizedVariable = $this->normalizer->format($variable);