Zend_Log-Factory.xml 20 KB

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