我是 log4php 的新手,我正在尝试在我的自定义框架上实现它。我已经设法将所有数据库查询归档。我现在要做的是以某种方式记录它,以便我可以识别每个客户端,以便我可以查看调试我的浏览器,而不会看到同一文件中的所有其他客户端。
xml
<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="default" class="LoggerAppenderConsole" />
<appender name="default" class="LoggerAppenderFile">
<layout class="LoggerLayoutSimple" />
<param name="file" value="/var/SP/oiadm/docroot/cache/my.log" />
<param name="append" value="true" />
</appender>
<root>
<level value="debug" />
<appender_ref ref="default" />
</root>
</configuration>
php 数据库类
if (defined ( 'OI_ENV' ) && OI_ENV == 'DEVELOPMENT') {
$debugtoolbar = array (
"stacktrace" => $stacktrace,
"sql" => $debug_sql,
"start" => $debug_sql_start,
"end" => $debug_sql_end
);
/*
if ( isset($_SESSION ['OI_DEBUG'] ['debugtoolbar'] ['database']) && count ( $_SESSION ['OI_DEBUG'] ['debugtoolbar'] ['database'] ) > 9) {
$_SESSION ['OI_DEBUG'] ['debugtoolbar'] ['database'] = null;
}
$_SESSION ['OI_DEBUG'] ['debugtoolbar'] ['database'] [] = $debugtoolbar;
*/
$log = $_SESSION['OI_DEBUG']['log'];
if(empty($log)){
// Create the logger
// Include and configure log4php
\Logger::configure(BASE_DIR . 'config.xml');
// The __CLASS__ constant holds the class name, in our case "Foo".
// Therefore this creates a logger named "Foo" (which we configured in the config file)
$log = \Logger::getLogger(__CLASS__);
}
$log->debug(json_encode($debugtoolbar));
}
更新
我现在正在使用会话 ID,但这仅在用户登录时才有效
if($_COOKIE['PHPSESSID']){
$log = $_SESSION['OI_DEBUG']['log'];
if(empty($log)){
// Create the logger
// Include and configure log4php
\Logger::configure(BASE_DIR . 'config.xml');
// The __CLASS__ constant holds the class name, in our case "Foo".
// Therefore this creates a logger named "Foo" (which we configured in the config file)
$log = \Logger::getLogger(__CLASS__);
}
$appender = new \LoggerAppenderRollingFile("default");
$appender->setFile(BASE_DIR . "cache/log4php_".$_COOKIE['PHPSESSID'].".log", true);
$appender->activateOptions();
$log->removeAllAppenders();
$log->addAppender($appender);
$log->debug(json_encode($debugtoolbar));
}