diff --git a/src/main/php/LoggerLoggingEvent.php b/src/main/php/LoggerLoggingEvent.php index 29ba3610..0aba5e6c 100644 --- a/src/main/php/LoggerLoggingEvent.php +++ b/src/main/php/LoggerLoggingEvent.php @@ -145,7 +145,26 @@ 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. @@ -153,6 +172,8 @@ public function getFullQualifiedClassname() { *

This method uses {@link PHP_MANUAL#debug_backtrace debug_backtrace()} function (if exists) * to collect informations about caller.

*

It only recognize informations generated by {@link Logger} and its subclasses.

+ *

Use setIgnoreFileRegex() to skip file(s) of caller using own log4php wrapper class/function.

+ *

Use setIgnorePathRegex() to shorten reported file path by removing a common prefix.

* @return LoggerLocationInfo */ public function getLocationInformation() { @@ -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; } }