Zend_Log-Factory.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. <para>
  150. <classname>Zend_Log_Writer_Mail</classname> currently (as of 1.10) does not
  151. implement a factory, and will raise an exception if you attempt to instantiate it
  152. via <methodname>Zend_Log::factory()</methodname>.
  153. </para>
  154. </sect3>
  155. <sect3 id="zend.log.factory.writer-options.mock">
  156. <title>Zend_Log_Writer_Mock Options</title>
  157. <para>
  158. This log writer takes no options; any provided will be ignored.
  159. </para>
  160. </sect3>
  161. <sect3 id="zend.log.factory.writer-options.null">
  162. <title>Zend_Log_Writer_Null Options</title>
  163. <para>
  164. This log writer takes no options; any provided will be ignored.
  165. </para>
  166. </sect3>
  167. <sect3 id="zend.log.factory.writer-options.stream">
  168. <title>Zend_Log_Writer_Stream Options</title>
  169. <variablelist>
  170. <varlistentry>
  171. <term>stream|url</term>
  172. <listitem>
  173. <para>
  174. A valid PHP stream identifier to which to log.
  175. </para>
  176. </listitem>
  177. </varlistentry>
  178. <varlistentry>
  179. <term>mode</term>
  180. <listitem>
  181. <para>
  182. The I/O mode with which to log; defaults to "a", for "append".
  183. </para>
  184. </listitem>
  185. </varlistentry>
  186. </variablelist>
  187. </sect3>
  188. <sect3 id="zend.log.factory.writer-options.syslog">
  189. <title>Zend_Log_Writer_Syslog Options</title>
  190. <variablelist>
  191. <varlistentry>
  192. <term>application</term>
  193. <listitem>
  194. <para>
  195. Application name used by the syslog writer.
  196. </para>
  197. </listitem>
  198. </varlistentry>
  199. <varlistentry>
  200. <term>facility</term>
  201. <listitem>
  202. <para>
  203. Facility used by the syslog writer.
  204. </para>
  205. </listitem>
  206. </varlistentry>
  207. </variablelist>
  208. </sect3>
  209. <sect3 id="zend.log.factory.writer-options.zendmonitor">
  210. <title>Zend_Log_Writer_ZendMonitor Options</title>
  211. <para>
  212. This log writer takes no options; any provided will be ignored.
  213. </para>
  214. </sect3>
  215. </sect2>
  216. <sect2 id="zend.log.factory.filter-options">
  217. <title>Filter Options</title>
  218. <sect3 id="zend.log.factory.filter-options.message">
  219. <title>Zend_Log_Filter_Message Options</title>
  220. <variablelist>
  221. <varlistentry>
  222. <term>regexp</term>
  223. <listitem>
  224. <para>
  225. Regular expression that must be matched in order to log a message.
  226. </para>
  227. </listitem>
  228. </varlistentry>
  229. </variablelist>
  230. </sect3>
  231. <sect3 id="zend.log.factory.filter-options.priority">
  232. <title>Zend_Log_Filter_Priority Options</title>
  233. <variablelist>
  234. <varlistentry>
  235. <term>priority</term>
  236. <listitem>
  237. <para>
  238. The maximum priority level by which messages will be logged.
  239. </para>
  240. </listitem>
  241. </varlistentry>
  242. <varlistentry>
  243. <term>operator</term>
  244. <listitem>
  245. <para>
  246. The comparison operator by which to do priority comparisons; defaults to
  247. "&lt;=".
  248. </para>
  249. </listitem>
  250. </varlistentry>
  251. </variablelist>
  252. </sect3>
  253. <sect3 id="zend.log.factory.filter-options.suppress">
  254. <title>Zend_Log_Writer_Suppress Options</title>
  255. <para>
  256. This log filter takes no options; any provided will be ignored.
  257. </para>
  258. </sect3>
  259. </sect2>
  260. <sect2 id="zend.log.factory.custom">
  261. <title>Creating Configurable Writers and Filters</title>
  262. <para>
  263. If you find yourself needing to write your own log writers and/or filters, you can make
  264. them compatible with <methodname>Zend_Log::factory()</methodname> very easily.
  265. </para>
  266. <para>
  267. At the minimum, you need to implement
  268. <interfacename>Zend_Log_FactoryInterface</interfacename>, which expects a static
  269. <methodname>factory()</methodname> method that accepts a single argument,
  270. <varname>$config</varname>, which may be either an array or
  271. <classname>Zend_Config</classname> object. If your log writer extends
  272. <classname>Zend_Log_Writer_Abstract</classname>, or your log filter extends
  273. <classname>Zend_Log_Filter_Abstract</classname>, you will pick this up for free.
  274. </para>
  275. <para>
  276. Then, simply define mappings between the accepted configuration and any constructor
  277. arguments. As an example:
  278. </para>
  279. <programlisting language="php"><![CDATA[
  280. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
  281. {
  282. public function __construct($bar, $baz)
  283. {
  284. // ...
  285. }
  286. public static function factory($config)
  287. {
  288. if ($config instanceof Zend_Config) {
  289. $config = $config->toArray();
  290. }
  291. if (!is_array($config)) {
  292. throw new Exception(
  293. 'factory expects an array or Zend_Config instance'
  294. );
  295. }
  296. $default = array(
  297. 'bar' => null,
  298. 'baz' => null,
  299. );
  300. $config = array_merge($default, $config);
  301. return new self(
  302. $config['bar'],
  303. $config['baz']
  304. );
  305. }
  306. }
  307. ]]></programlisting>
  308. <para>
  309. Alternately, you could call appropriate setters after instantiation, but prior to
  310. returning the instance:
  311. </para>
  312. <programlisting language="php"><![CDATA[
  313. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
  314. {
  315. public function __construct($bar = null, $baz = null)
  316. {
  317. // ...
  318. }
  319. public function setBar($value)
  320. {
  321. // ...
  322. }
  323. public function setBaz($value)
  324. {
  325. // ...
  326. }
  327. public static function factory($config)
  328. {
  329. if ($config instanceof Zend_Config) {
  330. $config = $config->toArray();
  331. }
  332. if (!is_array($config)) {
  333. throw new Exception(
  334. 'factory expects an array or Zend_Config instance'
  335. );
  336. }
  337. $writer = new self();
  338. if (isset($config['bar'])) {
  339. $writer->setBar($config['bar']);
  340. }
  341. if (isset($config['baz'])) {
  342. $writer->setBaz($config['baz']);
  343. }
  344. return $writer;
  345. }
  346. }
  347. ]]></programlisting>
  348. </sect2>
  349. </sect1>