Skip to content

dev-this/laravel-console-logg

Repository files navigation

Laravel ConsoleLogg

PHPUnit suite codecov Latest Stable Version Total Downloads License FOSSA Status

Effortless PSR-3 Logger output to your console applications

Powered by Symfony's Console Logger

Table of contents

What does it do?

ConsoleLogg provides output messages for your artisan commands, between your shared code which is using the built-in Laravel logger

Typically, this requires a hacky solution, such as coupling your shared services with a console logger, or configuring multiple driver channels.

With ConsoleLogg you can have logs for your artisan commands, and behave as usual with http/controllers.

Use your Laravel logger throughout your application shared services as usual

  • Dependency Injection/autowiring LoggerInterface $logger & $logger->debug("yeet")
  • logger()->critical("Send help")
  • Log::alert("She find pictures in my email")
  • Log::info("Found <X> to be processed")
php artisan my:command -vvv
[debug] yeet
[critical] Send help
[alert] She find pictures in my email
[info] Found <X> to be processed

Install

  1. Install the package via Composer:

    composer require devthis/console-logg
    
    # to optionally copy config
    php artisan vendor:publish --provider="DevThis\ConsoleLogg\Providers\ConsoleLoggServiceProvider"

Compatibility

Compatible Laravel PHP
✔️ 8.* PHP >=7.1
✔️ 7.* PHP >=7.1
✔️ 6.* PHP >=7.1
❗ 🔜 > 5.6 PHP >=7.1

PHP 8 compatibility is currently being tested

Compatibility assurance

Click to find out how we ensure compatibility

Compatibility is thoroughly tested using a combination of real world tests (functional), and unit tests

ConsoleLogg test suite is run in isolation

Unit tests ensure type-compatibility, expected behaviour is met & compatibility with each version of Laravel contracts

[TODO] Functional tests ensure real world expectations are through a real Laravel application


Features

Literally Effortless

Your application will not be coupled with ConsoleLogg.

There are no traits, classes, interfaces that you need to use. ConsoleLogg does not require any custom code, it just works.

The ConsoleLog Service Provider should be automatically added to your app, but if it hasn't, you can add it yourself to config/app.php

// generally only required when you have composer intalled with --no-scripts

'providers' => [
    //...
    \DevThis\ConsoleLogg\Providers\ConsoleLoggServiceProvider::class,
];

Settings

See config/console-logg.php for the raw configuration file

Filtering

default = false

If you choose to enable filtering, logs will only be output to artisan console commands if they have the context property logg set to true

eg.

logger()->info("Informative message #1", ['logg' => true]);
logger()->alert("Nice one");

With these logs being invoked by your artisan command, only [info] Informative message #1 will be output

Command-in-command

ConsoleLogg has (not yet) been tested for compatibility using artisan commands in a command with nested command calls

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyConsoleApp extends Command
{
    protected $description = '';
    protected $signature = 'my:app';

    public function handle(): int
    {
        //other:command may invoke servies that use the Laravel Logger
        //these logs will still output to this current console
        $this->call('other:command');
        //...
        
        return 0;
    }
}

Light footprint

  • Zero external dependencies outside of Laravel contracts
  • No memory leakage (needs validation/tests)
    • One time use console logger is attached & detached alongside command execution
    • All references destroyed after command termination (letting PHP Garbage Collection do its thing)
  • Service Provider lazily works only when running in console mode

Usage

Verbosity

Verbosity is optionally controlled by either using -v arguments when running artisan.

This is not behaviour set by ConsoleLogg, it is defined in combination of Laravel & Symfony

ConsoleLogg may provide configuration for this in the future, if demand is apparent

Verbosity Level
default emergency, alert, critical, error, warning
-v notice + all of above
-vv info + all of above
-vvv debug + all of above

Examples

Running artisan

View example usage

Example #1 - SQL query logging

There are several guides/answers on the internet that enable you to send all SQL queries to your configured Logger.

With ConsoleLogg installed this means

Links (in no order):

Example #2 - Raw code

⚠️ REMINDER: n-o-t-h-i-n-g is special about this following code example

There are no traits, interfaces, or classes/dependencies that are involved to use ConsoleLogg once it's installed.

Souce code for example service

Source of App\Service\MyExampleService
namespace App\Service;

use Illuminate\Support\Facades\Log;
use Psr\Log\LoggerInterface;

class MyExampleService {
    private $logger;
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
    
    public function doSomethingCool(): void
    {
        // using Laravel's logger with DI/autowiring
        $this->logger->debug("A message that should have value when output to your application in general");
        
        // Facade
        Log::info("or just the facade if you love magic");
        
        // Helper function
        logger()->notice("or this weird helper function I guess");
        
        // ... <imaginary useful code here>
    }
}

Source code for Console Application

Source of App\Console\Commands\ExampleConsole
namespace App\Console\Commands;

use App\Service\ExampleService;
use Illuminate\Console\Command;

class ExampleConsole extends Command
{
    /**
     * The console command description.
     */
    protected $description = '';

    /**
     * The name and signature of the console command.
     */
    protected $signature = 'something';

    public function handle(ExampleService $exampleService): int
    {
        $exampleService->doSomethingCool();

        return 0;
    }
}

Running artisan

Artisan command output
not-root@linux:~/MyProject$ php artisan something -vv
[debug] A message that should have value when output to your application in general
[info] or just the facade if you love magic
[notice] or this weird helper function I guess

License

Laravel ConsoleLogg is open-sourced software licensed under the MIT license.

About

Laravel Logging output for your artisan commands

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages