2

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?

mtk
  • 13,221
  • 16
  • 72
  • 112

2 Answers2

4

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.

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Hey,Thanks for the solution, great effort. I have found a simple work-around for this. will post soon. But your solution too works :) – mtk Oct 13 '12 at 17:53
  • @mtk I like Sven's answer, but I'm eager to see yours too. Will you be posting it soon? – wmarbut Oct 23 '12 at 16:57
  • @wmar Have posted it. please check. sorry for delay, had forgot about this. – mtk Oct 23 '12 at 17:22
2

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.

Community
  • 1
  • 1
mtk
  • 13,221
  • 16
  • 72
  • 112
  • I feel a little bit of cheating here, but creative solution. Bad thing is you have to add the whole creation of the logging message over and over again if it is needed in more than one place. – Sven Oct 23 '12 at 21:31
  • I have put here the minimalized code. I have my own function of logging(in which the log to log4php is there) which is called instead of this log statement. But yes, that too needs to pass atleast the filename as parameter. – mtk Oct 24 '12 at 06:55