I am currently transitioning from our own proprietary logging solution to log4php in one of our projects. Our own solution has a helper method, which I posted below. The purpose of the method is to write the contents of a variable (and any of its elements recursively) to the log.
The equivalent place for this method in log4php is the Logger class (suppose). But I wonder what the correct way would be to integrate the functionality.
Should I just extract from Logger and expand it? Or is there a way to βconnectβ this functionality.
Thanks in advance.
public static function dump( $target, $level = Logging::eDEBUG, $indent = 0 ) {
if( $level < self::getInstance()->logLevel ) return;
if( null == $target ) {
self::log( "d", "> " . str_repeat( "\t", $indent ) . "null", $level );
return;
}
if( is_string( $target ) || is_numeric( $target ) ) {
self::log( "d", "> " . str_repeat( "\t", $indent ) . $target, $level );
return;
}
foreach( $target as $key => $value ) {
if( is_array( $value ) ) {
self::log( "d", "> " . str_repeat( "\t", $indent ) . $key . " -> Array (", $level );
self::dump( $value, $level, $indent + 1 );
self::log( "d", "> " . str_repeat( "\t", $indent ) . ")", $level );
continue;
}
if( is_object( $value ) ) {
self::log( "d", "> " . str_repeat( "\t", $indent ) . $key . " -> Object (", $level );
self::dump( (array)$value, $level, $indent + 1 );
self::log( "d", "> " . str_repeat( "\t", $indent ) . ")", $level );
} else {
self::log( "d", "> " . str_repeat( "\t", $indent ) . $key . " -> " . $value, $level );
}
}
}
source
share