Append query ID and IP address as a /* comment */ to every query, if enabled in debug settings

This commit is contained in:
Kijin Sung 2021-01-13 22:14:15 +09:00
parent fcc3db3a7a
commit 4805a1864b
7 changed files with 47 additions and 1 deletions

View file

@ -100,6 +100,7 @@ return array(
'display_type' => array('comment'),
'display_content' => array('request_info', 'entries', 'errors', 'queries'),
'display_to' => 'admin',
'query_comment' => false,
'write_error_log' => 'fatal',
'allow' => array(),
),

View file

@ -42,6 +42,7 @@ class DB
protected $_query_id = '';
protected $_errno = 0;
protected $_errstr = '';
protected $_debug_comment = false;
/**
* Transaction level.
@ -127,6 +128,9 @@ class DB
// Get the DB version.
$this->db_version = $this->_handle->getAttribute(\PDO::ATTR_SERVER_VERSION);
// Cache the debug comment setting.
$this->_debug_comment = config('debug.query_comment') ? true : false;
}
/**
@ -154,6 +158,12 @@ class DB
// Add table prefixes to the query string.
$statement = $this->addPrefixes($statement);
// Add the debug comment.
if ($this->_debug_comment)
{
$statement .= "\n" . sprintf('/* prepare() %s */', \RX_CLIENT_IP);
}
// Create and return a prepared statement.
$this->_last_stmt = $this->_handle->prepare($statement, $driver_options);
return $this->_last_stmt;
@ -185,6 +195,12 @@ class DB
// Add table prefixes to the query string.
$query_string = $this->addPrefixes($query_string);
// Add the debug comment.
if ($this->_debug_comment)
{
$query_string .= "\n" . sprintf('/* query() %s */', \RX_CLIENT_IP);
}
// Execute either a prepared statement or a regular query depending on whether there are arguments.
if (count($args))
{
@ -315,6 +331,11 @@ class DB
// Prepare and execute the main query.
try
{
if ($this->_debug_comment)
{
$query_string .= "\n" . sprintf('/* %s %s */', $query_id, \RX_CLIENT_IP);
}
$this->_query_id = $query_id;
if (count($query_params))
{
@ -395,6 +416,11 @@ class DB
// Prepare and execute the query.
try
{
if ($this->_debug_comment)
{
$query_string .= "\n" . sprintf('/* %s %s */', $query_id, \RX_CLIENT_IP);
}
if (count($query_params))
{
$this->_last_stmt = $this->_handle->prepare($query_string);
@ -457,6 +483,11 @@ class DB
*/
public function _query($query_string)
{
if ($this->_debug_comment)
{
$query_string .= "\n" . sprintf('/* _query() %s */', \RX_CLIENT_IP);
}
$this->_last_stmt = $this->_handle->query($query_string);
return $this->_last_stmt;
}
@ -1084,7 +1115,7 @@ class DB
// Compose the basic structure of the log entry.
$result = array(
'query' => $query,
'query' => preg_replace('!\n/\* .+ \*/$!s', '', $query),
'query_id' => $this->_query_id,
'connection' => $this->_type,
'elapsed_time' => sprintf('%0.5f', $elapsed_time),

View file

@ -880,6 +880,7 @@ class adminAdminController extends admin
Rhymix\Framework\Config::set('debug.log_slow_remote_requests', max(0, floatval($vars->debug_log_slow_remote_requests)));
Rhymix\Framework\Config::set('debug.display_type', $display_type);
Rhymix\Framework\Config::set('debug.display_to', strval($vars->debug_display_to) ?: 'admin');
Rhymix\Framework\Config::set('debug.query_comment', $vars->debug_query_comment === 'Y');
Rhymix\Framework\Config::set('debug.write_error_log', strval($vars->debug_write_error_log) ?: 'fatal');
// Debug content

View file

@ -626,6 +626,7 @@ class adminAdminView extends admin
Context::set('debug_display_type', (array)Rhymix\Framework\Config::get('debug.display_type'));
Context::set('debug_display_content', Rhymix\Framework\Config::get('debug.display_content'));
Context::set('debug_display_to', Rhymix\Framework\Config::get('debug.display_to'));
Context::set('debug_query_comment', Rhymix\Framework\Config::get('debug.query_comment'));
Context::set('debug_write_error_log', Rhymix\Framework\Config::get('debug.write_error_log'));
// IP access control

View file

@ -230,6 +230,7 @@ $lang->debug_display_to_admin = 'Administrator only';
$lang->debug_display_to_ip = 'Visitors from IP adresses listed below';
$lang->debug_display_to_everyone = 'Everyone';
$lang->debug_log_filename = 'Log filename';
$lang->debug_query_comment = 'Add Comment to Queries';
$lang->debug_write_error_log = 'Write to Error Log';
$lang->debug_write_error_log_all = 'All errors and warnings';
$lang->debug_write_error_log_fatal = 'Fatal errors only';
@ -237,6 +238,7 @@ $lang->debug_write_error_log_none = 'None';
$lang->about_debug_log_filename = 'YYYYMMDD in the filename will be replaced with the current date.<br>It is recommended to split the log file by date to prevent it from getting too large.';
$lang->about_debug_write_error_log = 'Select how much information will be recorded in the PHP error log.<br>Error logs are recorded even if debugging is disabled in Rhymix.';
$lang->about_debug_error_log_path = 'The PHP error log is currently located at: %s';
$lang->about_debug_query_comment = 'Add a comment containing the query name and IP address to every SQL statement. This may help you determine where slow queries are coming from.';
$lang->msg_debug_log_filename_not_writable = 'Rhymix cannot write log files in the specified path.';
$lang->debug_allowed_ip = 'Allowed IP addresses';
$lang->seo_main_title = 'Main Page Title';

View file

@ -226,6 +226,7 @@ $lang->debug_display_to_admin = '관리자에게만 표시';
$lang->debug_display_to_ip = '아래 IP의 방문자에게만 표시';
$lang->debug_display_to_everyone = '모두에게 표시';
$lang->debug_log_filename = '디버그 정보 기록 파일';
$lang->debug_query_comment = '쿼리에 주석 추가';
$lang->debug_write_error_log = '에러 로그에 기록';
$lang->debug_write_error_log_all = '모든 에러와 경고를 기록';
$lang->debug_write_error_log_fatal = '치명적인 에러만 기록';
@ -233,6 +234,7 @@ $lang->debug_write_error_log_none = '기록하지 않음';
$lang->about_debug_log_filename = '파일명에 YYYYMMDD가 포함된 경우 날짜별로 파일을 분리하여 기록합니다.<br>파일을 분리하지 않으면 용량이 매우 커질 수 있으니 주의하십시오.';
$lang->about_debug_write_error_log = 'PHP 에러 로그에 얼마나 많은 정보를 기록할지 선택할 수 있습니다.<br>오래된 서드파티 자료를 사용하시는 경우 경고(E_WARNING)가 많이 발생할 수 있으니 이 설정을 적절히 조정하십시오.<br>디버그 기능을 사용하지 않더라도 에러 로그는 항상 기록됩니다.';
$lang->about_debug_error_log_path = '현재 설정된 PHP 에러 로그 경로는 %s 입니다.';
$lang->about_debug_query_comment = '쿼리명과 IP 주소를 포함하는 주석을 모든 쿼리에 추가하여, 부하를 유발하는 쿼리 및 방문자를 DB에서 쉽게 파악할 수 있도록 합니다.';
$lang->msg_debug_log_filename_not_writable = '지정한 경로에 로그 파일을 작성할 수 없습니다.';
$lang->debug_allowed_ip = '디버그 허용 IP';
$lang->seo_main_title = '메인화면 제목';

View file

@ -87,6 +87,14 @@
<textarea name="debug_allowed_ip" id="debug_allowed_ip" rows="4" cols="42" placeholder="{$remote_addr} ({$lang->local_ip_address})" style="margin-right:10px">{$debug_allowed_ip}</textarea>
</div>
</div>
<div class="x_control-group">
<label class="x_control-label">{$lang->debug_query_comment}</label>
<div class="x_controls">
<label for="debug_query_comment_Y" class="x_inline"><input type="radio" name="debug_query_comment" id="debug_query_comment_Y" value="Y" checked="checked"|cond="$debug_query_comment" /> {$lang->cmd_yes}</label>
<label for="debug_query_comment_N" class="x_inline"><input type="radio" name="debug_query_comment" id="debug_query_comment_N" value="N" checked="checked"|cond="!$debug_query_comment" /> {$lang->cmd_no}</label>
<p class="x_help-block">{$lang->about_debug_query_comment}</p>
</div>
</div>
<div class="x_control-group">
<label class="x_control-label">{$lang->debug_write_error_log}</label>
<div class="x_controls">