Zend_Log-Factory.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. If you plan on only providing a single writer, you can use a slightly more compact syntax:
  43. </para>
  44. <programlisting language="php"><![CDATA[
  45. $logger = Zend_Log::factory(array(
  46. 'writerName' => 'Stream',
  47. 'writerParams' => array(
  48. 'stream' => '/tmp/zend.log',
  49. ),
  50. 'filterName' => 'Priority',
  51. 'filterParams' => array(
  52. 'priority' => Zend_Log::WARN,
  53. ),
  54. ));
  55. ]]></programlisting>
  56. <para>
  57. We recommend using embedded arrays/configuration, however, as this provides more flexibility
  58. later should you need to add log writers.
  59. </para>
  60. <para>
  61. Each writer can be defined with the following keys:
  62. </para>
  63. <variablelist>
  64. <varlistentry>
  65. <term>writerName (required)</term>
  66. <listitem>
  67. <para>
  68. The "short" name of a log writer; the name of the log writer minus the leading
  69. class prefix/namespace. See the "writerNamespace" entry below for more details.
  70. Examples: "Mock", "Stream", "Firebug".
  71. </para>
  72. </listitem>
  73. </varlistentry>
  74. <varlistentry>
  75. <term>writerParams (optional)</term>
  76. <listitem>
  77. <para>
  78. An associative array of parameters to use when instantiating the log writer.
  79. Each log writer's <methodname>factory()</methodname> method will map these to
  80. constructor arguments, as noted below.
  81. </para>
  82. </listitem>
  83. </varlistentry>
  84. <varlistentry>
  85. <term>writerNamespace (optional)</term>
  86. <listitem>
  87. <para>
  88. The class prefix/namespace to use when constructing the final log writer
  89. classname. By default, if this is not provided, "Zend_Log_Writer" is assumed;
  90. however, you can pass your own namespace if you are using a custom log writer.
  91. </para>
  92. </listitem>
  93. </varlistentry>
  94. <varlistentry>
  95. <term>filterName (optional)</term>
  96. <listitem>
  97. <para>
  98. The "short" name of a filter to use with the given log writer; the name of the
  99. filter minus the leading class prefix/namespace. See the "filterNamespace" entry
  100. below for more details. Examples: "Message", "Priority".
  101. </para>
  102. </listitem>
  103. </varlistentry>
  104. <varlistentry>
  105. <term>filterParams (optional)</term>
  106. <listitem>
  107. <para>
  108. An associative array of parameters to use when instantiating the log filter.
  109. Each log filter's <methodname>factory()</methodname> method will map these to
  110. constructor arguments, as noted below.
  111. </para>
  112. </listitem>
  113. </varlistentry>
  114. <varlistentry>
  115. <term>filterNamespace (optional)</term>
  116. <listitem>
  117. <para>
  118. The class prefix/namespace to use when constructing the final log filter
  119. classname. By default, if this is not provided, "Zend_Log_Filter" is assumed;
  120. however, you can pass your own namespace if you are using a custom log filter.
  121. </para>
  122. </listitem>
  123. </varlistentry>
  124. </variablelist>
  125. <para>
  126. Each writer and each filter has specific options.
  127. </para>
  128. <sect2 id="zend.log.factory.writer-options">
  129. <title>Writer Options</title>
  130. <sect3 id="zend.log.factory.writer-options.db">
  131. <title>Zend_Log_Writer_Db Options</title>
  132. <variablelist>
  133. <varlistentry>
  134. <term>db</term>
  135. <listitem>
  136. <para>
  137. A <classname>Zend_Db_Adapter</classname> instance.
  138. </para>
  139. </listitem>
  140. </varlistentry>
  141. <varlistentry>
  142. <term>table</term>
  143. <listitem>
  144. <para>
  145. The name of the table in the RDBMS that will contain log entries.
  146. </para>
  147. </listitem>
  148. </varlistentry>
  149. <varlistentry>
  150. <term>columnMap</term>
  151. <listitem>
  152. <para>
  153. An associative array mapping database table column names to log event
  154. fields.
  155. </para>
  156. </listitem>
  157. </varlistentry>
  158. </variablelist>
  159. </sect3>
  160. <sect3 id="zend.log.factory.writer-options.firebug">
  161. <title>Zend_Log_Writer_Firebug Options</title>
  162. <para>
  163. This log writer takes no options; any provided will be ignored.
  164. </para>
  165. </sect3>
  166. <sect3 id="zend.log.factory.writer-options.mail">
  167. <title>Zend_Log_Writer_Mail Options</title>
  168. <para>
  169. <classname>Zend_Log_Writer_Mail</classname> currently (as of 1.10) does not
  170. implement a factory, and will raise an exception if you attempt to instantiate it
  171. via <methodname>Zend_Log::factory()</methodname>.
  172. </para>
  173. </sect3>
  174. <sect3 id="zend.log.factory.writer-options.mock">
  175. <title>Zend_Log_Writer_Mock Options</title>
  176. <para>
  177. This log writer takes no options; any provided will be ignored.
  178. </para>
  179. </sect3>
  180. <sect3 id="zend.log.factory.writer-options.null">
  181. <title>Zend_Log_Writer_Null 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.stream">
  187. <title>Zend_Log_Writer_Stream Options</title>
  188. <variablelist>
  189. <varlistentry>
  190. <term>stream|url</term>
  191. <listitem>
  192. <para>
  193. A valid PHP stream identifier to which to log.
  194. </para>
  195. </listitem>
  196. </varlistentry>
  197. <varlistentry>
  198. <term>mode</term>
  199. <listitem>
  200. <para>
  201. The I/O mode with which to log; defaults to "a", for "append".
  202. </para>
  203. </listitem>
  204. </varlistentry>
  205. </variablelist>
  206. </sect3>
  207. <sect3 id="zend.log.factory.writer-options.syslog">
  208. <title>Zend_Log_Writer_Syslog Options</title>
  209. <variablelist>
  210. <varlistentry>
  211. <term>application</term>
  212. <listitem>
  213. <para>
  214. Application name used by the syslog writer.
  215. </para>
  216. </listitem>
  217. </varlistentry>
  218. <varlistentry>
  219. <term>facility</term>
  220. <listitem>
  221. <para>
  222. Facility used by the syslog writer.
  223. </para>
  224. </listitem>
  225. </varlistentry>
  226. </variablelist>
  227. </sect3>
  228. <sect3 id="zend.log.factory.writer-options.zendmonitor">
  229. <title>Zend_Log_Writer_ZendMonitor Options</title>
  230. <para>
  231. This log writer takes no options; any provided will be ignored.
  232. </para>
  233. </sect3>
  234. </sect2>
  235. <sect2 id="zend.log.factory.filter-options">
  236. <title>Filter Options</title>
  237. <sect3 id="zend.log.factory.filter-options.message">
  238. <title>Zend_Log_Filter_Message Options</title>
  239. <variablelist>
  240. <varlistentry>
  241. <term>regexp</term>
  242. <listitem>
  243. <para>
  244. Regular expression that must be matched in order to log a message.
  245. </para>
  246. </listitem>
  247. </varlistentry>
  248. </variablelist>
  249. </sect3>
  250. <sect3 id="zend.log.factory.filter-options.priority">
  251. <title>Zend_Log_Filter_Priority Options</title>
  252. <variablelist>
  253. <varlistentry>
  254. <term>priority</term>
  255. <listitem>
  256. <para>
  257. The maximum priority level by which messages will be logged.
  258. </para>
  259. </listitem>
  260. </varlistentry>
  261. <varlistentry>
  262. <term>operator</term>
  263. <listitem>
  264. <para>
  265. The comparison operator by which to do priority comparisons; defaults to
  266. "&lt;=".
  267. </para>
  268. </listitem>
  269. </varlistentry>
  270. </variablelist>
  271. </sect3>
  272. <sect3 id="zend.log.factory.filter-options.suppress">
  273. <title>Zend_Log_Writer_Suppress Options</title>
  274. <para>
  275. This log filter takes no options; any provided will be ignored.
  276. </para>
  277. </sect3>
  278. </sect2>
  279. <sect2 id="zend.log.factory.custom">
  280. <title>Creating Configurable Writers and Filters</title>
  281. <para>
  282. If you find yourself needing to write your own log writers and/or filters, you can make
  283. them compatible with <methodname>Zend_Log::factory()</methodname> very easily.
  284. </para>
  285. <para>
  286. At the minimum, you need to implement
  287. <interfacename>Zend_Log_FactoryInterface</interfacename>, which expects a static
  288. <methodname>factory()</methodname> method that accepts a single argument,
  289. <varname>$config</varname>, which may be either an array or
  290. <classname>Zend_Config</classname> object. If your log writer extends
  291. <classname>Zend_Log_Writer_Abstract</classname>, or your log filter extends
  292. <classname>Zend_Log_Filter_Abstract</classname>, you will pick this up for free.
  293. </para>
  294. <para>
  295. Then, simply define mappings between the accepted configuration and any constructor
  296. arguments. As an example:
  297. </para>
  298. <programlisting language="php"><![CDATA[
  299. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
  300. {
  301. public function __construct($bar, $baz)
  302. {
  303. // ...
  304. }
  305. public static function factory($config)
  306. {
  307. if ($config instanceof Zend_Config) {
  308. $config = $config->toArray();
  309. }
  310. if (!is_array($config)) {
  311. throw new Exception(
  312. 'factory expects an array or Zend_Config instance'
  313. );
  314. }
  315. $default = array(
  316. 'bar' => null,
  317. 'baz' => null,
  318. );
  319. $config = array_merge($default, $config);
  320. return new self(
  321. $config['bar'],
  322. $config['baz']
  323. );
  324. }
  325. }
  326. ]]></programlisting>
  327. <para>
  328. Alternately, you could call appropriate setters after instantiation, but prior to
  329. returning the instance:
  330. </para>
  331. <programlisting language="php"><![CDATA[
  332. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
  333. {
  334. public function __construct($bar = null, $baz = null)
  335. {
  336. // ...
  337. }
  338. public function setBar($value)
  339. {
  340. // ...
  341. }
  342. public function setBaz($value)
  343. {
  344. // ...
  345. }
  346. public static function factory($config)
  347. {
  348. if ($config instanceof Zend_Config) {
  349. $config = $config->toArray();
  350. }
  351. if (!is_array($config)) {
  352. throw new Exception(
  353. 'factory expects an array or Zend_Config instance'
  354. );
  355. }
  356. $writer = new self();
  357. if (isset($config['bar'])) {
  358. $writer->setBar($config['bar']);
  359. }
  360. if (isset($config['baz'])) {
  361. $writer->setBaz($config['baz']);
  362. }
  363. return $writer;
  364. }
  365. }
  366. ]]></programlisting>
  367. </sect2>
  368. </sect1>