Search code examples
phploggingfilenameslog4phppattern-layout

log4php- Set layout pattern to display filename


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?


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.