Skip to content

Improve Log #412

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 72 additions & 56 deletions src/TelegramLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Longman\TelegramBot\Exception\TelegramLogException;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

Expand All @@ -20,14 +21,14 @@ class TelegramLog
/**
* Monolog instance
*
* @var \Monolog\Logger
* @var Logger
*/
static protected $monolog;

/**
* Monolog instance for update
*
* @var \Monolog\Logger
* @var Logger
*/
static protected $monolog_update;

Expand Down Expand Up @@ -65,24 +66,15 @@ class TelegramLog
* Initilize monolog instance. Singleton
* Is possbile provide an external monolog instance
*
* @param \Monolog\Logger
* @param Logger $external_monolog
*
* @return \Monolog\Logger
* @return Logger
*/
public static function initialize(Logger $external_monolog = null)
{
if (self::$monolog === null) {
if ($external_monolog !== null) {
self::$monolog = $external_monolog;

foreach (self::$monolog->getHandlers() as $handler) {
if ($handler->getLevel() === 400) {
self::$error_log_path = 'true';
}
if ($handler->getLevel() === 100) {
self::$debug_log_path = 'true';
}
}
} else {
self::$monolog = new Logger('bot_log');
}
Expand All @@ -91,52 +83,81 @@ public static function initialize(Logger $external_monolog = null)
return self::$monolog;
}

/**
* Initialize update
*
* Initilize monolog update instance. Singleton
* Is possbile provide an external monolog instance
*
* @param Logger $external_monolog
*
* @return Logger
*/
public static function initializeUpdate(Logger $external_monolog = null)
{
if (self::$monolog_update === null) {
if ($external_monolog !== null) {
self::$monolog_update = $external_monolog;
} else {
self::$monolog_update = new Logger('bot_update_log');
}
}

return self::$monolog_update;
}

/**
* Initialize error log
*
* @param string $path
* @param string $path
* @param HandlerInterface $external_handler
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
* @return Logger
* @throws TelegramLogException
*/
public static function initErrorLog($path)
public static function initErrorLog($path, HandlerInterface $external_handler = null)
{
if ($path === null || $path === '') {
if (($path === null || $path === '') && is_null($external_handler)) {
throw new TelegramLogException('Empty path for error log');
}
self::initialize();
self::$error_log_path = $path;

return self::$monolog->pushHandler(
(new StreamHandler(self::$error_log_path, Logger::ERROR))
->setFormatter(new LineFormatter(null, null, true))
);
if (is_null($external_handler)) {
self::$error_log_path = $path;
$handler = new StreamHandler(self::$error_log_path, Logger::ERROR);
} else {
self::$error_log_path = 'true';
$handler = $external_handler;
}

return self::$monolog->pushHandler($handler->setFormatter(new LineFormatter(null, null, true)));
}

/**
* Initialize debug log
*
* @param string $path
* @param string $path
* @param HandlerInterface $external_handler
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
* @return Logger
* @throws TelegramLogException
*/
public static function initDebugLog($path)
public static function initDebugLog($path, HandlerInterface $external_handler = null)
{
if ($path === null || $path === '') {
if (($path === null || $path === '') && is_null($external_handler)) {
throw new TelegramLogException('Empty path for debug log');
}
self::initialize();
self::$debug_log_path = $path;

return self::$monolog->pushHandler(
(new StreamHandler(self::$debug_log_path, Logger::DEBUG))
->setFormatter(new LineFormatter(null, null, true))
);
if (is_null($external_handler)) {
self::$debug_log_path = $path;
$handler = new StreamHandler(self::$debug_log_path, Logger::DEBUG);
} else {
self::$debug_log_path = 'true';
$handler = $external_handler;
}

return self::$monolog->pushHandler($handler->setFormatter(new LineFormatter(null, null, true)));
}

/**
Expand Down Expand Up @@ -177,33 +198,28 @@ public static function endDebugLogTempStream($message = '%s')
* Initilize monolog instance. Singleton
* Is possbile provide an external monolog instance
*
* @param string $path
* @param string $path
* @param HandlerInterface $external_handler
*
* @return \Monolog\Logger
* @throws \Longman\TelegramBot\Exception\TelegramLogException
* @throws \InvalidArgumentException
* @throws \Exception
* @return Logger
* @throws TelegramLogException
*/
public static function initUpdateLog($path)
public static function initUpdateLog($path, HandlerInterface $external_handler = null)
{
if ($path === null || $path === '') {
if (($path === null || $path === '') && is_null($external_handler)) {
throw new TelegramLogException('Empty path for update log');
}
self::$update_log_path = $path;
if (self::$monolog_update === null) {
self::$monolog_update = new Logger('bot_update_log');
// Create a formatter
$output = '%message%' . PHP_EOL;
$formatter = new LineFormatter($output);
self::initializeUpdate();

// Update handler
$update_handler = new StreamHandler(self::$update_log_path, Logger::INFO);
$update_handler->setFormatter($formatter);

self::$monolog_update->pushHandler($update_handler);
if (is_null($external_handler)) {
self::$update_log_path = $path;
$handler = new StreamHandler(self::$update_log_path, Logger::INFO);
} else {
self::$update_log_path = 'true';
$handler = $external_handler;
}

return self::$monolog;
return self::$monolog_update->pushHandler($handler->setFormatter(new LineFormatter('%message%' . PHP_EOL)));
}

/**
Expand Down