1

I am new to log4php and I am trying to implement this on my custom framework. I have managed to get all db queries to file. what I want to do now is log it in a way so i can identify each client so that I can view debug my browser without seeing all the other clients in the same file.

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 db class

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));
        }

UPDATE

I am using session id now, but this only works if user is logged in

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));
            }
Cœur
  • 37,241
  • 25
  • 195
  • 267
shorif2000
  • 2,582
  • 12
  • 65
  • 137

0 Answers0