Nikita Yunoshev
Nikita Yunoshev

Reputation: 409

Logging in Laravel 5.5

I have a very simple question, I guest, but I can't find an answer to it in a documentation.

Here's how I log info now:

    $logger->debug('user_id: ', [Auth::id()]);
    $logger->debug('requested_url: ', [$request->getRequestUri()]);
    $logger->debug('requested_method: ', [$request->getMethod()]);
    $logger->debug('referer_url: ', [$request->headers->get('referer')]);
    $logger->debug('date_and_time: ', [Carbon::now()->toDateTimeString()]);
    $logger->debug('error_message: ', [$exception->getMessage()]);
    $logger->debug('http_status_code: ', [$this->prepareResponse($request, $exception)->getStatusCode()]);
    $logger->debug($exception->getMessage(), ['stack_trace' => $exception->getTraceAsString()]);

But I need to make it in a one line. I want see in the logs 1 message related to an error.

[2018-02-15 13:31:19] local.DEBUG: user_id:  [null]
[2018-02-15 13:31:19] local.DEBUG: requested_url:["/css/bootstrap.css.map"]
[2018-02-15 13:31:19] local.DEBUG: requested_method:  ["GET"]
[2018-02-15 13:31:19] local.DEBUG: referer_url:  [null]
[2018-02-15 13:31:19] local.DEBUG: date_and_time:  ["2018-02-15 13:31:19"]
[2018-02-15 13:31:19] local.DEBUG: error_message:  [""]
[2018-02-15 13:31:19] local.DEBUG: http_status_code:  [404]
[2018-02-15 13:31:19] local.DEBUG:  {"stack_trace":"#0 }

How can I manage it?

Upvotes: 2

Views: 2277

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

From the docs:

An array of contextual data may also be passed to the log methods. This contextual data will be formatted and displayed with the log message:

Log::info('User failed to login.', ['id' => $user->id]);

So, you could do this:

$logger->debug('The error message', [
    'user_id' => Auth::id(),
    'requested_url' => $request->getRequestUri(),
    ....
]);

Upvotes: 1

Related Questions