You are viewing documentation for an outdated version. It is no longer supported!

Logging

Some of Laravel's services depend on the Loggingopen in new window package[1]. Should you stumble across such a dependency, then you might be required to install it, in order to get the service to work as intended.

[1]: The illuminate/log package uses MonoLogopen in new window.

How to install

composer require illuminate/log

Logger Configuration

Copy the logging.php configuration file from Laravel's Repositoryopen in new window, and place it within your /configs directory. You can read more about the configuration in Laravel's documentationopen in new window.

Register LogServiceProvider

Add the class path to LogServiceProvider in your providers array, in your /configs/app.php configuration file.

<?php
return [
    // ... previous not shown ...

    'providers' => [
        \Illuminate\Log\LogServiceProvider::class,
    ],

    // ... remaining not shown ...
];

At this point, you should have logging available in your application.

Folder Permissions

If you chose a log-profile that stores log entries in files, then please ensure that storage directory has the correct permissions. For instance, if you chose to store log-files in the /storage/logs directory, you could change the permissions to the following:

chown -R www-data:www-data /storage/logs

The above example applies to Linux environments. Please seek appropriate guidance regarding read/write permissions, if you work on a difference type of server environment.

Usage

Via $app

If your $app is available, then use the make() method to obtain the logger instance.

<?php
// Obtain logger instance
$logger = $app->make('log');

$logger->info('Logger works great');

Via Log Facade

You can also use Laravel's Log Facadeopen in new window.

<?php
use Illuminate\Support\Facades\Log;

Log::warning('User is missing contact information', [ 'user' => $user ]);

Via Aware-of Helper

Alternatively, you can make use of the Log Aware-of Helper.

<?php
use Aedart\Support\Helpers\Logging\LogTrait;

class DeviceRegistrationController
{
    use LogTrait;

    public function index()
    {
        // ... previous not shown ...

        $logger = $this->getLog();

        $logger->debug('Device requesting registration', [ 'device' => '...' ]);

        // ... Remaining not shown ...
    }
}