Introducción a Zend Framework

 Aprendiendo Zend Framework

Apéndice

 Referencia de Zend Framework


  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Traducción al 26.9% - Actualizado el 2011-11-16 - Revisión 24249 - Versión ZF 1.11.x

    44.5. Using the Factory to Create a Log

    In addition to direct instantiation, you may also use the static factory() method to instantiate a Log instance, as well as to configure attached writers and their filters. Using the factory, you can attach zero or more writers. Configuration may be passed as either an array or a Zend_Config instance.

    As an example:

    $logger Zend_Log::factory(array(
        
    'timestampFormat' => 'Y-m-d',
        array(
            
    'writerName'   => 'Stream',
            
    'writerParams' => array(
                
    'stream'   => '/tmp/zend.log',
            ),
            
    'formatterName' => 'Simple',
            
    'formatterParams' => array(
                
    'format'   => '%timestamp%: %message% -- %info%',
            ),
            
    'filterName'   => 'Priority',
            
    'filterParams' => array(
                
    'priority' => Zend_Log::WARN,
            ),
        ),
        array(
            
    'writerName'   => 'Firebug',
            
    'filterName'   => 'Priority',
            
    'filterParams' => array(
                
    'priority' => Zend_Log::INFO,
            ),
        ),
    ));

    The above will instantiate a logger with two writers, one for writing to a local file, another for sending data to Firebug. Each has an attached priority filter, with different maximum priorities.

    By default, events are logged with the ISO 8601 date format. You can choose your own format with the option timestampFormat.

    Each writer can be defined with the following keys:

    writerName (required)

    The "short" name of a log writer; the name of the log writer minus the leading class prefix/namespace. See the "writerNamespace" entry below for more details. Examples: "Mock", "Stream", "Firebug".

    writerParams (optional)

    An associative array of parameters to use when instantiating the log writer. Each log writer's factory() method will map these to constructor arguments, as noted below.

    writerNamespace (optional)

    The class prefix/namespace to use when constructing the final log writer classname. By default, if this is not provided, "Zend_Log_Writer" is assumed; however, you can pass your own namespace if you are using a custom log writer.

    formatterName (optional)

    The "short" name of a formatter to use with the given log writer; the name of the formatter minus the leading class prefix/namespace. See the "formatterNamespace" entry below for more details. Examples: "Simple", "Xml".

    formatterParams (optional)

    An associative array of parameters to use when instantiating the log formatter. Each log formatter's factory() method will map these to constructor arguments, as noted below.

    formatterNamespace (optional)

    The class prefix/namespace to use when constructing the final log formatter classname. By default, if this is not provided, "Zend_Log_Formatter" is assumed; however, you can pass your own namespace if you are using a custom log formatter.

    filterName (optional)

    The "short" name of a filter to use with the given log writer; the name of the filter minus the leading class prefix/namespace. See the "filterNamespace" entry below for more details. Examples: "Message", "Priority".

    filterParams (optional)

    An associative array of parameters to use when instantiating the log filter. Each log filter's factory() method will map these to constructor arguments, as noted below.

    filterNamespace (optional)

    The class prefix/namespace to use when constructing the final log filter classname. By default, if this is not provided, "Zend_Log_Filter" is assumed; however, you can pass your own namespace if you are using a custom log filter.

    Each writer and each filter has specific options.

    44.5.1. Writer Options

    44.5.1.1. Zend_Log_Writer_Db Options

    db

    A Zend_Db_Adapter instance.

    table

    The name of the table in the RDBMS that will contain log entries.

    columnMap

    An associative array mapping database table column names to log event fields.

    44.5.1.2. Zend_Log_Writer_Firebug Options

    This log writer takes no options; any provided will be ignored.

    44.5.1.3. Zend_Log_Writer_Mail Options

    Tabla 44.3. Zend_Log_Writer_Mail Options

    Option Data Type Default Value Description
    mail String Zend_Mail An Zend_Mail instance
    charset String iso-8859-1 Charset of the mail
    from String or Array NULL Sender of the mail The parameters for Array type are :
    • email : address of sender

    • name : name of sender

    to String or Array NULL Recipient(s) of the mail
    cc String or Array NULL Carbon copy recipient(s) of the mail
    bcc String or Array NULL Blind carbon copy recipient(s) of the mail
    subject String NULL Subject of the mail
    subjectPrependText String NULL A summary of number of errors per priority is appended to the subject of the mail
    layout String NULL An Zend_Layout instance
    layoutOptions Array NULL See the section Sección 40.3, “Zend_Layout Configuration Options”
    layoutFormatter String NULL An Zend_Log_Formatter_Interface instance

    44.5.1.4. Zend_Log_Writer_Mock Options

    This log writer takes no options; any provided will be ignored.

    44.5.1.5. Zend_Log_Writer_Null Options

    This log writer takes no options; any provided will be ignored.

    44.5.1.6. Zend_Log_Writer_Stream Options

    stream|url

    A valid PHP stream identifier to which to log.

    mode

    The I/O mode with which to log; defaults to "a", for "append".

    44.5.1.7. Zend_Log_Writer_Syslog Options

    application

    Application name used by the syslog writer.

    facility

    Facility used by the syslog writer.

    44.5.1.8. Zend_Log_Writer_ZendMonitor Options

    This log writer takes no options; any provided will be ignored.

    44.5.2. Filter Options

    44.5.2.1. Zend_Log_Filter_Message Options

    regexp

    Regular expression that must be matched in order to log a message.

    44.5.2.2. Zend_Log_Filter_Priority Options

    priority

    The maximum priority level by which messages will be logged.

    operator

    The comparison operator by which to do priority comparisons; defaults to "<=".

    44.5.2.3. Zend_Log_Filter_Suppress Options

    This log filter takes no options; any provided will be ignored.

    44.5.3. Creating Configurable Writers and Filters

    If you find yourself needing to write your own log writers and/or filters, you can make them compatible with Zend_Log::factory() very easily.

    At the minimum, you need to implement Zend_Log_FactoryInterface, which expects a static factory() method that accepts a single argument, $config, which may be either an array or Zend_Config object. If your log writer extends Zend_Log_Writer_Abstract, or your log filter extends Zend_Log_Filter_Abstract, you will pick this up for free.

    Then, simply define mappings between the accepted configuration and any constructor arguments. As an example:

    class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
    {
        public function 
    __construct($bar$baz)
        {
            
    // ...
        
    }

        public static function 
    factory($config)
        {
            if (
    $config instanceof Zend_Config) {
                
    $config $config->toArray();
            }
            if (!
    is_array($config)) {
                throw new 
    Exception(
                    
    'factory expects an array or Zend_Config instance'
                
    );
            }

            
    $default = array(
                
    'bar' => null,
                
    'baz' => null,
            );
            
    $config array_merge($default$config);

            return new 
    self(
                
    $config['bar'],
                
    $config['baz']
            );
        }
    }

    Alternately, you could call appropriate setters after instantiation, but prior to returning the instance:

    class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
    {
        public function 
    __construct($bar null$baz null)
        {
            
    // ...
        
    }

        public function 
    setBar($value)
        {
            
    // ...
        
    }

        public function 
    setBaz($value)
        {
            
    // ...
        
    }

        public static function 
    factory($config)
        {
            if (
    $config instanceof Zend_Config) {
                
    $config $config->toArray();
            }
            if (!
    is_array($config)) {
                throw new 
    Exception(
                    
    'factory expects an array or Zend_Config instance'
                
    );
            }

            
    $writer = new self();
            if (isset(
    $config['bar'])) {
                
    $writer->setBar($config['bar']);
            }
            if (isset(
    $config['baz'])) {
                
    $writer->setBaz($config['baz']);
            }
            return 
    $writer;
        }
    }
    digg delicious meneame google twitter technorati facebook

    Comentarios

    Loading...