Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
bf48e732
Commit
bf48e732
authored
May 21, 2016
by
MBoretto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
monolog works
parent
2ccc2af1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
116 additions
and
89 deletions
+116
-89
Request.php
src/Request.php
+12
-35
Telegram.php
src/Telegram.php
+25
-46
TelegramLog.php
src/TelegramLog.php
+79
-8
No files found.
src/Request.php
View file @
bf48e732
...
...
@@ -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
)
{
...
...
src/Telegram.php
View file @
bf48e732
...
...
@@ -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
set
LogPath
(
$log_
path
)
public
function
set
ErrorLog
(
$
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
set
LogVerbosity
(
$log_verbosity
)
public
function
set
DebugLog
(
$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
;
}
/**
...
...
src/TelegramLog.php
View file @
bf48e732
...
...
@@ -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
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment