PHP code example of crazyfactory / phalcon-loggers

1. Go to this page and download the library: Download crazyfactory/phalcon-loggers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

crazyfactory / phalcon-loggers example snippets


$loader = new Phalcon\Loader;
$loader->registerNamespaces([
    'CrazyFactory\\PhalconLogger' => '/path/to/vendor/crazyfactory/phalcon-loggers/src/',
])->register();



Raven_Autoloader::register();

$id = (new Phalcon\Security\Random)->uuid();
$di = new Phalcon\Di\FactoryDefault;
(new CrazyFactory\PhalconLogger\Service)->register(null, $di);

// If you already have `requestId` in config array/object you don't need to set it again.
$di->getShared('logger')->setRequestId($id);

$di->getShared('logger')->info('some text');
// Supports interpolation for keys wrapped in curly brace.
$di->getShared('logger')->critical('some text {key}', ['key' => 'val']);

//
// Below examples assume that info level is allowed in config->slack->levels array.
//
// Mention an user in slack:
$context = ['mentions' => 'slackbot', 'a' => 10];
$di->getShared('slack')->info('some text {a}', $context);

// Customize channel, username, icon_emoji, icon_url via context:
$context += [
    'username' => 'bot',
    'channel' => '#general',
    'icon_emoji' => ':monkey_face:',
];
$di->getShared('slack')->info('some other text {a}', $context);

// Attachment:
$context += [
    'attachment' => [
        'title' => 'Attachment title',
        'text' => 'Attachment text',
        'color' => 'good',
    ],
];
$di->getShared('slack')->info('yet other text {a} with attachment', $context);

use Phalcon\Logger;

$config = [
    // The application name used in logs. Helps to distinguish &/or search.
    'appName' => '',
    // Current application environment.
    'environment' => 'prod',
    'requestID'   => null,
    'sentry'      => [
        // The login information for Sentry. If one of the values is empty the logging is suppressed silently.
        'credential' => [
            'key'       => '',
            'secret'    => '',
            'projectId' => '',
        ],
        // The options for Raven_Client. See https://docs.sentry.io/clients/php/config/#available-settings
        'options' => [
            'curl_method' => 'sync',
            'prefixes'    => [],
            'app_path'    => '',
            'timeout'     => 2,
        ],
        // Sentry will log errors/exceptions when the application environment set above is one of these.
        'environments' => ['prod', 'staging'],
        // The log levels which are forwarded to sentry.
        'levels' => [Logger::EMERGENCY, Logger::CRITICAL, Logger::ERROR],
        // These exceptions are not reported to sentry.
        'dontReport' => [],
    ],
    'slack' => [
        // If webhook url is missing the logging is suppressed silently.
        'webhookUrl'   => '',
        // Slack will log messages when the application environment set above is one of these.
        'environments' => ['prod', 'dev'],
        // Curl method can be 'sync' or 'exec' (sync uses php curl_*, exec runs in background).
        'curlMethod'   => 'sync',
        // HTTP headers to be appended to request.
        'headers'      => [],
        // The log levels which are forwarded to slack.
        'levels'       => [Logger::SPECIAL, Logger::CUSTOM],
    ],
];

$di = new Phalcon\Di\FactoryDefault;

// Register the loggers with the config:
$di->setShared('sentry', function () use ($config) {
    return new CrazyFactory\PhalconLogger\Adapter\Sentry($config);
});
$di->setShared('slack', function () use ($config) {
    return new CrazyFactory\PhalconLogger\Adapter\Slack($config);
});

// OR you could just use supplied `Service`:
(new CrazyFactory\PhalconLogger\Service)->register($config, $di);


    $di->getShared('sentry')->addCrumb('User has just logged in');

    $di->getShared('sentry')->setTag('name', 'value')->setTag('another', 'another-value');

    // you can also use current user from DI if you have one
    $di->getShared('sentry')->setUserContext(['id' => 1, 'username' => 'someone', 'email' => '[email protected]']);

    $di->getShared('sentry')->setExtraContext(['arbitrary_key' => 'arbitrary_value', 'arbitrary_key_2', 'arbitrary_value_2']);

    $di->getShared('sentry')->getLastEventId();

    try {
        $app->handle();
    } catch (\Throwable $e) {
        $di->getShared('sentry')->logException($e);
        // However it is advisable to just use logException of the logger collection
        // so that all the registered loggers are notified of the exception to do the needful.
        $di->getShared('logger')->logException($e);
    }