1. Go to this page and download the library: Download migears/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/ */
$log = Logger::toStream(STDERR, LogLevel::WARNING, 'cli');
$log->error('Something went wrong');
$log = Logger::null();
$log->debug('This will be silently discarded');
// Send logs to any destination via a callable
$log = new Logger(function (string $line): void {
// e.g. send to syslog, Slack, database...
syslog(LOG_INFO, $line);
}, LogLevel::INFO, 'app');
$log->info('Hello, {name}! You have {count} messages.', [
'name' => 'Bob',
'count' => 5,
]);
// Output: [2024-01-01 12:00:00] [app] INFO: Hello, Bob! You have 5 messages.
use MiGears\Web\MiRest;
use MiGears\Log\Logger;
$rest = new MiRest(__DIR__ . '/resources', 'App\\Resources');
// Register logger as a service
$rest->set('logger', function () {
return Logger::toFile(__DIR__ . '/logs/app.log', 'debug', 'app');
});
// In a resource:
class Users extends AbstractResource
{
public function POST(Request $request): Response
{
$this->resolve('logger')->info('User created', ['id' => $userId]);
return Response::json(['status' => 'ok']);
}
}
use MiGears\Web\MiRest;
use MiGears\Log\Logger;
$rest = new MiRest(__DIR__ . '/resources', 'App\\Resources');
// 将日志器注册为服务
$rest->set('logger', function () {
return Logger::toFile(__DIR__ . '/logs/app.log', 'debug', 'app');
});
// 在资源类中:
class Users extends AbstractResource
{
public function POST(Request $request): Response
{
$this->resolve('logger')->info('用户已创建', ['id' => $userId]);
return Response::json(['status' => 'ok']);
}
}
[2024-01-15 10:30:45] [app] INFO: User Alice logged in
[2024-01-15 10:31:02] [app] WARNING: Rate limit approaching for user 123
[2024-01-15 10:31:10] [db] ERROR: Connection timeout on host db2