Commit bf48e732 authored by MBoretto's avatar MBoretto

monolog works

parent 2ccc2af1
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace Longman\TelegramBot; namespace Longman\TelegramBot;
use Longman\TelegramBot\TelegramBot;
use Longman\TelegramBot\Entities\File; use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Entities\ServerResponse; use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
...@@ -90,6 +91,7 @@ class Request ...@@ -90,6 +91,7 @@ class Request
{ {
if (is_string($input) | $input == false) { if (is_string($input) | $input == false) {
self::$input = $input; self::$input = $input;
TelegramLog::update(self::$input);
} else { } else {
throw new TelegramException('Input must be a string!'); throw new TelegramException('Input must be a string!');
} }
...@@ -107,33 +109,9 @@ class Request ...@@ -107,33 +109,9 @@ class Request
} else { } else {
self::setInputRaw(file_get_contents('php://input')); self::setInputRaw(file_get_contents('php://input'));
} }
self::log(self::$input);
return self::$input; return self::$input;
} }
/**
* Write log entry
*
* @todo Take log verbosity into account
*
* @param string $string
*
* @return mixed
*/
private static function log($string)
{
if (!self::$telegram->getLogRequests()) {
return false;
}
$path = self::$telegram->getLogPath();
if (!$path) {
return false;
}
return file_put_contents($path, $string . "\n", FILE_APPEND);
}
/** /**
* Generate general fake server response * Generate general fake server response
* *
...@@ -198,28 +176,27 @@ class Request ...@@ -198,28 +176,27 @@ class Request
$curlConfig[CURLOPT_POSTFIELDS] = $data; $curlConfig[CURLOPT_POSTFIELDS] = $data;
} }
if (self::$telegram->getLogVerbosity() >= 3) { if (TelegramLog::isDebugLogActive()) {
$curlConfig[CURLOPT_VERBOSE] = true; $curlConfig[CURLOPT_VERBOSE] = true;
$verbose = fopen('php://temp', 'w+'); $verbose_curl_output = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose); curl_setopt($ch, CURLOPT_STDERR, $verbose_curl_output);
} }
curl_setopt_array($ch, $curlConfig); curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch); $result = curl_exec($ch);
//Logging curl requests //Logging curl requests
if (self::$telegram->getLogVerbosity() >= 3) { if (TelegramLog::isDebugLogActive()) {
rewind($verbose); rewind($verbose_curl_output);
$verboseLog = stream_get_contents($verbose); $verboseLog = stream_get_contents($verbose_curl_output);
self::log('Verbose curl output:' . "\n" . htmlspecialchars($verboseLog) . "\n"); fclose($verbose_curl_output);
TelegramLog::debug('Verbose curl output:' . "\n" . htmlspecialchars($verboseLog) . "\n");
} }
//Logging getUpdates Update //Logging getUpdates Update
//Logging curl updates if ($action == 'getUpdates') {
if ($action == 'getUpdates' & self::$telegram->getLogVerbosity() >= 1 | self::$telegram->getLogVerbosity() >= 3 //Will be Logged in Update steam
) {
self::setInputRaw($result); self::setInputRaw($result);
self::log($result);
} }
if ($result === false) { if ($result === false) {
......
...@@ -262,78 +262,57 @@ class Telegram ...@@ -262,78 +262,57 @@ class Telegram
} }
/** /**
* Set log requests * Redirect log stream to an existing monolog entity
* *
* 0 don't store * @param \Monolog\Logger $monolog
* 1 store the Curl verbose output with Telegram updates * @param string $table_prefix
*
* @param bool $log_requests
*
* @return Telegram
*/
public function setLogRequests($log_requests)
{
$this->log_requests = $log_requests;
return $this;
}
/**
* Get log requests
*
* @return bool
*/ */
public function getLogRequests() public function enableExternalLog(\Monolog\Logger $monolog = null)
{ {
return $this->log_requests; TelegramLog::initialize($monolog);
} }
/** /**
* Set log path * Set error log
* *
* @param string $log_path * @param string $path
* *
* @return \Longman\TelegramBot\Telegram * @return Telegram
*/ */
public function setLogPath($log_path) public function setErrorLog($path)
{ {
$this->log_path = $log_path; TelegramLog::initErrorLog($path);
return $this; return $this;
} }
/** /**
* Get log path * Set requests log
* *
* @return string * For debug purpore logs all requests done from Request.php
*/
public function getLogPath()
{
return $this->log_path;
}
/**
* Set log Verbosity
*
* @param int $log_verbosity
* *
* 1 only incoming updates from webhook and getUpdates * @param string $path
* 3 incoming updates from webhook and getUpdates and curl request info and response
* *
* @return \Longman\TelegramBot\Telegram * @return Telegram
*/ */
public function setLogVerbosity($log_verbosity) public function setDebugLog($path)
{ {
$this->log_verbosity = $log_verbosity; TelegramLog::initDebugLog($path);
return $this; return $this;
} }
/** /**
* Get log verbosity * Set Update log
*
* Log row request coming from Telegram
* *
* @return int * @param string $path
*
* @return Telegram
*/ */
public function getLogVerbosity() public function setUpdateLog($path)
{ {
return $this->log_verbosity; TelegramLog::initUpdateLog($path);
return $this;
} }
/** /**
......
...@@ -32,6 +32,27 @@ class TelegramLog ...@@ -32,6 +32,27 @@ class TelegramLog
*/ */
static protected $monolog_update = null; static protected $monolog_update = null;
/**
* Path for error log
*
* @var string
*/
static protected $error_log_path = null;
/**
* Path for debug log
*
* @var string
*/
static protected $debug_log_path = null;
/**
* Path for update log
*
* @var string
*/
static protected $update_log_path = null;
/** /**
* Initialize * Initialize
* *
...@@ -64,7 +85,8 @@ class TelegramLog ...@@ -64,7 +85,8 @@ class TelegramLog
public static function initErrorLog($path) public static function initErrorLog($path)
{ {
self::initialize(); self::initialize();
return self::$monolog->pushHandler(new \Monolog\Handler\StreamHandler($path, \Monolog\Logger::ERROR)); self::$error_log_path = $path;
return self::$monolog->pushHandler(new \Monolog\Handler\StreamHandler(self::$error_log_path, \Monolog\Logger::ERROR));
} }
/** /**
...@@ -77,7 +99,8 @@ class TelegramLog ...@@ -77,7 +99,8 @@ class TelegramLog
public static function initDebugLog($path) public static function initDebugLog($path)
{ {
self::initialize(); self::initialize();
return self::$monolog->pushHandler(new \Monolog\Handler\StreamHandler($path, \Monolog\Logger::DEBUG)); self::$debug_log_path = $path;
return self::$monolog->pushHandler(new \Monolog\Handler\StreamHandler(self::$debug_log_path, \Monolog\Logger::DEBUG));
} }
/** /**
...@@ -88,14 +111,17 @@ class TelegramLog ...@@ -88,14 +111,17 @@ class TelegramLog
* *
* @return \Monolog\Logger * @return \Monolog\Logger
*/ */
public static function initUpdateLog() public static function initUpdateLog($path)
{ {
self::$update_log_path = $path;
if (self::$monolog_update === null) { if (self::$monolog_update === null) {
self::$monolog_update = new \Monolog\Logger('bot_update_log'); self::$monolog_update = new \Monolog\Logger('bot_update_log');
// Create a formatter // Create a formatter
$formatter = new \Monolog\Formatter\LineFormatter('%message%'); $output = "%message%\n";
$formatter = new \Monolog\Formatter\LineFormatter($output);
// Update handler // Update handler
$update_handler = new \Monolog\Handler\StreamHandler($path, \Monolog\Logger::INFO); $update_handler = new \Monolog\Handler\StreamHandler(self::$update_log_path, \Monolog\Logger::INFO);
$update_handler->setFormatter($formatter); $update_handler->setFormatter($formatter);
self::$monolog_update->pushHandler($update_handler); self::$monolog_update->pushHandler($update_handler);
...@@ -103,6 +129,45 @@ class TelegramLog ...@@ -103,6 +129,45 @@ class TelegramLog
return self::$monolog; return self::$monolog;
} }
/**
* Is error log active
*
* @return bool
*/
public static function isErrorLogActive()
{
if (self::$error_log_path === null) {
return 0;
}
return 1;
}
/**
* Is debug log active
*
* @return bool
*/
public static function isDebugLogActive()
{
if (self::$debug_log_path === null) {
return 0;
}
return 1;
}
/**
* Is update log active
*
* @return bool
*/
public static function isUpdateLogActive()
{
if (self::$update_log_path === null) {
return 0;
}
return 1;
}
/** /**
* Report error log * Report error log
* *
...@@ -110,7 +175,9 @@ class TelegramLog ...@@ -110,7 +175,9 @@ class TelegramLog
*/ */
public static function error($text) public static function error($text)
{ {
self::$monolog->error($text); if (self::isErrorLogActive()) {
self::$monolog->error($text);
}
} }
/** /**
...@@ -120,7 +187,9 @@ class TelegramLog ...@@ -120,7 +187,9 @@ class TelegramLog
*/ */
public static function debug($text) public static function debug($text)
{ {
self::$monolog->debug($text); if (self::isDebugLogActive()) {
self::$monolog->debug($text);
}
} }
/** /**
...@@ -130,6 +199,8 @@ class TelegramLog ...@@ -130,6 +199,8 @@ class TelegramLog
*/ */
public static function update($text) public static function update($text)
{ {
self::$monolog_update->info($text); if (self::isUpdateLogActive()) {
self::$monolog_update->info($text);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment