Composer package for Laravel 5 that adds a few additional log files as well as facades to write to them.
Writing to laravel.log
is fairly easy but sometimes it is a bit cumbersome to write to additional logs.
To install from Composer, use the following command:
composer require csun-metalab/laravel-multiple-logs
Now, add the following optional line(s) to your .env
file:
# Number of days for which the "daily" logs should be kept; default is 7
LOG_MAX_DAYS=7
Next, add the service provider to your providers
array in Laravel as follows:
'providers' => [
//...
CSUNMetaLab\MultipleLogs\Providers\LoggingServiceProvider::class,
// You can also use the following depending on Laravel convention:
// 'CSUNMetaLab\MultipleLogs\Providers\LoggingServiceProvider',
//...
],
Add the two facades for the new loggers to your aliases
array in Laravel as follows:
'aliases' => [
//...
'AuditLog' => CSUNMetaLab\MultipleLogs\Facades\AuditLog::class,
'AuthLog' => CSUNMetaLab\MultipleLogs\Facades\AuthLog::class,
// You can also use the following depending on Laravel convention:
//'AuditLog' => 'CSUNMetaLab\MultipleLogs\Facades\AuditLog',
//'AuthLog' => 'CSUNMetaLab\MultipleLogs\Facades\AuthLog',
//...
],
Finally, run the following Artisan command to publish the configuration:
php artisan vendor:publish
The two facades you added to your config/app.php
file are the following:
This writes to a file called audit.log
in your storage/logs
directory.
This supports all of the same methods the native Log
facade does.
The matching auditLogger()
helper method also supports them as instance methods.
This writes to a file called auth.log
in your storage/logs
directory.
This supports all of the same methods the native Log
facade does.
The matching authLogger()
helper method also supports them as instance methods.
AuditLog::info("User performed some action");
AuthLog::info("User authenticated successfully");
auditLogger()->info("User performed some action");
authLogger()->info("User authenticated successfully");
AuditLog::error("User tried to perform an action unsuccessfully");
AuthLog::error("User could not authenticate");
auditLogger()->error("User tried to perform an action unsuccessfully");
authLogger()->error("User could not authenticate");
You can create your own custom file loggers by extending the Logger
class:
<?php
namespace App\Logging;
use CSUNMetaLab\MultipleLogs\Loggers\Logger;
class PurchaseLogger extends Logger
{
/**
* Constructs a new PurchaseLogger object.
*
* @param string $path The path to the log file
* @param string $logLevel Optional parameter to specify minimum log level
*/
public function __construct($path, $logLevel="debug") {
parent::__construct($path, 'purchase', $logLevel);
}
}
?>
Now, you can just construct it and immediately use the methods, for example:
<?php
use App\Logging\PurchaseLogger;
$pLogger = new PurchaseLogger(storage_path('logs/purchases.log'));
$pLogger->info("Successfully made a purchase");
?>
It is now significantly easier to add multiple log files and additional logging capabilities in Laravel.