upaid/elk

ELK Logger for Laravel
13,889 14
Install
composer require upaid/elk
Latest Version:1.0.2
License:MIT
Last Updated:Nov 19, 2019
Links: GitHub  ·  Packagist
Maintainer: uPaid

ELK

Laravel logger for formatting logs for ELK. Adds support for trace / span id according to the format used by Spring Cloud Sleuth. Adds also a few additional fields.

Installation

Via Composer
$ composer require upaid/elk
Publish the config

Run the following command to publish the package config file:

php artisan vendor:publish --provider="Upaid\Elk\Providers\ElkServiceProvider"
Add keys to .env
ELK_SERVICE_NAME='app name'
ELK_BANKNAME='bank name'
ELK_CHANNEL='channel for recognizing applications by logstash'
Or override config file
<?php

return [
    'logstash' => [
        'serviceName' => 'app name',
        'bankName' => 'bank name',
        'channel' => 'channel for recognizing applications by logstash',
        'showLogType' => true,
    ]
];
Changing default log fields and they order

To change default log fields or they order override fields property in config file

<?php

return [
    'logstash' => [
        'serviceName' => 'app name',
        'bankName' => 'bank name',
        'channel' => 'channel for recognizing applications by logstash',
        'fields' => [
            'timestamp',
            'severity' => 'level_name',
            'msg' => 'message',
            'span',
            'class' => 'class',
            'exception' => 'exception',
            'trace',
            'parent',
            'bank.name',
            'environment',
            'service',
            'channel',
            'context' => [
                'user.phone',
                'user.email',
                'card.id',
            ],
            'extra',
            'log_type',
        ],
        'showLogType' => true,
    ]
];
Hiding sensitive data

In order to remove sensitive data from the log file content you can add mask.fields property to the config file and specify the values of which fields should be hidden. Fields values will be replaced with mask.replacement property. It's also possible to hide values according to the patterns specified in mask.patterns. The example represents replacement of card number.

<?php

return [
    'mask' => [
        'replacement' => '[MASKED]',
        'fields' => [
            'password',
            'new_password',
            'password_confirmation',
            'card_no',
            'cvc',
        ],
        'patterns' => [
            '/^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/',
        ],
    ]
];
Add logging channel

Add elk channel to the channels array in the config/logging.php config file as follows:

'channels' => [
    'elk' => [
        'driver' => 'custom',
        'via' => \Upaid\Elk\Services\Logging\CreateCustomLogger::class,
        'log_name' => 'global'
    ],
    
    ...

In the same file set default log channel to elk:

return [
    'default' => 'elk',
    
    ...

or when you want to use stack channel (default channel is 'stack') add elk to stack channel:

'channels' => [

    ...
    
    'stack' => [
        'driver' => 'stack',
        'channels' => ['elk'],
    ],

    ...

Usage

Standard logging in Laravel (to global.json)

Log::debug('An informational message.');

Logging to custom log file name (in this case to testlog.json):

use \Upaid\Elk\Services\Logging\Logger;

$logger = new Logger('testlog');
$logger->info('Hello World!');

Change log

Please see the changelog for more information on what has changed recently.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

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

Version History

Version Released Laravel License
1.0.2 5.8.* MIT
1.0.1 ~5 MIT
1.0.0 ~5 MIT
0.0.3 ~5 MIT
0.0.2 ~5 MIT