如何将会话ID添加到Laravel 5.2中的所有日志条目

时间:2016-03-30 18:35:01

标签: php laravel monolog

我知道我可以使用TagProcessor

向Monolog添加属性
$log->getMonolog()->pushProcessor(
    new TagProcessor(
        array(
            'session: ' => session_id(),
            'session2'  => Session::getId()
        )
    )
);

我应该扩展Illuminate \ Foundation \ Bootstrap \ ConfigureLogging。 这工作正常,但是当我尝试添加会话ID时,我收到错误。 似乎在生成记录器时会话和请求对象不可用?

[2016-03-30 18:52:21] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Class 'Session' not found in /mywebapp/bootstrap/ConfigureLogging.php:43
Stack trace:
#0 /mywebapp/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/ConfigureLogging.php(60): Bootstrap\ConfigureLogging->configureTechOpsHandler(Object(Illuminate\Foundation\Application), Object(Illuminate\Log\Writer))
#1 /mywebapp/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/ConfigureLogging.php(30): Illuminate\Foundation\Bootstrap\ConfigureLogging->configureHandlers(Object(Illuminate\Foundation\Application), Object(Illuminate\Log\Writer))
#2 /mywebapp/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(203): Illuminate\Foundation\Bootstrap\ConfigureLogging->bootstrap(Object(Illuminate\Foundation\Application))
#3 /mywebapp/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(232): Illuminate\Foundation\Application->bootstrapWith(Array)
#4 /mywebapp/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(127): Illuminate\Foundation\Http\Kernel->bootstrap()
#5 /mywebapp/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#6 /mywebapp/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#7 {main}

我的自定义ConfigureLogging.php文件的要点:https://gist.github.com/quixand/323227a08ede13e1536e51f37000674b

2 个答案:

答案 0 :(得分:1)

我们在ConfigureLogging.php

中最后使用了以下内容
<?php namespace Bootstrap;

use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger as Monolog;

class ConfigureLogging extends BaseConfigureLogging {

    /**
     * Configure the Monolog handlers for the application.
     *
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureDailyHandler(Application $app, Writer $log)
    {
        // Stream handlers
        $logPath = $app->storagePath().'/logs/laravel.log';

        $logStreamHandler = new RotatingFileHandler(
            $logPath,
            $app->make('config')->get('app.log_max_files', 5));

        // Adds Username to each log line
        $log->getMonolog()->pushProcessor( function ($record) {
            $record['extra']['user'] = \Session::get("username");
            $record['extra']['sessionId'] = \Session::getId();
            return $record;
        });

        // Formatting
        // the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
        $logFormat = "%datetime% [%level_name%] (%channel%) %extra%: %message% %context%\n";
        $formatter = new LineFormatter($logFormat, null, true, true);
        $logStreamHandler->setFormatter($formatter);

        // push handlers
        $logger = $log->getMonolog();

        $logger->pushHandler($logStreamHandler);
    }
}

答案 1 :(得分:0)

您在文件顶部缺少use Session;