Question

I am using log4php to log messages in php. I have the following xml configuration

<configuration xmlns="http://logging.apache.org/log4php/">
    <appender name="myAppender" class="LoggerAppenderFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%d{Y-m-d H:i:s} %c %-5p %F %L %m%n" />
        </layout>
        <param name="file" value="myLog.log" />
    </appender>

    <root>
        <level value="TRACE" />
        <appender_ref ref="myAppender" />
    </root>
</configuration>

The concerned part is

<param name="conversionPattern" value="%d{Y-m-d H:i:s} %c %-5p %F %L %m%n" />

The %F is the specifier for getting the file-name. This is logging the message's into the log file. Here is a sample logged message:

2012-09-23 22:15:04 myLog FATAL /media/study/code/live/public_html/log.php 18 My message.

Problem

I want to display only the filename(log.php in this case) and not the complete path (/media/study/code/live/public_html/log.php) of the file here. Have searched the Apache docs and SO but couldn't find anything in this reference.

Any hints how to achieve this?

Was it helpful?

Solution

This task can be completed with a little help of code that you add.

Not surprisingly the pattern layout is configured in layouts/LoggerLayoutPattern.php and has a lengthy array protected static $defaultConverterMap that defines all patterns understood in the conversion pattern. As you can see, the letter "F" is linked with the LoggerPatternConverterFile class in patterns/LoggerPatternConverterFile.php. A quick look reveals:

public function convert(LoggerLoggingEvent $event) {
    return $event->getLocationInformation()->getFileName();
}

That's what is causing the full filepath. Adding a call to the basename() function would return the wished result, but beware this will not survive updates of Log4PHP. You can add it, though, and are done.

If you want a persistant change, you'll have to extend the two mentioned classes and add them to your own autoloading or including:

First extend the LoggerPatternConverterFile. This brings you the basename of the relevant file:

class LoggerPatternConverterFileBasename extends LoggerPatternConverterFile
{
    public function convert(LoggerLoggingEvent $event) {
      return basename(parent::convert($event));
    }
}

Second extend the LoggerLayoutPattern class

class YourLoggerLayoutPattern extends LoggerLayoutPattern {
  public function __construct() {
    parent::__construct();
    $this->converterMap['f'] = 'LoggerPatternConverterFileBasename';
  }
}

That way, you just defined that for "small letter f" you will only see the basename of the file.

In your configuration you just reference this new YourLoggerLayoutPattern class with the changed conversion string.

This change should survive updates to log4php.

OTHER TIPS

It would take a simple effort as explained by Sven in his answer. But I figured out a work around for this, that is as follows:

Don't put any file format in conversionPattern in log4php configuration, instead pass the name of file as the initial part from the php. So, the code

log4php config:

'conversionPattern' => '%d{H:i:s Y-m-d} %c %-5p - %m%n'

Then in php call it as :

$log->info(basename($_SERVER['PHP_SELF']).":".__LINE__": Your message/debug details here");

So, if you can live with this format (and also you get stuck with displaying the file name in the middle always), this would be a good work around.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top