如何实现“对象转储”用log4php?

发布于 2024-11-07 09:52:42 字数 1565 浏览 0 评论 0原文

我目前正在我们的一个项目中从我们自己的专有日志记录解决方案迁移到 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 );
    }
  }
} 

I'm currently in the process of moving 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 it's members recursively) to the log.

The equivalent place for this method in log4php would be the Logger class (I assume). But I wonder what the proper way would be to integrate the functionality.

Should I just derive from Logger and extend it? Or is there a way to "plug in" this functionality.

Thanks in advance.

/**
* 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 );
    }
  }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不打扰别人 2024-11-14 09:52:42

log4php 文档中对此进行了全部解释。

This is all explained in the log4php docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文