csun-metalab/laravel-multiple-logs

Composer package for Laravel 5 that adds a few additional log files as well as facades to write to them.
2,403
Install
composer require csun-metalab/laravel-multiple-logs
Latest Version:1.0.1
PHP:>=5.5.9
License:MIT
Last Updated:Feb 25, 2024
Links: GitHub  ·  Packagist
Maintainer: matthewfritz

Laravel Multiple Logs

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.

Table of Contents

Installation

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

Facades

The two facades you added to your config/app.php file are the following:

AuditLog

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.

AuthLog

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.

Usage Examples

Writing INFO Data

AuditLog::info("User performed some action");
AuthLog::info("User authenticated successfully");

auditLogger()->info("User performed some action");
authLogger()->info("User authenticated successfully");

Writing ERROR Data

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");

Creating Custom File Loggers

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.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

637,827,751 9,703
laravel/framework

The Laravel Framework.

589,172,971 34,950
laravel/tinker

Powerful REPL for the Laravel framework.

498,216,183 7,442
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

413,412,504 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

394,964,158 4,660

Version History

Version Released PHP License
1.0.1 >=5.5.9 MIT
1.0.0 >=5.5.9 MIT