nagi/laravel-newrelic-log-api

This is my package laravel-newrelic-log-api

v1.0.5 2024-09-09 15:58 UTC

This package is auto-updated.

Last update: 2024-09-09 15:58:41 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Integrate New Relic Log API with Laravel your laravel application. It mainly utilizing queuse behind the scene to send the logs to New Relic API. so make sure you have queue worker running.

    // in loggin.php
    'stack' => [
                'driver' => 'stack',
                'channels' => ['single', 'newrelic-log-api'],
                'ignore_exceptions' => false,
            ],

        //...
        'newrelic-log-api' => [
            'driver' => 'monolog',
            'handler' => \Nagi\LaravelNewrelicLogApi\LaravelNewrelicLogApi::logHandler(),
            'level' => 'debug',
        ],
logger('newrelic-log-api')->info('Hey Mom!');

// or if you are using stack

logger()->info('Hey Mom!');

Support Me

Does your business depend on my contributions? Reach out and support me on PayPal. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

Installation

You can install the package via composer:

composer require nagi/laravel-newrelic-log-api

You can publish the config file with:

php artisan vendor:publish --tag="laravel-newrelic-log-api-config"

This is the contents of the published config file:

return [
    'enabled' => env('NEWRELIC_ENABLED', false),

    /**
     * New Relic API key or License key
     *
     * @see https://docs.newrelic.com/docs/logs/log-api/introduction-log-api/#setup
     */
    'api_key' => env('NEWRELIC_API_KEY'),

    /**
     * The base URL for the new relic log API
     *
     * @see https://docs.newrelic.com/docs/logs/log-api/introduction-log-api/#endpoint
     */
    'base_url' => env('NEWRELIC_BASE_URL', 'https://log-api.eu.newrelic.com'),

    /**
     * The minimum logging level at which this handler will be triggered
     */
    'level' => env('NEWRELIC_LEVEL', 'debug'),

    /**
     * Retry sending the log to New Relic API
     */
    'retry' => env('NEWRELIC_RETRY', 3),

    /**
     * Delay between retries in milliseconds
     */
    'retry_delay' => env('NEWRELIC_RETRY_DELAY', 1000),

    /**
     * Queue name to use for sending logs to New Relic API
     */
    'queue' => env('NEWRELIC_QUEUE', env('QUEUE_CONNECTION')),

    /**
     * Log handler to use for sending logs to New Relic API
     */
    'log_handler' => \Nagi\LaravelNewrelicLogApi\NewrelicLogHandler::class,
];

🚨 Make sure that you have queue worker configured.

Usage

Get your newrelic License/API

https://one.newrelic.com/api-keys

Add env values in your .env

NEWRELIC_ENABLED=true
NEWRELIC_API_KEY=<your_key>

# if your account is not EU change the baseurl
# https://log-api.newrelic.com/log/v1
NEWRELIC_BASE_URL=https://log-api.eu.newrelic.com

Add new relic channel to your config

In your logging.php add new-relic-log-api channel

        'newrelic-log-api' => [
            'driver' => 'monolog',
            'handler' => \Nagi\LaravelNewrelicLogApi\LaravelNewrelicLogApi::logHandler(),
            'level' => env('NEWRELIC_LEVEL', 'debug')
        ],

Add it to your logging stack

in logging.php

    'stack' => [
                'driver' => 'stack',
                'channels' => ['single', 'newrelic-log-api'],
                'ignore_exceptions' => false,
            ],

Listen to the event

If you are intrested in geting the response from the New Relic API you can listen to the event NewrelicLogApiResponseEvent It will have the status and the response from the API.

    protected $listen = [
        NewrelicLogApiResponseEvent::class => [
            NewrelicLogApiResponseListener::class,
        ],
    ];

Note on context log (attributes)

When sending one of the following attributes in the context it will be prefixed with attr_ to avoid overriding the whole message.

See: New Relic Log API

Extending

All of the classes in this package are loaded via Laravel's service container meaning you can easily replace them with your own implementation. On Your Own Risk.

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

The MIT License (MIT). Please see License File for more information.