November 20, 2016 —John Koster
logger($message = null, $context = [])
The logger
helper function is a versatile function. If the $message
is null
, which it is by default, the logger
function will return an implementation of Illuminate\Contracts\Logging\Log
, which by default is an instance of \Illuminate\Log\Writer
class.
When the logger
function is not being used to retrieve a Log
instance, a $message
can be set and it will be written to Laravel's log files. An optional $context
can also be supplied. The $context
must be an array, although the actual elements of the array do not have to be arrays themselves.
The following example shows the usage of the logger
helper function when supplying a $message
and $context
. An example of what the function calls would produce in the log files follows the code examples.
1<?php 2 3// An example message context. 4$context = ['name' => 'John Doe', 'email' => 'you@homestead']; 5 6// A log message without context. 7logger('This is a log message without context'); 8 9// A log message where the context is an array.10logger('This is a log message', $context);11 12// A log message where the context is an array of objects.13logger('This is another log message', [(object) $context]);
The above code would produce results similar to the following (some lines have been indented to improve readability and prevent wrapping):
1...2[2015-06-15 02:21:13] local.DEBUG: This is a log message without context3[2015-06-15 02:21:13] local.DEBUG: This is a log message4 {"name":"John Doe","email":"you@homestead"}5[2015-06-15 02:21:13] local.DEBUG: This is another log message6 [7 "[object] (stdClass: {\"name\":\"John Doe\",\"email\":\"you@homestead\"})"8 ]9...
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.