Zend_Log-Factory.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.log.factory">
  4. <title>Using the Factory to Create a Log</title>
  5. <para>
  6. In addition to direct instantiation, you may also use the static
  7. <methodname>factory()</methodname> method to instantiate a Log instance, as well as to
  8. configure attached writers and their filters. Using the factory, you can attach zero or
  9. more writers. Configuration may be passed as either an array or a
  10. <classname>Zend_Config</classname> instance. If you want to create an instance of
  11. a custom class (extending Zend_Log), you can pass a <varname>className</varname>
  12. option to the <methodname>factory()</methodname> method.
  13. </para>
  14. <para>
  15. As an example:
  16. </para>
  17. <programlisting language="php"><![CDATA[
  18. $logger = Zend_Log::factory(array(
  19. 'timestampFormat' => 'Y-m-d',
  20. array(
  21. 'writerName' => 'Stream',
  22. 'writerParams' => array(
  23. 'stream' => '/tmp/zend.log',
  24. ),
  25. 'formatterName' => 'Simple',
  26. 'formatterParams' => array(
  27. 'format' => '%timestamp%: %message% -- %info%',
  28. ),
  29. 'filterName' => 'Priority',
  30. 'filterParams' => array(
  31. 'priority' => Zend_Log::WARN,
  32. ),
  33. ),
  34. array(
  35. 'writerName' => 'Firebug',
  36. 'filterName' => 'Priority',
  37. 'filterParams' => array(
  38. 'priority' => Zend_Log::INFO,
  39. ),
  40. ),
  41. ));
  42. ]]></programlisting>
  43. <para>
  44. The above will instantiate a logger with two writers, one for writing to a local file,
  45. another for sending data to Firebug. Each has an attached priority filter, with different
  46. maximum priorities.
  47. </para>
  48. <para>
  49. By default, events are logged with the ISO 8601 date format. You can choose your own format
  50. with the option <emphasis>timestampFormat</emphasis>.
  51. </para>
  52. <para>
  53. Each writer can be defined with the following keys:
  54. </para>
  55. <variablelist>
  56. <varlistentry>
  57. <term>writerName (required)</term>
  58. <listitem>
  59. <para>
  60. The "short" name of a log writer; the name of the log writer minus the leading
  61. class prefix/namespace. See the "writerNamespace" entry below for more details.
  62. Examples: "Mock", "Stream", "Firebug".
  63. </para>
  64. </listitem>
  65. </varlistentry>
  66. <varlistentry>
  67. <term>writerParams (optional)</term>
  68. <listitem>
  69. <para>
  70. An associative array of parameters to use when instantiating the log writer.
  71. Each log writer's <methodname>factory()</methodname> method will map these to
  72. constructor arguments, as noted below.
  73. </para>
  74. </listitem>
  75. </varlistentry>
  76. <varlistentry>
  77. <term>writerNamespace (optional)</term>
  78. <listitem>
  79. <para>
  80. The class prefix/namespace to use when constructing the final log writer
  81. classname. By default, if this is not provided, "Zend_Log_Writer" is assumed;
  82. however, you can pass your own namespace if you are using a custom log writer.
  83. </para>
  84. </listitem>
  85. </varlistentry>
  86. <varlistentry>
  87. <term>formatterName (optional)</term>
  88. <listitem>
  89. <para>
  90. The "short" name of a formatter to use with the given log writer; the name of the
  91. formatter minus the leading class prefix/namespace. See the "formatterNamespace"
  92. entry below for more details. Examples: "Simple", "Xml".
  93. </para>
  94. </listitem>
  95. </varlistentry>
  96. <varlistentry>
  97. <term>formatterParams (optional)</term>
  98. <listitem>
  99. <para>
  100. An associative array of parameters to use when instantiating the log formatter.
  101. Each log formatter's <methodname>factory()</methodname> method will map these to
  102. constructor arguments, as noted below.
  103. </para>
  104. </listitem>
  105. </varlistentry>
  106. <varlistentry>
  107. <term>formatterNamespace (optional)</term>
  108. <listitem>
  109. <para>
  110. The class prefix/namespace to use when constructing the final log formatter
  111. classname. By default, if this is not provided, "Zend_Log_Formatter" is assumed;
  112. however, you can pass your own namespace if you are using a custom log formatter.
  113. </para>
  114. </listitem>
  115. </varlistentry>
  116. <varlistentry>
  117. <term>filterName (optional)</term>
  118. <listitem>
  119. <para>
  120. The "short" name of a filter to use with the given log writer; the name of the
  121. filter minus the leading class prefix/namespace. See the "filterNamespace" entry
  122. below for more details. Examples: "Message", "Priority".
  123. </para>
  124. </listitem>
  125. </varlistentry>
  126. <varlistentry>
  127. <term>filterParams (optional)</term>
  128. <listitem>
  129. <para>
  130. An associative array of parameters to use when instantiating the log filter.
  131. Each log filter's <methodname>factory()</methodname> method will map these to
  132. constructor arguments, as noted below.
  133. </para>
  134. </listitem>
  135. </varlistentry>
  136. <varlistentry>
  137. <term>filterNamespace (optional)</term>
  138. <listitem>
  139. <para>
  140. The class prefix/namespace to use when constructing the final log filter
  141. classname. By default, if this is not provided, "Zend_Log_Filter" is assumed;
  142. however, you can pass your own namespace if you are using a custom log filter.
  143. </para>
  144. </listitem>
  145. </varlistentry>
  146. </variablelist>
  147. <para>
  148. Each writer and each filter has specific options.
  149. </para>
  150. <sect2 id="zend.log.factory.writer-options">
  151. <title>Writer Options</title>
  152. <sect3 id="zend.log.factory.writer-options.db">
  153. <title>Zend_Log_Writer_Db Options</title>
  154. <variablelist>
  155. <varlistentry>
  156. <term>db</term>
  157. <listitem>
  158. <para>
  159. A <classname>Zend_Db_Adapter</classname> instance.
  160. </para>
  161. </listitem>
  162. </varlistentry>
  163. <varlistentry>
  164. <term>table</term>
  165. <listitem>
  166. <para>
  167. The name of the table in the RDBMS that will contain log entries.
  168. </para>
  169. </listitem>
  170. </varlistentry>
  171. <varlistentry>
  172. <term>columnMap</term>
  173. <listitem>
  174. <para>
  175. An associative array mapping database table column names to log event
  176. fields.
  177. </para>
  178. </listitem>
  179. </varlistentry>
  180. </variablelist>
  181. </sect3>
  182. <sect3 id="zend.log.factory.writer-options.firebug">
  183. <title>Zend_Log_Writer_Firebug Options</title>
  184. <para>
  185. This log writer takes no options; any provided will be ignored.
  186. </para>
  187. </sect3>
  188. <sect3 id="zend.log.factory.writer-options.mail">
  189. <title>Zend_Log_Writer_Mail Options</title>
  190. <table id="zend.log.factory.writer-options.mail.table">
  191. <title>Zend_Log_Writer_Mail Options</title>
  192. <tgroup cols="4">
  193. <thead>
  194. <row>
  195. <entry>Option</entry>
  196. <entry>Data Type</entry>
  197. <entry>Default Value</entry>
  198. <entry>Description</entry>
  199. </row>
  200. </thead>
  201. <tbody>
  202. <row>
  203. <entry><emphasis>mail</emphasis></entry>
  204. <entry><type>String</type></entry>
  205. <entry><classname>Zend_Mail</classname></entry>
  206. <entry>
  207. An <classname>Zend_Mail</classname> instance
  208. </entry>
  209. </row>
  210. <row>
  211. <entry><emphasis>charset</emphasis></entry>
  212. <entry><type>String</type></entry>
  213. <entry>iso-8859-1</entry>
  214. <entry>
  215. Charset of the mail
  216. </entry>
  217. </row>
  218. <row>
  219. <entry><emphasis>from</emphasis></entry>
  220. <entry><type>String</type> or <type>Array</type></entry>
  221. <entry><constant>NULL</constant></entry>
  222. <entry>
  223. Sender of the mail
  224. The parameters for <type>Array</type> type are :
  225. <itemizedlist>
  226. <listitem>
  227. <para>
  228. <property>email</property> : address of sender
  229. </para>
  230. </listitem>
  231. <listitem>
  232. <para>
  233. <property>name</property> : name of sender
  234. </para>
  235. </listitem>
  236. </itemizedlist>
  237. </entry>
  238. </row>
  239. <row>
  240. <entry><emphasis>to</emphasis></entry>
  241. <entry><type>String</type> or <type>Array</type></entry>
  242. <entry><constant>NULL</constant></entry>
  243. <entry>
  244. Recipient(s) of the mail
  245. </entry>
  246. </row>
  247. <row>
  248. <entry><emphasis>cc</emphasis></entry>
  249. <entry><type>String</type> or <type>Array</type></entry>
  250. <entry><constant>NULL</constant></entry>
  251. <entry>
  252. Carbon copy recipient(s) of the mail
  253. </entry>
  254. </row>
  255. <row>
  256. <entry><emphasis>bcc</emphasis></entry>
  257. <entry><type>String</type> or <type>Array</type></entry>
  258. <entry><constant>NULL</constant></entry>
  259. <entry>
  260. Blind carbon copy recipient(s) of the mail
  261. </entry>
  262. </row>
  263. <row>
  264. <entry><emphasis>subject</emphasis></entry>
  265. <entry><type>String</type></entry>
  266. <entry><constant>NULL</constant></entry>
  267. <entry>
  268. Subject of the mail
  269. </entry>
  270. </row>
  271. <row>
  272. <entry><emphasis>subjectPrependText</emphasis></entry>
  273. <entry><type>String</type></entry>
  274. <entry><constant>NULL</constant></entry>
  275. <entry>
  276. A summary of number of errors per priority is appended to the
  277. subject of the mail
  278. </entry>
  279. </row>
  280. <row>
  281. <entry><emphasis>layout</emphasis></entry>
  282. <entry><type>String</type></entry>
  283. <entry><constant>NULL</constant></entry>
  284. <entry>
  285. An <classname>Zend_Layout</classname> instance
  286. </entry>
  287. </row>
  288. <row>
  289. <entry><emphasis>layoutOptions</emphasis></entry>
  290. <entry><type>Array</type></entry>
  291. <entry><constant>NULL</constant></entry>
  292. <entry>
  293. See the section <xref linkend="zend.layout.options" />
  294. </entry>
  295. </row>
  296. <row>
  297. <entry><emphasis>layoutFormatter</emphasis></entry>
  298. <entry><type>String</type></entry>
  299. <entry><constant>NULL</constant></entry>
  300. <entry>
  301. An <classname>Zend_Log_Formatter_Interface</classname> instance
  302. </entry>
  303. </row>
  304. </tbody>
  305. </tgroup>
  306. </table>
  307. </sect3>
  308. <sect3 id="zend.log.factory.writer-options.mock">
  309. <title>Zend_Log_Writer_Mock Options</title>
  310. <para>
  311. This log writer takes no options; any provided will be ignored.
  312. </para>
  313. </sect3>
  314. <sect3 id="zend.log.factory.writer-options.null">
  315. <title>Zend_Log_Writer_Null Options</title>
  316. <para>
  317. This log writer takes no options; any provided will be ignored.
  318. </para>
  319. </sect3>
  320. <sect3 id="zend.log.factory.writer-options.stream">
  321. <title>Zend_Log_Writer_Stream Options</title>
  322. <variablelist>
  323. <varlistentry>
  324. <term>stream|url</term>
  325. <listitem>
  326. <para>
  327. A valid <acronym>PHP</acronym> stream identifier to which to log.
  328. </para>
  329. </listitem>
  330. </varlistentry>
  331. <varlistentry>
  332. <term>mode</term>
  333. <listitem>
  334. <para>
  335. The I/O mode with which to log; defaults to "a", for "append".
  336. </para>
  337. </listitem>
  338. </varlistentry>
  339. </variablelist>
  340. </sect3>
  341. <sect3 id="zend.log.factory.writer-options.syslog">
  342. <title>Zend_Log_Writer_Syslog Options</title>
  343. <variablelist>
  344. <varlistentry>
  345. <term>application</term>
  346. <listitem>
  347. <para>
  348. Application name used by the syslog writer.
  349. </para>
  350. </listitem>
  351. </varlistentry>
  352. <varlistentry>
  353. <term>facility</term>
  354. <listitem>
  355. <para>
  356. Facility used by the syslog writer.
  357. </para>
  358. </listitem>
  359. </varlistentry>
  360. </variablelist>
  361. </sect3>
  362. <sect3 id="zend.log.factory.writer-options.zendmonitor">
  363. <title>Zend_Log_Writer_ZendMonitor Options</title>
  364. <para>
  365. This log writer takes no options; any provided will be ignored.
  366. </para>
  367. </sect3>
  368. </sect2>
  369. <sect2 id="zend.log.factory.filter-options">
  370. <title>Filter Options</title>
  371. <sect3 id="zend.log.factory.filter-options.message">
  372. <title>Zend_Log_Filter_Message Options</title>
  373. <variablelist>
  374. <varlistentry>
  375. <term>regexp</term>
  376. <listitem>
  377. <para>
  378. Regular expression that must be matched in order to log a message.
  379. </para>
  380. </listitem>
  381. </varlistentry>
  382. </variablelist>
  383. </sect3>
  384. <sect3 id="zend.log.factory.filter-options.priority">
  385. <title>Zend_Log_Filter_Priority Options</title>
  386. <variablelist>
  387. <varlistentry>
  388. <term>priority</term>
  389. <listitem>
  390. <para>
  391. The maximum priority level by which messages will be logged.
  392. </para>
  393. </listitem>
  394. </varlistentry>
  395. <varlistentry>
  396. <term>operator</term>
  397. <listitem>
  398. <para>
  399. The comparison operator by which to do priority comparisons; defaults to
  400. "&lt;=".
  401. </para>
  402. </listitem>
  403. </varlistentry>
  404. </variablelist>
  405. </sect3>
  406. <sect3 id="zend.log.factory.filter-options.suppress">
  407. <title>Zend_Log_Filter_Suppress Options</title>
  408. <para>
  409. This log filter takes no options; any provided will be ignored.
  410. </para>
  411. </sect3>
  412. </sect2>
  413. <sect2 id="zend.log.factory.custom">
  414. <title>Creating Configurable Writers and Filters</title>
  415. <para>
  416. If you find yourself needing to write your own log writers and/or filters, you can make
  417. them compatible with <methodname>Zend_Log::factory()</methodname> very easily.
  418. </para>
  419. <para>
  420. At the minimum, you need to implement
  421. <interfacename>Zend_Log_FactoryInterface</interfacename>, which expects a static
  422. <methodname>factory()</methodname> method that accepts a single argument,
  423. <varname>$config</varname>, which may be either an array or
  424. <classname>Zend_Config</classname> object. If your log writer extends
  425. <classname>Zend_Log_Writer_Abstract</classname>, or your log filter extends
  426. <classname>Zend_Log_Filter_Abstract</classname>, you will pick this up for free.
  427. </para>
  428. <para>
  429. Then, simply define mappings between the accepted configuration and any constructor
  430. arguments. As an example:
  431. </para>
  432. <programlisting language="php"><![CDATA[
  433. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
  434. {
  435. public function __construct($bar, $baz)
  436. {
  437. // ...
  438. }
  439. public static function factory($config)
  440. {
  441. if ($config instanceof Zend_Config) {
  442. $config = $config->toArray();
  443. }
  444. if (!is_array($config)) {
  445. throw new Exception(
  446. 'factory expects an array or Zend_Config instance'
  447. );
  448. }
  449. $default = array(
  450. 'bar' => null,
  451. 'baz' => null,
  452. );
  453. $config = array_merge($default, $config);
  454. return new self(
  455. $config['bar'],
  456. $config['baz']
  457. );
  458. }
  459. }
  460. ]]></programlisting>
  461. <para>
  462. Alternately, you could call appropriate setters after instantiation, but prior to
  463. returning the instance:
  464. </para>
  465. <programlisting language="php"><![CDATA[
  466. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
  467. {
  468. public function __construct($bar = null, $baz = null)
  469. {
  470. // ...
  471. }
  472. public function setBar($value)
  473. {
  474. // ...
  475. }
  476. public function setBaz($value)
  477. {
  478. // ...
  479. }
  480. public static function factory($config)
  481. {
  482. if ($config instanceof Zend_Config) {
  483. $config = $config->toArray();
  484. }
  485. if (!is_array($config)) {
  486. throw new Exception(
  487. 'factory expects an array or Zend_Config instance'
  488. );
  489. }
  490. $writer = new self();
  491. if (isset($config['bar'])) {
  492. $writer->setBar($config['bar']);
  493. }
  494. if (isset($config['baz'])) {
  495. $writer->setBaz($config['baz']);
  496. }
  497. return $writer;
  498. }
  499. }
  500. ]]></programlisting>
  501. </sect2>
  502. </sect1>