Zend Framework の紹介

 Learning Zend Framework

appendix

 Zend Framework リファレンス


  • 第36章 Zend_Gdata
  • 第37章 Zend_Http
  • 第38章 Zend_InfoCard
  • 第39章 Zend_Json
  • 第40章 Zend_Layout
  • 第41章 Zend_Ldap
  • 第42章 Zend_Loader
  • 第43章 Zend_Locale
  • 第44章 Zend_Log
  • 第45章 Zend_Mail
  • 第46章 Zend_Markup
  • 第47章 Zend_Measure
  • 第48章 Zend_Memory
  • 第49章 Zend_Mime
  • 第50章 Zend_Navigation
  • 第51章 Zend_Oauth
  • 第52章 Zend_OpenId
  • 第53章 Zend_Paginator
  • 第54章 Zend_Pdf
  • 第55章 Zend_ProgressBar
  • 第56章 Zend_Queue
  • 第57章 Zend_Reflection
  • 第58章 Zend_Registry
  • 第59章 Zend_Rest

  • 第60章 Zend_Search_Lucene
  • 第61章 Zend_Serializer
  • 第62章 Zend_Server
  • 第63章 Zend_Service
  • 第64章 Zend_Session
  • 第65章 Zend_Soap
  • 第66章 Zend_Tag
  • 第67章 Zend_Test
  • 第68章 Zend_Text
  • 第69章 Zend_TimeSync
  • 第70章 Zend_Tool
  • 第71章 Zend_Tool_Framework
  • 第72章 Zend_Tool_Project
  • 第73章 Zend_Translate
  • 第74章 Zend_Uri
  • 第75章 Zend_Validate
  • 第76章 Zend_Version
  • 第77章 Zend_View
  • 第78章 Zend_Wildfire
  • 第79章 Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 70.6% Update 2010-11-28 - Revision 23415

    第44章 Zend_Log

    目次

    44.1. Overview
    44.1.1. Creating a Log
    44.1.2. Logging Messages
    44.1.3. Destroying a Log
    44.1.4. Using Built-in Priorities
    44.1.5. Adding User-defined Priorities
    44.1.6. Understanding Log Events
    44.1.7. Log PHP Errors
    44.2. Writers
    44.2.1. Writing to Streams
    44.2.2. Writing to Databases
    44.2.3. Firebug への書き込み
    44.2.3.1. 優先度のスタイルの設定
    44.2.3.2. ログ記録用のデータの準備
    44.2.3.3. 例外のログ記録
    44.2.3.4. 表形式のログ
    44.2.4. Email への書き込み
    44.2.4.1. Zend_Layout の使用法
    44.2.4.2. 件名でのエラーの概要
    44.2.4.3. 警告
    44.2.5. システムログへの書き込み
    44.2.6. Writing to the Zend Server Monitor
    44.2.7. Stubbing Out the Writer
    44.2.8. Testing with the Mock
    44.2.9. Compositing Writers
    44.3. Formatters
    44.3.1. Simple Formatting
    44.3.2. Formatting to XML
    44.4. Filters
    44.4.1. Filtering for All Writers
    44.4.2. Filtering for a Writer Instance
    44.5. Using the Factory to Create a Log
    44.5.1. Writer Options
    44.5.1.1. Zend_Log_Writer_Db Options
    44.5.1.2. Zend_Log_Writer_Firebug Options
    44.5.1.3. Zend_Log_Writer_Mail Options
    44.5.1.4. Zend_Log_Writer_Mock Options
    44.5.1.5. Zend_Log_Writer_Null Options
    44.5.1.6. Zend_Log_Writer_Stream Options
    44.5.1.7. Zend_Log_Writer_Syslog Options
    44.5.1.8. Zend_Log_Writer_ZendMonitor Options
    44.5.2. Filter Options
    44.5.2.1. Zend_Log_Filter_Message Options
    44.5.2.2. Zend_Log_Filter_Priority Options
    44.5.2.3. Zend_Log_Writer_Suppress Options
    44.5.3. Creating Configurable Writers and Filters

    44.1. Overview

    Zend_Log is a component for general purpose logging. It supports multiple log backends, formatting messages sent to the log, and filtering messages from being logged. These functions are divided into the following objects:

    • A Log (instance of Zend_Log) is the object that your application uses the most. You can have as many Log objects as you like; they do not interact. A Log object must contain at least one Writer, and can optionally contain one or more Filters.

    • A Writer (inherits from Zend_Log_Writer_Abstract) is responsible for saving data to storage.

    • A Filter (implements Zend_Log_Filter_Interface) blocks log data from being saved. A filter may be applied to an individual Writer, or to a Log where it is applied before all Writers. In either case, filters may be chained.

    • A Formatter (implements Zend_Log_Formatter_Interface) can format the log data before it is written by a Writer. Each Writer has exactly one Formatter.

    44.1.1. Creating a Log

    To get started logging, instantiate a Writer and then pass it to a Log instance:

    $logger = new Zend_Log();
    $writer = new Zend_Log_Writer_Stream('php://output');

    $logger->addWriter($writer);

    It is important to note that the Log must have at least one Writer. You can add any number of Writers using the Log's addWriter() method.

    Alternatively, you can pass a Writer directly to constructor of Log as a shortcut:

    $writer = new Zend_Log_Writer_Stream('php://output');
    $logger = new Zend_Log($writer);

    The Log is now ready to use.

    44.1.2. Logging Messages

    To log a message, call the log() method of a Log instance and pass it the message with a corresponding priority:

    $logger->log('Informational message'Zend_Log::INFO);

    The first parameter of the log() method is a string message and the second parameter is an integer priority. The priority must be one of the priorities recognized by the Log instance. This is explained in the next section.

    A shortcut is also available. Instead of calling the log() method, you can call a method by the same name as the priority:

    $logger->log('Informational message'Zend_Log::INFO);
    $logger->info('Informational message');

    $logger->log('Emergency message'Zend_Log::EMERG);
    $logger->emerg('Emergency message');

    44.1.3. Destroying a Log

    If the Log object is no longer needed, set the variable containing it to NULL to destroy it. This will automatically call the shutdown() instance method of each attached Writer before the Log object is destroyed:

    $logger null;

    Explicitly destroying the log in this way is optional and is performed automatically at PHP shutdown.

    44.1.4. Using Built-in Priorities

    The Zend_Log class defines the following priorities:

    EMERG   0;  // Emergency: system is unusable
    ALERT   1;  // Alert: action must be taken immediately
    CRIT    2;  // Critical: critical conditions
    ERR     3;  // Error: error conditions
    WARN    4;  // Warning: warning conditions
    NOTICE  5;  // Notice: normal but significant condition
    INFO    6;  // Informational: informational messages
    DEBUG   7;  // Debug: debug messages

    These priorities are always available, and a convenience method of the same name is available for each one.

    The priorities are not arbitrary. They come from the BSD syslog protocol, which is described in RFC-3164. The names and corresponding priority numbers are also compatible with another PHP logging system, PEAR Log, which perhaps promotes interoperability between it and Zend_Log.

    Priority numbers descend in order of importance. EMERG (0) is the most important priority. DEBUG (7) is the least important priority of the built-in priorities. You may define priorities of lower importance than DEBUG. When selecting the priority for your log message, be aware of this priority hierarchy and choose appropriately.

    44.1.5. Adding User-defined Priorities

    User-defined priorities can be added at runtime using the Log's addPriority() method:

    $logger->addPriority('FOO'8);

    The snippet above creates a new priority, FOO, whose value is '8'. The new priority is then available for logging:

    $logger->log('Foo message'8);
    $logger->foo('Foo Message');

    New priorities cannot overwrite existing ones.

    44.1.6. Understanding Log Events

    When you call the log() method or one of its shortcuts, a log event is created. This is simply an associative array with data describing the event that is passed to the writers. The following keys are always created in this array: timestamp, message, priority, and priorityName.

    The creation of the event array is completely transparent. However, knowledge of the event array is required for adding an item that does not exist in the default set above.

    To add a new item to every future event, call the setEventItem() method giving a key and a value:

    $logger->setEventItem('pid'getmypid());

    The example above sets a new item named pid and populates it with the PID of the current process. Once a new item has been set, it is available automatically to all writers along with all of the other data event data during logging. An item can be overwritten at any time by calling the setEventItem() method again.

    Setting a new event item with setEventItem() causes the new item to be sent to all writers of the logger. However, this does not guarantee that the writers actually record the item. This is because the writers won't know what to do with it unless a formatter object is informed of the new item. Please see the section on Formatters to learn more.

    44.1.7. Log PHP Errors

    Zend_Log can also be used to log PHP errors. Calling registerErrorHandler() will add Zend_Log before the current error handler, and will pass the error along as well.

    表 44.1. Zend_Log events from PHP errors have the additional fields matching handler ( int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] ) from set_error_handler

    Name Error Handler Paramater Description
    message errstr Contains the error message, as a string.
    errno errno Contains the level of the error raised, as an integer.
    file errfile Contains the filename that the error was raised in, as a string.
    line errline Contains the line number the error was raised at, as an integer.
    context errcontext (optional) An array that points to the active symbol table at the point the error occurred. In other words, errcontext will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context.

    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...