Skip to content
This repository has been archived by the owner on Dec 19, 2020. It is now read-only.

tweak log4php file:line column to optionally ignore log4php wrappers and optionally shorten file path #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/main/php/LoggerLoggingEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,35 @@ public function __construct($fqcn, $logger, LoggerLevel $level, $message, $timeS
public function getFullQualifiedClassname() {
return $this->fqcn;
}


private static $ignoreFileRegex;
private static $ignorePathRegexSrc = "//";
private static $ignorePathRegexDst = "";

/**
* Set filter for use by getLocationInformation() to ignore log4php caller wrapper functions.
*/
public static function setIgnoreFileRegex($regex) {
self::$ignoreFileRegex = $regex; // e.g. '/\/(my-log4php-wrapper.php|myOtherLog4PhpWrapper.php)$/'
}

/**
* Set filter for use by getLocationInformation() to ignore a common file prefix, or ignore entire path.
*/
public static function setIgnorePathRegex($regexSrc, $regexDst = "") {
self::$ignorePathRegexSrc = $regexSrc; // e.g. '/^.*\/(ignore everything before this folder\/)/' or ignore entire path '/^.*\/([^\/]+)/'
self::$ignorePathRegexDst = $regexDst; // e.g. '' or '$1'
}

/**
* Set the location information for this logging event. The collected
* information is cached for future use.
*
* <p>This method uses {@link PHP_MANUAL#debug_backtrace debug_backtrace()} function (if exists)
* to collect informations about caller.</p>
* <p>It only recognize informations generated by {@link Logger} and its subclasses.</p>
* <p>Use setIgnoreFileRegex() to skip file(s) of caller using own log4php wrapper class/function.</p>
* <p>Use setIgnorePathRegex() to shorten reported file path by removing a common prefix.</p>
* @return LoggerLocationInfo
*/
public function getLocationInformation() {
Expand All @@ -165,12 +186,21 @@ public function getLocationInformation() {
$hop = array_pop($trace);
while($hop !== null) {
if(isset($hop['class'])) {
// Note: This next if() statement inspired by the following stackoverflow question:
// http://stackoverflow.com/questions/15219327/php-log4php-dynamically-set-fileline-column
if (isset($hop['file']) and isset(self::$ignoreFileRegex)) {
if(1 === preg_match(self::$ignoreFileRegex, $hop['file'])) {
// come here if detected a caller wrapper for log4php
$locationInfo['line'] = $prevHop['line'] ;
$locationInfo['file'] = preg_replace(self::$ignorePathRegexSrc, self::$ignorePathRegexDst, $prevHop['file']); // replace annoyingly verbose path prefix
break;
}
}
// we are sometimes in functions = no class available: avoid php warning here
$className = strtolower($hop['class']);
if(!empty($className) and ($className == 'logger' or
strtolower(get_parent_class($className)) == 'logger')) {
$locationInfo['line'] = $hop['line'];
$locationInfo['file'] = $hop['file'];
if(!empty($className) and ($className == 'logger' or strtolower(get_parent_class($className)) == 'logger')) {
$locationInfo['line'] = $hop['line'] ;
$locationInfo['file'] = preg_replace(self::$ignorePathRegexSrc, self::$ignorePathRegexDst, $hop['file']); // replace annoyingly verbose path prefix
break;
}
}
Expand Down