Skip to content

kronostechnologies/kronos-log

Repository files navigation

kronos-log

PSR-3 Compliant implementation.

See php-fig/log for more information on root classes and interfaces.

Usage

use Kronos\Log;

$logger = new Log\Logger();

$debug = new Log\Writer\File('/var/log/debug.log', new Kronos\Log\Adaptor\FileFactory());
$logger->addWriter($debug);

...

$logger->info('Need more {drink}', ['drink' => 'coffee']);

This is a simple usage of a logger with a writer which output into given files.

It's possible to use multiple writer accepting only some log levels.

use Kronos\Log;

$logger = new Log\Logger();
$logger->addContext('user', $current_user);

$debug = new Log\Writer\File('/var/log/debug.log');
$debug->setMaxLevel(\Psr\Log\LogLevel::WARNING);
$logger->addWriter($debug);

$syslog = new Log\Writer\Syslog('application-name');
$syslog->setMinLevel(\Psr\Log\LogLevel::ERROR);
$logger->addWriter($syslog);

...

try {
    $logger->debug('Trying something with {value}', ['value' => $some_variable]);
    
    // Something
}
catch(\Exception $e) {
    $logger->error('Something did not work', ['exception' => $e]);
}

Here the info will be writen in /var/log/debug.log while the error will be sent to syslog.

Using the builder

You can use the builder to quickly create a fully configured logger.

$settings = [
	[
		'type' => \Kronos\Log\Enumeration\WriterTypes::FILE,
		'settings' => [
			'filename' => '/var/log/debug.log'
		]
	],
	[
		'type' => \Kronos\Log\Enumeration\WriterTypes::SENTRY,
		'settings' => [
			'minLevel' => \Psr\Log\LogLevel::ERROR,
			'key' => 'kronos',
			'secret' => 'supersecrethash',
			'projectId' => 12345
		]
	],
	[
		'type' => \Kronos\Log\Enumeration\WriterTypes::LOGDNA,
		'settings' => [
			'minLevel' => \Psr\Log\LogLevel::INFO,
			'hostname' => gethostname(),
			'application' => 'app-name',
			'ingestionKey' => 'anothersecrethash'
		]
	]
];

$builder = new \Kronos\Log\Builder();
$logger = $builder->buildFromArray($settings);