1. Go to this page and download the library: Download yii1tech/psr-log library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yii1tech / psr-log example snippets
// file '/public/index.php'
:setLogger(
\yii1tech\psr\log\Logger::new()
->setPsrLogger(function () {
// use Monolog as internal PSR logger:
$log = new \Monolog\Logger('yii');
$log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning));
return $log;
})
->enableYiiLog(true) // whether to continue passing logs to standard Yii log mechanism or not
);
// create and run Yii application:
Yii::createWebApplication($config)->run();
use Psr\log\LogLevel;
Yii::log('psr message', LogLevel::INFO, 'psr-category'); // same as `Yii::log('psr message', CLogger::LEVEL_INFO, 'psr-category');`
Yii::log('context message', LogLevel::INFO, ['category' => 'context-category']); // same as `Yii::log('context message', CLogger::LEVEL_INFO, 'context-category');`
Yii::log('context message', LogLevel::INFO, [
'foo' => 'bar', // specifying log context, which will be passed to the related PSR logged, and added as JSON to the Yii log message, if it is enabled
]);
try {
// ...
} catch (\Throwable $exception) {
Yii::log('context exception', LogLevel::ERROR, [
'exception' => $exception, // exception data such as class, message, file, line and so on will be logged
]);
}
// set custom logger:
Yii::setLogger(
\yii1tech\psr\log\Logger::new()
->setPsrLogger(function () {
// ...
})
->withGlobalContext(function () {
$context = [];
// log remote IP address if available:
if (!empty($_SERVER['REMOTE_ADDR'])) {
$context['ip'] = $_SERVER['REMOTE_ADDR'];
}
// log authenticated user ID, if available:
$webUser = Yii::app()->getComponent('user', false);
if ($webUser !== null && !$webUser->getIsGuest()) {
$context['auth_user_id'] = $webUser->getId();
}
return $context;
})
);
use Psr\Log\LoggerInterface;
use vendor\daemon\DaemonApplication;
$daemon = new DaemonApplication(Yii::app()->getComponent(LoggerInterface::class));
// ...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.