PHP code example of rcommerz / logger-laravel
1. Go to this page and download the library: Download rcommerz/logger-laravel 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/ */
rcommerz / logger-laravel example snippets
use Rcommerz\Logger\Logger;
// Get logger instance (singleton)
$logger = Logger::getInstance();
// Log messages with context
$logger->info('User logged in', ['user_id' => 'usr-123', 'email' => '[email protected] ']);
$logger->error('Payment failed', ['order_id' => 'ord-456', 'error' => 'Insufficient funds']);
$logger->debug('Cache hit', ['key' => 'user:123', 'ttl' => 3600]);
use Rcommerz\Logger\Facades\Logger;
Logger::info('Order created', ['order_id' => 'ord-789']);
Logger::security('Unauthorized access attempt', ['ip' => request()->ip()]);
Logger::audit('User role changed', ['user_id' => 'usr-123', 'old_role' => 'user', 'new_role' => 'admin']);
// app/Http/Kernel.php
protected $middleware = [
// ... other middleware
\Rcommerz\Logger\Middleware\HttpLoggerMiddleware::class,
];
// routes/api.php
Route::middleware(['api', \Rcommerz\Logger\Middleware\HttpLoggerMiddleware::class])
->group(function () {
Route::get('/users', [UserController::class, 'index']);
});
return [
'service_name' => env('SERVICE_NAME', config('app.name', 'laravel-app')),
'service_version' => env('SERVICE_VERSION', '1.0.0'),
'env' => env('APP_ENV', 'production'),
'level' => env('LOG_LEVEL', 'INFO'),
// Paths to exclude from HTTP logging
'exclude_paths' => [
'health',
'metrics',
'api/health',
'api/metrics',
],
'
$logger->debug('Debug information'); // DEBUG
$logger->info('Informational message'); // INFO
$logger->warn('Warning message'); // WARNING
$logger->error('Error occurred'); // ERROR
$logger->http('HTTP request'); // INFO with http context
$logger->security('Security event'); // WARNING with security flag
$logger->audit('Audit trail'); // INFO with audit flag
$monolog = Logger::getInstance()->getMonolog();
// Add custom handlers
$monolog->pushHandler(new \Monolog\Handler\SlackWebhookHandler($webhookUrl));
// Add custom processors
$monolog->pushProcessor(function ($record) {
$record->extra['custom_field'] = 'value';
return $record;
});
try {
processPayment($order);
} catch (\Exception $e) {
$logger->error('Payment processing failed', [
'order_id' => $order->id,
'exception' => $e // Automatically extracts: error.type, error.message, error.stack_trace
]);
}
use Monolog\Processor\UidProcessor;
$logger->getMonolog()->pushProcessor(new UidProcessor());
class CustomHttpLogger extends \Rcommerz\Logger\Middleware\HttpLoggerMiddleware
{
protected function buildContext($request, $response, $duration): array
{
$context = parent::buildContext($request, $response, $duration);
// Remove sensitive fields from body
if (isset($context['http.request.body.password'])) {
$context['http.request.body.password'] = '[REDACTED]';
}
return $context;
}
}
bash
php artisan vendor:publish --tag=rcommerz-logger-config