Zend_Log-Factory.xml 19 KB

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