Skip to content

Use PSR-3 LoggerInterface to allow custom logging implementations. #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 15, 2019

Conversation

noplanman
Copy link
Member

@noplanman noplanman commented Jun 8, 2019

As suggested in #831 this PR implements the PSR-3 LoggerInterface.
(@garak I'd appreciate your feedback as this is new-land for me)

It is still 100% backwards compatible (spewing deprecation notices) but the deprecated code will be removed in the near future.
For dev purposes, I'll move the Monolog requirement to require-dev when the deprecated code gets removed and the new logging methodology is enforced.

New way of initialising the logger (using Monolog in this example):
First we need to get Monolog via Composer:

composer require monolog/monolog

Then we initialise the logger objects in our webhook or getUpdates script:

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Longman\TelegramBot\TelegramLog;

TelegramLog::initialize(
    // Main logger that handles all 'error' and 'debug' logs.
    new Logger('telegram_bot', [
        (new StreamHandler(__DIR__ . "/logs/{$bot_username}_debug.log", Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)),
        (new StreamHandler(__DIR__ . "/logs/{$bot_username}_error.log", Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)),
    ]),
    // Updates logger for raw updates.
    new Logger('telegram_bot_updates', [
        (new StreamHandler(__DIR__ . "/logs/{$bot_username}_update.log", Logger::INFO))->setFormatter(new LineFormatter('%message%' . PHP_EOL)),
    ])
);

noplanman added 2 commits June 8, 2019 03:04
Note that all deprecated code will be removed in the near future!
Copy link
Member

@akalongman akalongman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I likw this PR 😍 Use PSR's is the right way

@noplanman noplanman changed the title WIP: Use PSR-3 LoggerInterface to allow custom logging implementations. Use PSR-3 LoggerInterface to allow custom logging implementations. Jun 15, 2019
@noplanman noplanman merged commit 0e5821b into php-telegram-bot:develop Jun 15, 2019
@noplanman noplanman deleted the psr3-logger branch June 15, 2019 14:11
@noplanman noplanman mentioned this pull request Jun 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants