From 6ae0e4f04e01731c9bf5a7061e33460d88c11aff Mon Sep 17 00:00:00 2001 From: Simon Hardy-Francis Date: Wed, 5 Nov 2014 15:59:26 -0800 Subject: [PATCH 1/3] tweak log4php file:line column to optionally ignore log4php wrappers and optionally shorten file path --- src/main/php/LoggerLoggingEvent.php | 38 +++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/php/LoggerLoggingEvent.php b/src/main/php/LoggerLoggingEvent.php index 29ba3610..c26b1a7c 100644 --- a/src/main/php/LoggerLoggingEvent.php +++ b/src/main/php/LoggerLoggingEvent.php @@ -145,7 +145,24 @@ public function __construct($fqcn, $logger, LoggerLevel $level, $message, $timeS public function getFullQualifiedClassname() { return $this->fqcn; } - + + private static $ignoreFileRegex; + private static $ignorePathRegex = "//"; + + /** + * 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($regex) { + self::$ignorePathRegex = $regex; // e.g. '/^.*\/(ignore everything before this folder\/)/' or ignore entire path '/^.*\/([^\/]+)/' + } + /** * Set the location information for this logging event. The collected * information is cached for future use. @@ -153,6 +170,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 +184,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::$ignorePathRegex, "$1", $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::$ignorePathRegex, "$1", $hop['file']); // replace annoyingly verbose path prefix break; } } From ff8f5fe407fb762b69a02e3357284b8a0fbf26b8 Mon Sep 17 00:00:00 2001 From: Simon Hardy-Francis Date: Tue, 18 Nov 2014 15:02:45 -0800 Subject: [PATCH 2/3] - syslog chomps file pathes up to AND INCLUDING regex folder(s) specified --- src/main/php/LoggerLoggingEvent.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/php/LoggerLoggingEvent.php b/src/main/php/LoggerLoggingEvent.php index c26b1a7c..7756251f 100644 --- a/src/main/php/LoggerLoggingEvent.php +++ b/src/main/php/LoggerLoggingEvent.php @@ -147,7 +147,8 @@ public function getFullQualifiedClassname() { } private static $ignoreFileRegex; - private static $ignorePathRegex = "//"; + private static $ignorePathRegexSrc = "//"; + private static $ignorePathRegexDst = ""; /** * Set filter for use by getLocationInformation() to ignore log4php caller wrapper functions. @@ -159,8 +160,9 @@ public static function setIgnoreFileRegex($regex) { /** * Set filter for use by getLocationInformation() to ignore a common file prefix, or ignore entire path. */ - public static function setIgnorePathRegex($regex) { - self::$ignorePathRegex = $regex; // e.g. '/^.*\/(ignore everything before this folder\/)/' 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' } /** @@ -189,16 +191,16 @@ public function getLocationInformation() { 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::$ignorePathRegex, "$1", $prevHop['file']); // replace annoyingly verbose path prefix + $locationInfo['line'] = $prevHop['line'] ; + $locationInfo['file'] = preg_replace(self::$ignorePathRegexSrc, $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'] = preg_replace(self::$ignorePathRegex, "$1", $hop['file']); // replace annoyingly verbose path prefix + $locationInfo['line'] = $hop['line'] ; + $locationInfo['file'] = preg_replace(self::$ignorePathRegexSrc, $ignorePathRegexDst, $hop['file']); // replace annoyingly verbose path prefix break; } } From 66703d6c25340cfd48c3fcbc261042517dd9ff83 Mon Sep 17 00:00:00 2001 From: Simon Hardy-Francis Date: Tue, 18 Nov 2014 15:21:59 -0800 Subject: [PATCH 3/3] - fix typo --- src/main/php/LoggerLoggingEvent.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/php/LoggerLoggingEvent.php b/src/main/php/LoggerLoggingEvent.php index 7756251f..0aba5e6c 100644 --- a/src/main/php/LoggerLoggingEvent.php +++ b/src/main/php/LoggerLoggingEvent.php @@ -191,16 +191,16 @@ public function getLocationInformation() { 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, $ignorePathRegexDst, $prevHop['file']); // replace annoyingly verbose path prefix + $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'] = preg_replace(self::$ignorePathRegexSrc, $ignorePathRegexDst, $hop['file']); // replace annoyingly verbose path prefix + $locationInfo['line'] = $hop['line'] ; + $locationInfo['file'] = preg_replace(self::$ignorePathRegexSrc, self::$ignorePathRegexDst, $hop['file']); // replace annoyingly verbose path prefix break; } }