3

我目前正在我们的一个项目中从我们自己的专有日志记录解决方案转移到 log4php。我们自己的解决方案有一个我在下面发布的辅助方法。该方法的目的是将变量(及其任何成员递归)的内容写入日志。

此方法在 log4php 中的等效位置是 Logger 类(我假设)。但我想知道集成功能的正确方法是什么。

我应该从 Logger 派生并扩展它吗?或者有没有办法“插入”这个功能。

提前致谢。

/**
* Dump the complete content of the target object to the log.
*
* @param mixed $target The object to dump.
* @param int $level The verbosity level.
* @param int $indent Indentation level.
*/
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 );
    }
  }
} 
4

1 回答 1

2

这一切都在log4php 文档中进行了解释。

于 2011-05-14T16:48:48.367 回答