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.3. Formatters

    A Formatter is an object that is responsible for taking an event array describing a log event and outputting a string with a formatted log line.

    Some Writers are not line-oriented and cannot use a Formatter. An example is the Database Writer, which inserts the event items directly into database columns. For Writers that cannot support a Formatter, an exception is thrown if you attempt to set a Formatter.

    44.3.1. Simple Formatting

    Zend_Log_Formatter_Simple is the default formatter. It is configured automatically when you specify no formatter. The default configuration is equivalent to the following:

    $format '%timestamp% %priorityName% (%priority%): %message%' PHP_EOL;
    $formatter = new Zend_Log_Formatter_Simple($format);

    A formatter is set on an individual Writer object using the Writer's setFormatter() method:

    $writer = new Zend_Log_Writer_Stream('php://output');
    $formatter = new Zend_Log_Formatter_Simple('hello %message%' PHP_EOL);
    $writer->setFormatter($formatter);

    $logger = new Zend_Log();
    $logger->addWriter($writer);

    $logger->info('there');

    // outputs "hello there"

    The constructor of Zend_Log_Formatter_Simple accepts a single parameter: the format string. This string contains keys surrounded by percent signs (e.g. %message%). The format string may contain any key from the event data array. You can retrieve the default keys by using the DEFAULT_FORMAT constant from Zend_Log_Formatter_Simple.

    44.3.2. Formatting to XML

    Zend_Log_Formatter_Xml formats log data into XML strings. By default, it automatically logs all items in the event data array:

    $writer = new Zend_Log_Writer_Stream('php://output');
    $formatter = new Zend_Log_Formatter_Xml();
    $writer->setFormatter($formatter);

    $logger = new Zend_Log();
    $logger->addWriter($writer);

    $logger->info('informational message');

    The code above outputs the following XML (space added for clarity):

    <logEntry>
      <
    timestamp>2007-04-06T07:24:37-07:00</timestamp>
      <
    message>informational message</message>
      <
    priority>6</priority>
      <
    priorityName>INFO</priorityName>
    </
    logEntry>

    It's possible to customize the root element as well as specify a mapping of XML elements to the items in the event data array. The constructor of Zend_Log_Formatter_Xml accepts a string with the name of the root element as the first parameter and an associative array with the element mapping as the second parameter:

    $writer = new Zend_Log_Writer_Stream('php://output');
    $formatter = new Zend_Log_Formatter_Xml('log',
                                            array(
    'msg' => 'message',
                                                  
    'level' => 'priorityName')
                                           );
    $writer->setFormatter($formatter);

    $logger = new Zend_Log();
    $logger->addWriter($writer);

    $logger->info('informational message');

    The code above changes the root element from its default of logEntry to log. It also maps the element msg to the event data item message. This results in the following output:

    <log>
      <
    msg>informational message</msg>
      <
    level>INFO</level>
    </
    log>
    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...