custom file:line column entries

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

custom file:line column entries

David Coombes
HI,

I have a method in a class that calls Logger::info() The issue is the file:line column in the html appender will always report my helper method and not the calling file:line. Using php's debug_backtrace I can get the calling file:line but can't see how I can dynamically change the column. Is this possible, or is there a Logger::method() that I need to overwrite?

My helper method currently is:
public function log( $msg, $level='info' ){
//trace
$bt = debug_backtrace();
$caller = array_shift($bt);
$trace = $caller['file'] . ":" . $caller['line'];
$this->log_api->trace($trace);
//log
$this->log_api->$level( $msg );
}
}

And my xml config is:
<appender name="myHTMLFileAppender" class="LoggerAppenderFile">
<layout class="LoggerLayoutHtml">
<param name="locationInfo" value="true" />
</layout>
<param name="file" value="/path/to/logfile/log.html" />
<param name="append" value="false" />
</appender>

regards,
Daithi

--
"Any society that would give up a little liberty to gain a little security,
               will deserve neither and lose both." - Benjamin Franklin
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: custom file:line column entries

Ivan Habunek
On 5 March 2013 19:05, David Coombes <[hidden email]> wrote:
> I have a method in a class that calls Logger::info() The issue is the
> file:line column in the html appender will always report my helper method
> and not the calling file:line. Using php's debug_backtrace I can get the
> calling file:line but can't see how I can dynamically change the column. Is
> this possible, or is there a Logger::method() that I need to overwrite?

Hi!

Currently it's not possible to override the location information.
There is a hackish way of doing this, but you have to modify log4php
code.

Add this method to LoggerLoggingEvent class (located in /src/main/php/):
public function setLocationInformation(LoggerLocationInfo $locationInfo) {
    $this->locationInfo = $locationInfo;
}

And then do this:

public function log($msg, $level = "info")
{
    // Manually construct a logging event
    $level = LoggerLevel::toLevel($level);
    $logger = Logger::getLogger(__CLASS__);
    $event = new LoggerLoggingEvent(__CLASS__, $logger, $level, $msg);

    // Override the location info
    $bt = debug_backtrace();
    $caller = array_shift($bt);
    $location = new LoggerLocationInfo($caller);
    $event->setLocationInformation($location);

    // Log it
    $logger->logEvent($event);
}

Just tried it out. Seems to work fine. Let me know how it works.

I will add LoggerLoggingEvent::setLocationInformation() to the develop
branch so it will be included in next release.

Looking at the HTML layout... man, it's really outdated. Still has
Category instead of Logger, and other stuff... Will have to update it.
:)

Best regards,
Ivan
Loading...