nix-logger / nix-logger-laravel
Nix Logger
Requires
- php: ^8.1
- illuminate/contracts: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0
- monolog/monolog: ^2 || ^3
- nix-logger/nix-logger: ^1.4
Requires (Dev)
- laravel/pint: ^1.13
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.5|^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Installation
Install the nix-logger/nix-logger-laravel package:
composer require nix-logger/nix-logger-laravel
Add the service provider
Add provider NixLogger\Laravel\NixLoggerServiceProvider::class
Check Version
php artisan --version
# Laravel Framework xx.xx.xx
- For Laravel < 11, update `config/app.php
// config/app.php 'providers' => [ Illuminate\Auth\AuthServiceProvider::class, NixLogger\Laravel\NixLoggerServiceProvider::class, ]
- For Laravel >= 11, update
bootstrap/providers.php
// bootstrap/providers.php return [ Illuminate\Auth\AuthServiceProvider::class, NixLogger\Laravel\NixLoggerServiceProvider::class, ];
Add the Facade
- For Laravel < 11, update
config/app.php
// config/app.php 'aliases' => [ 'NixLogger' => NixLogger\Laravel\Facades\NixLogger::class, ]
- For Laravel >= 11, update
bootstrap/app.php
// bootstrap/app.php ->withMiddleware(function (Middleware $middleware) { $middleware->alias([ 'NixLogger' => NixLogger\Laravel\Facades\NixLogger::class, ]); })
Configuration
Configure your Nix-Logger by edit the .env file
# .env NIX_LOGGER_API_KEY="" NIX_LOGGER_LEVEL=critical,error,warning LOG_CHANNEL="stack" LOG_STACK="daily,nix-logger" # APP_ENV=
If you’d like to configure Nix-Logger further, create and edit a config/nix-logger.php file
Setting Log report
In config/logging.php, add the nix-logger logging channel by adding the following under the channels key:
'stack' => [ 'driver' => 'stack', 'channels' => explode(',', env('LOG_STACK', 'single')), 'ignore_exceptions' => false, ], 'nix-logger' => [ 'driver' => 'custom', 'via' => \NixLogger\Laravel\Logger\NixLogger::class, ],
For stderr
'channels' => [ 'stderr' => [ 'driver' => 'monolog', 'level' => env('LOG_LEVEL', 'debug'), 'handler' => \NixLogger\Laravel\Handlers\NixLoggerStreamHandler::class, 'formatter' => env('LOG_STDERR_FORMATTER'), 'with' => [ 'stream' => 'php://stderr', ], 'processors' => [PsrLogMessageProcessor::class], ], ]
Console commands, queued jobs and scheduled tasks
HTTP issues carry their request. Issues reported from the console carry what was running instead, in extraData:
| Key | Filled while | Fields |
|---|---|---|
command |
an artisan command runs (queue:work, schedule:run, your batches...) |
name, input, pid, exitCode (failed command) |
job |
a queued job is processed (worker or sync connection) |
name, id, uuid, connection, queue, attempts, maxTries, timeout, batchId, pushedAt |
scheduledTask |
the scheduler runs a task | name, command, expression, timezone, withoutOverlapping, onOneServer, runInBackground |
commandStack, jobStack |
executions are nested (Artisan::call() inside a command...) |
names, outer → inner |
{
"runningMode": "cli",
"extraData": {
"command": { "name": "queue:work", "input": "'queue:work' --queue=stock", "pid": 4182 },
"job": { "name": "App\\Jobs\\SyncStock", "id": "1532", "uuid": "9f0c...", "connection": "database", "queue": "stock", "attempts": 2, "maxTries": 3 }
}
}
Nothing to configure. To turn it off:
NIX_LOGGER_CAPTURE_EXECUTION_CONTEXT=false
A command run with $this->call() from another command is not tracked separately (Laravel fires no event for it): its issues carry the outer command. Artisan::call() is tracked.
Usage
Use NixLogger Debug
php artisan tinker
\NixLogger::error('Test Error');
Log a caught exception
Always pass the exception as exception in the context: NixLogger builds the trace from it, and keeps your message in front of the exception message.
try { $task->run(); } catch (Throwable $e) { // Issue message: "IF_TOOL_RUN_THREW: pdb_sku_jan_issue | Exception: ... in /app/...:107" Log::error('IF_TOOL_RUN_THREW: pdb_sku_jan_issue', ['exception' => $e, 'run_id' => $runId]); }
Putting $e->getTraceAsString() in the context instead sends no trace, only a string.
Text with example exception
Route::get('/error', function () { throw new Exception('This is a test exception'); return [ 'message' => 'This is a test response', ]; });
Open browser and navigate to http://localhost:8000/error
Testing
composer install
composer test
To test against a local checkout of nix-logger/nix-logger (sibling folder ../nix-logger-php), use a composer.local.json with a path repository (ignored by git):
COMPOSER=composer.local.json composer update vendor/bin/phpunit