No description
Find a file
Joas Schilling 82ba062180
chore(release): Changelog for 2.0.0
Signed-off-by: Joas Schilling <coding@schilljs.com>
2025-10-24 15:47:11 +02:00
.github ci(lint): Run PHP lint on all versions we claim to support 2025-10-10 11:51:40 +02:00
LICENSES feat(reuse): Add REUSE 2025-10-09 17:04:59 +02:00
src Merge pull request #16 from nextcloud/bugfix/9/remove-reference 2025-10-24 15:42:35 +02:00
tests Merge pull request #16 from nextcloud/bugfix/9/remove-reference 2025-10-24 15:42:35 +02:00
.editorconfig ci(phpunit): Run PHPUnit on CI 2025-10-04 12:25:23 +02:00
.gitignore feat(reuse): Add REUSE 2025-10-09 17:04:59 +02:00
.php-cs-fixer.dist.php fix(cs): Apply coding standards to test 2025-10-24 15:32:38 +02:00
CHANGELOG.md chore(release): Changelog for 2.0.0 2025-10-24 15:47:11 +02:00
composer.json ci(phpunit): Run PHPUnit on CI 2025-10-04 12:25:23 +02:00
COPYING Initial commit 2015-04-24 23:30:38 +02:00
phpunit.xml ci(phpunit): Run PHPUnit on CI 2025-10-04 12:25:23 +02:00
psalm.xml fix(format): Don't modify the $data by reference 2025-10-10 16:23:27 +02:00
README.md chore(release): Changelog for 2.0.0 2025-10-24 15:47:11 +02:00
REUSE.toml feat(reuse): Add REUSE 2025-10-09 17:04:59 +02:00

Log Normalizer

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

Based on the Monolog formatter/normalizer.

How to use

Initialization 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);