Zend_Log-Factory.xml 20 KB

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