You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4php-user@logging.apache.org by Lst Recv <li...@gmail.com> on 2005/06/23 00:15:30 UTC

Logging the user's IP with log4php

Is there anyway to have it include the user's IP address,
PHPSESSIONID, URL, and filename/linenumber?  These things are very
useful - more than a PID etc in PHP.

Re: Logging the user's IP with log4php

Posted by "Marco V." <ma...@apache.org>.
----- Original Message -----
From: "Lst Recv" <li...@gmail.com>
To: <lo...@logging.apache.org>
Sent: Thursday, June 23, 2005 12:15 AM
Subject: Logging the user's IP with log4php


>Is there anyway to have it include the user's IP address,
>PHPSESSIONID, URL, and filename/linenumber?  These things are very
>useful - more than a PID etc in PHP.

You can use the method LoggerLoggingEvent::getMDC() (see LoggerMDC class for details).
If you use the LoggerPatternLayout a conversion character %X{clientNumber} is what you need.
With %X{server.REMOTE_ADDR} you get the client IP address.
Put any key/value pair using the static method LoggerMDC::put(key, value) and get the value via %X{key}.

Example:
----
LoggerMDC::put('sessionid', session_id());
....
in log4php.xml
...
    <appender name="default" class="LoggerAppenderEcho">
        <layout class="LoggerPatternLayout">
            <param name="conversionPattern" value="%-5p [%t] [%X{sessionid}]: %m in %F line %L%n" />
        </layout>
    </appender>
....

For filename/linenumber:
with LoggerPatternLayout use the %F, %L conversion chars and with LoggerLayoutHtml, LoggerXmlLayout set the "locationInfo" property
"true". (with locationInfo enabled every log event calls a debug backtrace... (see LoggerLoggingEvent::getLocationInformation()).

-Marco