Commit bf48e732 authored by MBoretto's avatar MBoretto

monolog works

parent 2ccc2af1
......@@ -10,6 +10,7 @@
namespace Longman\TelegramBot;
use Longman\TelegramBot\TelegramBot;
use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
......@@ -90,6 +91,7 @@ class Request
{
if (is_string($input) | $input == false) {
self::$input = $input;
TelegramLog::update(self::$input);
} else {
throw new TelegramException('Input must be a string!');
}
......@@ -107,33 +109,9 @@ class Request
} else {
self::setInputRaw(file_get_contents('php://input'));
}
self::log(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
*
......@@ -198,28 +176,27 @@ class Request
$curlConfig[CURLOPT_POSTFIELDS] = $data;
}
if (self::$telegram->getLogVerbosity() >= 3) {
if (TelegramLog::isDebugLogActive()) {
$curlConfig[CURLOPT_VERBOSE] = true;
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
$verbose_curl_output = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose_curl_output);
}
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
//Logging curl requests
if (self::$telegram->getLogVerbosity() >= 3) {
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
self::log('Verbose curl output:' . "\n" . htmlspecialchars($verboseLog) . "\n");
if (TelegramLog::isDebugLogActive()) {
rewind($verbose_curl_output);
$verboseLog = stream_get_contents($verbose_curl_output);
fclose($verbose_curl_output);
TelegramLog::debug('Verbose curl output:' . "\n" . htmlspecialchars($verboseLog) . "\n");
}
//Logging getUpdates Update
//Logging curl updates
if ($action == 'getUpdates' & self::$telegram->getLogVerbosity() >= 1 | self::$telegram->getLogVerbosity() >= 3
) {
if ($action == 'getUpdates') {
//Will be Logged in Update steam
self::setInputRaw($result);
self::log($result);
}
if ($result === false) {
......
......@@ -262,78 +262,57 @@ class Telegram
}
/**
* Set log requests
* Redirect log stream to an existing monolog entity
*
* 0 don't store
* 1 store the Curl verbose output with Telegram updates
*
* @param bool $log_requests
*
* @return Telegram
*/
public function setLogRequests($log_requests)
{
$this->log_requests = $log_requests;
return $this;
}
/**
* Get log requests
*
* @return bool
* @param \Monolog\Logger $monolog
* @param string $table_prefix
*/
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;
}
/**
* Get log path
* Set requests log
*
* @return string
*/
public function getLogPath()
{
return $this->log_path;
}
/**
* Set log Verbosity
*
* @param int $log_verbosity
* For debug purpore logs all requests done from Request.php
*
* 1 only incoming updates from webhook and getUpdates
* 3 incoming updates from webhook and getUpdates and curl request info and response
* @param string $path
*
* @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;
}
/**
* 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
*/
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
*
......@@ -64,7 +85,8 @@ class TelegramLog
public static function initErrorLog($path)
{
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
public static function initDebugLog($path)
{
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
*
* @return \Monolog\Logger
*/
public static function initUpdateLog()
public static function initUpdateLog($path)
{
self::$update_log_path = $path;
if (self::$monolog_update === null) {
self::$monolog_update = new \Monolog\Logger('bot_update_log');
// Create a formatter
$formatter = new \Monolog\Formatter\LineFormatter('%message%');
$output = "%message%\n";
$formatter = new \Monolog\Formatter\LineFormatter($output);
// 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);
self::$monolog_update->pushHandler($update_handler);
......@@ -103,6 +129,45 @@ class TelegramLog
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
*
......@@ -110,7 +175,9 @@ class TelegramLog
*/
public static function error($text)
{
self::$monolog->error($text);
if (self::isErrorLogActive()) {
self::$monolog->error($text);
}
}
/**
......@@ -120,7 +187,9 @@ class TelegramLog
*/
public static function debug($text)
{
self::$monolog->debug($text);
if (self::isDebugLogActive()) {
self::$monolog->debug($text);
}
}
/**
......@@ -130,6 +199,8 @@ class TelegramLog
*/
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