php – 在Zend框架应用程序中记录设计模式

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在Zend框架应用程序中记录设计模式脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Zend Framework构建一个应用程序.应用程序需要对代码中的每个操作或函数进行密集的日志记录. @H_777_1@所以我的代码大部分时间看起来像这样:

function SendMailsAction(){
      $LOGger->log('Started sending mails.')
...
...
...some Code...
...
...

foreach ($mails as $mail){
  try{      
       $logger->log('Trying to send')
       $mail->send()
       $logger->log('Mail sent successfully.')
  }catch(Exception $e){
       $logger->log('Failed to send mail.')
      }      
 }

...
...
...some Code...
...
...
       $logger->log('Finished sending mails.')
}

有时我甚至必须登录2个表,所以绝大多数记录代码加倍,函数开始变得复杂和漫长.

我使用zend框架的Zend_Log进行日志记录,所以我的问题不是记录类本身,而是将日志记录代码代码功能本身分离开来,并保持分离问题.

有些人提出了面向方面的编程(AOP),但不幸的是,对于我的客户来说,不幸的是,PHP的AOP是不可接受的,所以我正在寻找面向对象的解决方案或最佳实践.

注意:

只是为了使事情清楚我的问题不是如何使用Zend_Log,但如何添加日志记录到我的应用程序代码一般.

会很久如果您的代码执行大量日志记录,则会很长时间,因为它将不得不记录,并且其记录的每一行操作将意味着代码中有一行.然而,在任何情况下,不应该复杂,因为日志记录是您可以做的最简单的事情之一.令我担心的是,你提到“有时我甚至要登录2张桌子”.在我的书中,一行,两张,五张,六千张或一千张表由一行进行.每个记录器的代码不会翻倍.如果您要复制一行,并将$log更改为$log2,则显然会出错(tm).

很好,AOP.它有缺点;与debug_backtrace方法一样,性能很高.那么加上代码变得越来越“神奇”,因为当您查看代码本身时,它会执行不清楚的事情.这增加了调试应用程序的时间.

我的$0.02首先,不要重复你自己:每个动作一个日志条目应该是足够的.使用灵活的记录器,可以在运行时连接到某些类.根据“严重性”或“类型”,确定是否将消息实际记录在记录器中.总而言之,只是实现观察者模式:

<?PHP

namespace Foo;

class MailService {
    public function attach( Observes $observer ) {
        $this->observers[] = $observer;
    }

    public function notify( $message,$tyPE = 'notice' ) {
        foreach( $this->observers as $observer ) {
            $observer->notify( $message,$type );
        }
    }

    public function sendMail( ) {
        $this->notify( 'Started sending mails','debug' );
        $mails = array( );
        foreach( $mails as $mail ) {
            try {
                $this->notify( 'Trying to send','debug' );
                $mail->send( );
                $this->notify( 'Mail sent succesfully','debug' );
            }
            catch( Exception $e ) {
                $this->notify( 'Failed to send mail','notice' );
            }
        }
        $this->notify( 'Finished sending mail','debug' );
    }
}

interface Observes {
    public function notify( $message,$type = 'notice' );
}

abstract class Logger implements Observes {
    PRotected $types = array(
        'debug' => 0,'notice' => 1,'warning' => 2,'error' => 3
    );

    protected function code( $type ) {
        return isset( $this->types[$type] ) ? $this->types[$type] : 0;
    }
}

class FileLogger extends Logger implements Observes {

    public function __construct( $filename ) {
        $this->filename = $filename;
    }

    /**
     * @todo replace the method body wITh a call to,say,file_put_contents.
     */
    public function notify( $message,$type = 'notice' ) {
        if( $this->code( $type ) > $this->code( 'notice' ) ) { // only for warning and error.
            echo $message . "\n";
        }
    }


}

class DebugLogger extends Logger implements Observes {
    public function notify( $message,$type = 'notice' ) {
        if( $this->code( $type ) === $this->code( 'debug' ) ) { // only show "debug" notices.
            echo $message . "\n";
        }
    }
}


$service = new MailService( );
$service->attach( new FileLogger( 'yourlog.txt' ) );
$service->attach( new DebugLogger( ) );
$service->sendMail( );

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在Zend框架应用程序中记录设计模式全部内容,希望文章能够帮你解决php – 在Zend框架应用程序中记录设计模式所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。