stackify / stackify-log-monolog

Apache License 2.0
3 stars 6 forks source link
monolog php stackify

Stackify Monolog v3 Handler

Monolog handler for sending log messages and exceptions to Stackify. Monolog >= 3.0.0 is supported.

For Monolog V1

Use the 1.x branch

For Monolog V2

Use the 2.x branch

Installation

Install the latest version with composer require stackify/monolog "~2.0"

Installation with Linux Agent

This is the suggested installation option, offering the best logging performance.

PHP:

use Monolog\Logger;
use Stackify\Log\Monolog\Handler as StackifyHandler;

$handler = new StackifyHandler('application_name', 'environment_name');
$logger = new Logger('logger');
$logger->pushHandler($handler);

Symfony:

services:
    stackify_handler:
        class: "Stackify\\Log\\Monolog\\Handler"
        arguments: ["application_name", "environment_name"]
monolog:
    handlers:
        stackify:
            type: service
            id: stackify_handler

Optional Settings

Log Server Environment Variables

Installation without Linux Agent

This option does not require a Stackify Agent to be installed because it sends data directly to Stackify services. It collects log entries in batches, calls curl using the exec function, and sends data to the background immediately [exec('curl ... &')]. This will affect the performance of your application minimally, but it requires permissions to call exec inside the PHP script and it may cause silent data loss in the event of any network issues. This transport method does not work on Windows. To configure ExecTransport you need to pass the environment name and API key (license key):

PHP:

use Stackify\Log\Transport\ExecTransport;
use Stackify\Log\Monolog\Handler as StackifyHandler;

$transport = new ExecTransport('api_key');
$handler = new StackifyHandler('application_name', 'environment_name', $transport);
$logger = new Logger('logger');
$logger->pushHandler($handler);

Symfony:

services:
    stackify_transport:
        class: "Stackify\\Log\\Transport\ExecTransport"
        arguments: ["api_key"]
    stackify_handler:
        class: "Stackify\\Log\\Monolog\\Handler"
        arguments: ["application_name", "environment_name", "@stackify_transport"]
monolog:
    handlers:
        stackify:
            type: service
            id: stackify_handler

Optional Configuration

Proxy

Curl path

Log Server Environment Variables

Notes

To get more error details pass Exception objects to the logger if available:

try {
    $db->connect();
catch (DbException $ex) {
    // you may use any key name
    $logger->addError('DB is not available', ['ex' => $ex]);
}

Additional Configuration

For additional configurations, you can set on the XML or the PHP File Configuration. Reference for the additional options are located on the stackify logger repository Stackify PHP Logger - Configuration Settings

Transport Level

$config = array( 'CaptureServerVariables' => false, 'CaptureServerVariablesWhitelist' => '*', 'CaptureServerVariablesBlacklist' => 'REMOTE_ADDR,SERVER_ADDR', ... );

$transport = new ExecTransport($apiKey, [ 'config' => $config ]);

$handler = new StackifyHandler('application_name', 'environment_name', $transport); $logger = new Logger('logger'); $logger->pushHandler($handler);

### Handler Level
- This applies to the current Monolog Handler

 ```php
use Monolog\Logger;
use Stackify\Log\Monolog\Handler as StackifyHandler;

$transport = new ExecTransport($apiKey); // Your selected transport (Can be null which defaults to AgentSocketTransport)
$logServerVariables = false; // Default
$config = array(
        'CaptureServerVariables' => false,
        'CaptureServerVariablesWhitelist' => '*',
        'CaptureServerVariablesBlacklist' => 'REMOTE_ADDR,SERVER_ADDR',
        ...
);

$handler = new StackifyHandler('application_name', 'environment_name', $transport, $logServerVariables, $config);
$logger = new Logger('logger');
$logger->pushHandler($handler);

Handler Level Option

$transport = new ExecTransport($apiKey); // Your selected transport (Can be null which defaults to AgentSocketTransport) $logServerVariables = false; // Default $config = array( 'IncludeChannel' => true, ... );

$handler = new StackifyHandler('application_name', 'environment_name', $transport, $logServerVariables, $config); $logger = new Logger('logger'); $logger->pushHandler($handler);


- **Include Extra In Context**
    - This will include the extra property to the context (merging extra to context)

 ```php
use Monolog\Logger;
use Stackify\Log\Monolog\Handler as StackifyHandler;

$transport = new ExecTransport($apiKey); // Your selected transport (Can be null which defaults to AgentSocketTransport)
$logServerVariables = false; // Default
$config = array(
        'IncludeExtraInContext' => true,
        ...
);

$handler = new StackifyHandler('application_name', 'environment_name', $transport, $logServerVariables, $config);
$logger = new Logger('logger');
$logger->pushProcessor(function ($record) {
      if (empty($record['extra'])) {
        $record['extra'] = [];
      }
      $record['extra']['dummy'] = 1;
      return $record;
});
$logger->pushHandler($handler);

Symfony

services:
    stackify_transport:
        class: "Stackify\\Log\\Transport\\CurlTransport"
        arguments: ["api_key"]
# Square/Curly Brackets
    stackify_handler:
        class: "Stackify\\Log\\Monolog\\Handler"
        arguments: ["application_name", "environment_name", "@stackify_transport", false, { CaptureServerVariables: false, ... }]
# or
# Dash/Colon Space
    stackify_handler:
        class: "Stackify\\Log\\Monolog\\Handler"
        arguments:
            - "application_name"
            - "environment_name"
            - "@stackify_transport"
            - false
            - CaptureServerVariables: false
              CaptureServerWhitelist: "*"
              CaptureServerBlacklist: null

monolog:
    handlers:
        stackify:
            type: service
            id: stackify_handler

Troubleshooting

If transport does not work, try looking into vendor\stackify\logger\src\Stackify\debug\log.log file (if it is available for writing). Errors are also written to global PHP error_log. Note that ExecTransport does not produce any errors at all, but you can switch it to debug mode:

$transport = new ExecTransport($apiKey, ['debug' => true]);

You can set it also on the Logger level. Setting the Debug and DebugLogPath

$config = array(
        'DebugLogPath' => '/path/to/log.log',
        'Debug' => true
    );

$logger = new StackifyHandler('application_name', 'environment_name', $transport, $logServerVariables, $config);

License

Copyright 2019 Stackify, LLC.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.