Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Simple Laravel tool to log messages and metrics to a database

License

Notifications You must be signed in to change notification settings

dotburo/laravel-log-metrics

Repository files navigation

Laravel log metrics

This project has been refactored and moved to dotburo/laravel-molog

Installation

You can install the package via composer:

composer require dotburo/laravel-log-metrics

You can publish and run the migrations with:

php artisan vendor:publish --provider="Dotburo\LogMetrics\LogMetricsServiceProvider" --tag="laravel-log-metrics-migrate"
php artisan migrate

Logging trait examples

use Dotburo\LogMetrics\Models\Metric;
use Dotburo\LogMetrics\Logging;
use Psr\Log\LogLevel;

class YourClass {
    use Logging;
    
    protected function yourMethod()
    {
        // Message examples
        
        $this->message('Import process initiated', LogLevel::INFO)
            ->notice('Import process ongoing')
            ->warn('Import process aborted')
            ->save();
        
       $this->message()
            ->setContextGlobally('Import process')
            ->notice('ongoing')
            ->warn('aborted')
            ->setLevel(LogLevel::CRITICAL)
            ->save();
        
        // Metric examples
        
        $this->metric('density', 5)->save();
        
        $this->metrics([
            ['key' => 'density', 'value' => 5.3567],
            ['key' => 'pressure', 'value' => 2.35, 'unit' => 'bar', 'type' => 'int'],
            new Metric(['key' => 'quality', 'value' => 3])
        ]);
        
        $this->metrics()
            ->setTenant(5)
            ->setRelation($this->message()->previous())
            ->save();
    }
}

Factory instantiation examples

use Dotburo\LogMetrics\Factories\MessageFactory;
use Dotburo\LogMetrics\Factories\MetricFactory;
use Psr\Log\LogLevel;

$messageFactory = new MessageFactory();
$messageFactory->setTenantGlobally(7);
$messageFactory->add('Import process completed', LogLevel::NOTICE);
$messageFactory->add('Bad air quality', LogLevel::WARNING);
$messageFactory->setTenant(5);
$messageFactory->setRelation($yourModel);
$messageFactory->setContext('Import process');
#$messageFactory->setLevel('key', LogLevel::ERROR);
#$messageFactory->setBody('key', 'Air quality import process completed');
$messageFactory->save();
$messageFactory->reset();

$metricFactory = new MetricFactory();
$metricFactory->setRelationGlobally($messageFactory->previous());
$metricFactory->add('pressure', 2.35, 'bar', 'int');
$metricFactory->add('density', 5.43);
$metricFactory->setContext('Import process');
$metricFactory->previous()->value = 5.45;
$metricFactory->save();
$metricFactory->reset();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

GNU General Public License (GPL). Please see the license file for more information.

About

Simple Laravel tool to log messages and metrics to a database

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages