Zend_Log-Factory.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19550 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.log.factory">
  5. <title>Die Factory verwenden um ein Log zu erstellen</title>
  6. <para>
  7. Zusätzlich zur direkten Instanzierung kann man auch die statische
  8. <methodname>factory()</methodname> Methode verwenden um eine Log Instanz zu initiieren,
  9. und auch um angehängte Writer und deren Filter zu konfigurieren. Bei Verwendung der
  10. Factory kann man keine oder mehrere Writer anhängen. Die Konfiguration kann entweder als
  11. Array oder Instanz von <classname>Zend_Config</classname> übergeben werden.
  12. </para>
  13. <para>
  14. Als Beispiel:
  15. </para>
  16. <programlisting language="php"><![CDATA[
  17. $logger = Zend_Log::factory(array(
  18. array(
  19. 'writerName' => 'Stream',
  20. 'writerParams' => array(
  21. 'stream' => '/tmp/zend.log',
  22. ),
  23. 'filterName' => 'Priority',
  24. 'filterParams' => array(
  25. 'priority' => Zend_Log::WARN,
  26. ),
  27. ),
  28. array(
  29. 'writerName' => 'Firebug',
  30. 'filterName' => 'Priority',
  31. 'filterParams' => array(
  32. 'priority' => Zend_Log::INFO,
  33. ),
  34. ),
  35. ));
  36. ]]></programlisting>
  37. <para>
  38. Das obige instanziert einen Logger mit zwei Writern, einen für das schreiben einer lokalen
  39. Daten, und einen anderen für das Senden der Daten zu Firebug. Jeder hat einen angehängten
  40. Filter für Prioritäten, mit unterschiedlichen maximalen Prioritäten.
  41. </para>
  42. <para>
  43. Jeder Writer kann mit den folgenden Schlüsseln definiert werden:
  44. </para>
  45. <variablelist>
  46. <varlistentry>
  47. <term>writerName (required)</term>
  48. <listitem>
  49. <para>
  50. Der "kurze" Name eines Log Writers; der Name des Log Writers ohne den führenden
  51. Klassenpräfix/Namespace. Siehe auch den "writerNamespace" Eintrag weiter unten
  52. für weitere Details. Beispiele: "Mock", "Stream", "Firebug".
  53. </para>
  54. </listitem>
  55. </varlistentry>
  56. <varlistentry>
  57. <term>writerParams (optional)</term>
  58. <listitem>
  59. <para>
  60. Ein assoziatives Array von Parametern das verwendet wird wenn der Log Writer
  61. instanziert wird. Jede <methodname>factory()</methodname> Methode eines Log
  62. Writers führt diese mit den Argumenten des Contructors zusammen, wie anbei
  63. erwähnt.
  64. </para>
  65. </listitem>
  66. </varlistentry>
  67. <varlistentry>
  68. <term>writerNamespace (optional)</term>
  69. <listitem>
  70. <para>
  71. Der Klassenpräfix/Namespace der verwendet wird wenn der endgültige Klassenname
  72. des Log Writers erstellt wird. Wird er nicht angegeben, dann wird standardmäßig
  73. "Zend_Log_Writer" angenommen; trotzdem kann man einen eigenen Namespace
  74. übergeben, wenn man einen eigenen Log Writer verwendet.
  75. </para>
  76. </listitem>
  77. </varlistentry>
  78. <varlistentry>
  79. <term>filterName (optional)</term>
  80. <listitem>
  81. <para>
  82. Der "kurze" Name des Filters der mit dem angegebenen Log Writer verwendet werden
  83. soll; der Name des Filters ohne den führenden Klassenpräfix/Namespace. Siehe
  84. auch den "filterNamespace" Eintrag weiter unter für weitere Details. Beispiele:
  85. "Message", "Priority".
  86. </para>
  87. </listitem>
  88. </varlistentry>
  89. <varlistentry>
  90. <term>filterParams (optional)</term>
  91. <listitem>
  92. <para>
  93. An associative array of parameters to use when instantiating the log filter.
  94. Each log filter'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>filterNamespace (optional)</term>
  101. <listitem>
  102. <para>
  103. The class prefix/namespace to use when constructing the final log filter
  104. classname. By default, if this is not provided, "Zend_Log_Filter" is assumed;
  105. however, you can pass your own namespace if you are using a custom log filter.
  106. </para>
  107. </listitem>
  108. </varlistentry>
  109. </variablelist>
  110. <para>
  111. Each writer and each filter has specific options.
  112. </para>
  113. <sect2 id="zend.log.factory.writer-options">
  114. <title>Writer Options</title>
  115. <sect3 id="zend.log.factory.writer-options.db">
  116. <title>Zend_Log_Writer_Db Options</title>
  117. <variablelist>
  118. <varlistentry>
  119. <term>db</term>
  120. <listitem>
  121. <para>
  122. A <classname>Zend_Db_Adapter</classname> instance.
  123. </para>
  124. </listitem>
  125. </varlistentry>
  126. <varlistentry>
  127. <term>table</term>
  128. <listitem>
  129. <para>
  130. The name of the table in the RDBMS that will contain log entries.
  131. </para>
  132. </listitem>
  133. </varlistentry>
  134. <varlistentry>
  135. <term>columnMap</term>
  136. <listitem>
  137. <para>
  138. An associative array mapping database table column names to log event
  139. fields.
  140. </para>
  141. </listitem>
  142. </varlistentry>
  143. </variablelist>
  144. </sect3>
  145. <sect3 id="zend.log.factory.writer-options.firebug">
  146. <title>Zend_Log_Writer_Firebug Options</title>
  147. <para>
  148. This log writer takes no options; any provided will be ignored.
  149. </para>
  150. </sect3>
  151. <sect3 id="zend.log.factory.writer-options.mail">
  152. <title>Zend_Log_Writer_Mail Options</title>
  153. <para>
  154. <classname>Zend_Log_Writer_Mail</classname> currently (as of 1.10) does not
  155. implement a factory, and will raise an exception if you attempt to instantiate it
  156. via <methodname>Zend_Log::factory()</methodname>.
  157. </para>
  158. </sect3>
  159. <sect3 id="zend.log.factory.writer-options.mock">
  160. <title>Zend_Log_Writer_Mock Options</title>
  161. <para>
  162. This log writer takes no options; any provided will be ignored.
  163. </para>
  164. </sect3>
  165. <sect3 id="zend.log.factory.writer-options.null">
  166. <title>Zend_Log_Writer_Null Options</title>
  167. <para>
  168. This log writer takes no options; any provided will be ignored.
  169. </para>
  170. </sect3>
  171. <sect3 id="zend.log.factory.writer-options.stream">
  172. <title>Zend_Log_Writer_Stream Options</title>
  173. <variablelist>
  174. <varlistentry>
  175. <term>stream|url</term>
  176. <listitem>
  177. <para>
  178. A valid PHP stream identifier to which to log.
  179. </para>
  180. </listitem>
  181. </varlistentry>
  182. <varlistentry>
  183. <term>mode</term>
  184. <listitem>
  185. <para>
  186. The I/O mode with which to log; defaults to "a", for "append".
  187. </para>
  188. </listitem>
  189. </varlistentry>
  190. </variablelist>
  191. </sect3>
  192. <sect3 id="zend.log.factory.writer-options.syslog">
  193. <title>Zend_Log_Writer_Syslog Options</title>
  194. <variablelist>
  195. <varlistentry>
  196. <term>application</term>
  197. <listitem>
  198. <para>
  199. Application name used by the syslog writer.
  200. </para>
  201. </listitem>
  202. </varlistentry>
  203. <varlistentry>
  204. <term>facility</term>
  205. <listitem>
  206. <para>
  207. Facility used by the syslog writer.
  208. </para>
  209. </listitem>
  210. </varlistentry>
  211. </variablelist>
  212. </sect3>
  213. <sect3 id="zend.log.factory.writer-options.zendmonitor">
  214. <title>Zend_Log_Writer_ZendMonitor Options</title>
  215. <para>
  216. This log writer takes no options; any provided will be ignored.
  217. </para>
  218. </sect3>
  219. </sect2>
  220. <sect2 id="zend.log.factory.filter-options">
  221. <title>Filter Options</title>
  222. <sect3 id="zend.log.factory.filter-options.message">
  223. <title>Zend_Log_Filter_Message Options</title>
  224. <variablelist>
  225. <varlistentry>
  226. <term>regexp</term>
  227. <listitem>
  228. <para>
  229. Regular expression that must be matched in order to log a message.
  230. </para>
  231. </listitem>
  232. </varlistentry>
  233. </variablelist>
  234. </sect3>
  235. <sect3 id="zend.log.factory.filter-options.priority">
  236. <title>Zend_Log_Filter_Priority Options</title>
  237. <variablelist>
  238. <varlistentry>
  239. <term>priority</term>
  240. <listitem>
  241. <para>
  242. The maximum priority level by which messages will be logged.
  243. </para>
  244. </listitem>
  245. </varlistentry>
  246. <varlistentry>
  247. <term>operator</term>
  248. <listitem>
  249. <para>
  250. The comparison operator by which to do priority comparisons; defaults to
  251. "&lt;=".
  252. </para>
  253. </listitem>
  254. </varlistentry>
  255. </variablelist>
  256. </sect3>
  257. <sect3 id="zend.log.factory.filter-options.suppress">
  258. <title>Zend_Log_Writer_Suppress Options</title>
  259. <para>
  260. This log filter takes no options; any provided will be ignored.
  261. </para>
  262. </sect3>
  263. </sect2>
  264. <sect2 id="zend.log.factory.custom">
  265. <title>Creating Configurable Writers and Filters</title>
  266. <para>
  267. If you find yourself needing to write your own log writers and/or filters, you can make
  268. them compatible with <methodname>Zend_Log::factory()</methodname> very easily.
  269. </para>
  270. <para>
  271. At the minimum, you need to implement
  272. <interfacename>Zend_Log_FactoryInterface</interfacename>, which expects a static
  273. <methodname>factory()</methodname> method that accepts a single argument,
  274. <varname>$config</varname>, which may be either an array or
  275. <classname>Zend_Config</classname> object. If your log writer extends
  276. <classname>Zend_Log_Writer_Abstract</classname>, or your log filter extends
  277. <classname>Zend_Log_Filter_Abstract</classname>, you will pick this up for free.
  278. </para>
  279. <para>
  280. Then, simply define mappings between the accepted configuration and any constructor
  281. arguments. As an example:
  282. </para>
  283. <programlisting language="php"><![CDATA[
  284. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
  285. {
  286. public function __construct($bar, $baz)
  287. {
  288. // ...
  289. }
  290. public static function factory($config)
  291. {
  292. if ($config instanceof Zend_Config) {
  293. $config = $config->toArray();
  294. }
  295. if (!is_array($config)) {
  296. throw new Exception(
  297. 'factory expects an array or Zend_Config instance'
  298. );
  299. }
  300. $default = array(
  301. 'bar' => null,
  302. 'baz' => null,
  303. );
  304. $config = array_merge($default, $config);
  305. return new self(
  306. $config['bar'],
  307. $config['baz']
  308. );
  309. }
  310. }
  311. ]]></programlisting>
  312. <para>
  313. Alternately, you could call appropriate setters after instantiation, but prior to
  314. returning the instance:
  315. </para>
  316. <programlisting language="php"><![CDATA[
  317. class My_Log_Writer_Foo extends Zend_Log_Writer_Abstract
  318. {
  319. public function __construct($bar = null, $baz = null)
  320. {
  321. // ...
  322. }
  323. public function setBar($value)
  324. {
  325. // ...
  326. }
  327. public function setBaz($value)
  328. {
  329. // ...
  330. }
  331. public static function factory($config)
  332. {
  333. if ($config instanceof Zend_Config) {
  334. $config = $config->toArray();
  335. }
  336. if (!is_array($config)) {
  337. throw new Exception(
  338. 'factory expects an array or Zend_Config instance'
  339. );
  340. }
  341. $writer = new self();
  342. if (isset($config['bar'])) {
  343. $writer->setBar($config['bar']);
  344. }
  345. if (isset($config['baz'])) {
  346. $writer->setBaz($config['baz']);
  347. }
  348. return $writer;
  349. }
  350. }
  351. ]]></programlisting>
  352. </sect2>
  353. </sect1>