phongtran / logger
A package which provides a simple logger
Installs: 150
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
pkg:composer/phongtran/logger
Requires
- php: ^8.0
- laravel/framework: ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- orchestra/testbench: ^8.0 || ^9.0 || ^10.0
- phpunit/phpunit: ^10.0
README
Introduction
The Logger package provides advanced logging capabilities for Laravel applications. It allows you to easily record messages at various levels (such as warning, error, debug, etc.), log SQL queries, and store logs through a custom service. This package supports multiple logging channels, formats backtrace information, and integrates smoothly with Laravel's existing logging system.
Installation
composer require phongtran/logger
After installation, you can publish the configuration file if needed:
php artisan vendor:publish --tag=logger
Configuration
Make sure the required parameters are configured in the config/logger.php or .env file. You can add custom channels or use the default channels available in the package.
ENABLE_QUERY_DEBUGGER=true
Usage
Logging Messages
To log various messages, use the Logger class. The package supports logging at different levels such as warning, error, debug, and info.
Log a Warning Message
use phongtran\Logger\Logger; Logger::warning('This is a warning message.');
Log an Exception Message
use phongtran\Logger\Logger; Logger::exception('Error occurred: Database connection failed.');
Log a Fatal Error Message
use phongtran\Logger\Logger; Logger::fatal('A critical error occurred while processing the request.');
Log Debug Information
use phongtran\Logger\Logger; Logger::debug('Debugging application state: User has logged in.');
Log an Info Message
use phongtran\Logger\Logger; Logger::info('User profile updated successfully.');
Log SQL Queries
To log SQL queries, you just need to add the configuration in the .env file. The Logger will listen to all queries and store them in the log_queries table (by default).
ENABLE_QUERY_DEBUGGER=true
Log Activities
Log all application requests, including the URL, parameters, response, and the execution time of the request. The activity log is configured in the activity middleware. To activate this middleware, you just need to add it to your routes.
Route::group(['middleware' => 'activity'], function () { Route::get('/', [HomeController::class, 'index']); });
HTTP Exception
Add this line in Handle Exceptions (bootstrap/app.php)
return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { }) ->withExceptions(function (Exceptions $exceptions) { \phongtran\Logger\LoggerHandler::handle($exceptions); //Log exceptions })->create();
Advanced Features
Backtrace Formatting
The Logger automatically adds backtrace information (file and line number) to logs to make debugging easier. This allows you to track the location where the log is generated in the code.
Example of a log message with backtrace:
<app/Http/Controllers/HomeController.php (Line:42)> This is a debug message.
This helps you pinpoint exactly where the log message was triggered in your code.
Contributions
We welcome contributions from the community! If you would like to contribute, please fork the repository and submit a pull request with your improvements or bug fixes
License
This package is licensed under the MIT license. Please refer to the LICENSE file for more details.
Author
Phong Tran
Email: jarvis.phongtran@gmail.com
GitHub: github/mockingbitch