This repository has been archived by the owner on Nov 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
log.xml
402 lines (296 loc) · 14.1 KB
/
log.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % BOOK_ENTITIES SYSTEM "../../Developer_Manual.ent">
%BOOK_ENTITIES;
]>
<section id="chap-Joomla_Platform_Manual-Log">
<title>The Log Package</title>
<section>
<title>Introduction</title>
<para>The Joomla Platform includes a Log package that allows for configurable, hook-driven logging to a variety of
formats.</para>
<para>The classes included in the Log package are JLog, JLogEntry, LogException, JLogger as well as the classes
JLoggerDatabase, JLoggerEcho, JLoggerEmail, JLoggerFormattedtext, JLoggerMessagequeue, JLoggerSyslog and JLoggerW3c which support formatting
and storage. Of all these classes, you will generally only use JLog in your projects.</para>
<para>Logging is a two-step process.</para>
<para>First you must add the add loggers to listen for log messages. Any number of loggers can be configured to listen for log
messages based on a priority and a category. For example, you can configure all log messages to be logged to the database, but
also set just errors to be logged to a file. To do this, you use the JLog::addLogger method.</para>
<para>After at least one logger is configured, you can then add messages using the JLog::addLogEntry method where you can
specify a message, and optionally a priority (integer), category (string) and date.</para>
<para></para>
</section>
<section>
<title>Logging priority</title>
<para>Before we look at any logging examples, we need to understand what the priority is. The priority is an integer mask and
is set using one or more predefined constants in the JLog class. These are:</para>
<simplelist>
<member>JLog::EMERGENCY</member>
<member>JLog::ALERT</member>
<member>JLog::CRITICAL</member>
<member>JLog::ERROR</member>
<member>JLog::WARNING</member>
<member>JLog::NOTICE</member>
<member>JLog::INFO</member>
<member>JLog::DEBUG</member>
</simplelist>
<para>A final constant, JLog::ALL is also available which corresponds to hex FFFF (16 bits). The other constants reserve the
first eight bits for system use. This allows the developer the last eight bits, hex 100 to 8000, for custom use if desired. As
the values are for masking, they can be mixed using any of the bitwise operators for <emphasis>and</emphasis>,
<emphasis>or</emphasis>, <emphasis>not</emphasis> and <emphasis>xor</emphasis>.</para>
<para>By default, loggers are added to listen for JLog::ALL priorities and log entries are added using the JLog::INFO
mask.</para>
</section>
<section>
<title>Logging to files (formattedtext)</title>
<para>A very typical example of logging is the ability to log to a file, and this is the default handler for logging. To do
this, you need to import the JLog class (if your project is based on JApplication, such as in the Joomla CMS, or JCli, the
JLog class will already be loaded), add the logger and then you can add log messages.</para>
<example>
<title>Simple log example</title>
<programlisting>// Include the JLog class.
jimport('joomla.log.log');
// Initialise a basic logger with no options (once only).
JLog::addLogger(array());
// Add a message.
JLog::add('Logged');</programlisting>
</example>
<para>As no logger has been specified in the JLog::addLogger call, the "formattedtext" logger will be used. This will log the
message to a file called "error.php" in the log folder specified by the "log_path" configuration variable (in the Joomla CMS,
the default is /logs/). It will look something like this:</para>
<example>
<title>Example of what a log file may look like</title>
<programlisting>#<?php die('Forbidden.'); ?>
#Date: 2011-06-17 02:56:21 UTC
#Software: Joomla Platform 11.1 Stable [ Ember ] 01-Jun-2011 06:00 GMT
#Fields: datetime priority category message
2011-06-17T03:06:44+00:00 INFO - Logged
</programlisting>
</example>
<para>The file is tab-delimited and the default columns are the timestamp, the text representation of the priority, the
category and finally the message. The category is empty (a dash) because we didn't supply it.</para>
<para>To log a different priority, you can use code like:</para>
<example>
<title>Logging with a priority</title>
<programlisting>JLog::add('Logged 3', JLog::WARNING, 'Test');</programlisting>
</example>
<para>The log file will now look similar to the following:</para>
<example>
<title>Log file with priorities</title>
<programlisting>2011-06-17T03:06:44+00:00 INFO - Logged
2011-06-17T03:52:08+00:00 WARNING - Logged 2
2011-06-17T03:57:03+00:00 WARNING test Logged 3</programlisting>
</example>
<section>
<title>Additional options with formattedtext</title>
<para>When adding the "formattedtext" logger, the following options are available to supply in the array you pass to
JLog::addLogger.</para>
<simplelist>
<member><emphasis role="bold">text_file</emphasis> - allows you to specify the name of the file to which messages are
logged.</member>
<member><emphasis role="bold">text_file_path</emphasis> - allows you to specify the folder path to the file to which
messages are logged.</member>
<member><emphasis role="bold">text_file_no_php</emphasis> - if set, the PHP die statement will not be added to the file
line of the file.</member>
<member><emphasis role="bold">text_entry_format</emphasis> - allows you to change the format of the entire line of the log
message in the file.</member>
</simplelist>
</section>
<section>
<title>Changing the name of the log file</title>
<para>Given the options outlined in the previous section, you can change the name of the file to which you are logging when
you add the logger, like this:</para>
<example>
<title>Changing the name of the log file</title>
<programlisting>// Log to a specific text file.
JLog::addLogger(
array(
'text_file' => 'mylogs.php'
)
);</programlisting>
</example>
</section>
<section>
<title>Logging different priorities to different files</title>
<para>You can log different types of messages to different files by adding multiple loggers that bind different log
priorities to different files. For example, the following code will log all messages except errors to one file, and error
messages to a separate file.</para>
<example>
<title>Logging different priorities to different files</title>
<programlisting>// Log all message except errors to mylogs.php.
JLog::addLogger(
array(
'text_file' => 'mylogs.php'
),
JLog::ALL ^ JLog::ERROR
);
// Log errors to myerrors.php.
JLog::addLogger(
array(
'text_file' => 'myerrors.php'
),
JLog::ERROR
);</programlisting>
</example>
</section>
<section>
<title>Logging specific categories to a file</title>
<para>If you are wanting to collect errors for your specific project, class or extension, you can also bind logging to
different categories. For example, the following code could be used in a Joomla extension to just collect errors relating to
it.</para>
<example>
<title>Setting up a logger for a category</title>
<programlisting>// Log my extension errors only.
JLog::addLogger(
array(
'text_file' => 'com_hello.errors.php'
),
JLog::ERROR,
'com_hello'
);</programlisting>
</example>
<para>To log messages to that logger, you would use something similar to the following code:</para>
<example>
<title>Logging to a category</title>
<programlisting>JLog::add('Forgot to say goodbye', JLog::ERROR, 'com_hello');</programlisting>
</example>
<para>It is important to note that other loggers, added beyond your control, may also pick up this message.</para>
</section>
<section>
<title>Splitting up logs by date</title>
<para>Log files can, potentially, get very long over time. A convenient solution to this is to roll logs into different
files based on a period of time - an hour, a day, a month or even a year. To do this, you just need to add the date to the
file name of the log file. The following example shows you how to do this on a daily basis.</para>
<example>
<title>Splitting log files by date</title>
<programlisting>// Get the date.
$date = JFactory::getDate()->format('Y-m-d');
// Add the logger.
JLog::addLogger(
array(
'text_file' => 'com_hello.'.$date.'.php'
)
);</programlisting>
</example>
</section>
<section>
<title>Changing the format of the log message</title>
<para>When you adding a log message, it is written to the file in a default format in the form:</para>
<example>
<title>Default log file format</title>
<programlisting>{DATETIME} {PRIORITY} {CATEGORY} {MESSAGE}</programlisting>
</example>
<para>Each field is written in upper case, wrapped in curly braces and separated by tabs. There are a number of other fields
that are automatically defined in the "formattedtext" logger that you can take advantage of automatically. These are:</para>
<simplelist>
<member>{CLIENTIP} - the IP address of the user.</member>
<member>{DATE} - the "Y-m-d" date component of the message datestamp.</member>
<member>{TIME} - the "H:i:s" time component of the message datestamp.</member>
</simplelist>
<para>To modify for the log format to add any or all of these fields, you can add the logger as shown in the following
code.</para>
<example>
<title>Customizing the log file format</title>
<programlisting>// Add the logger.
JLog::addLogger(
array(
'text_file' => 'com_hello.php',
'text_entry_format' => '{DATE} {TIME} {CLIENTIP} {CATEGORY} {MESSAGE}'
)
);</programlisting>
</example>
<para>As you can see, you can include or leave out any fields as you require to suit the needs of your project.</para>
<para>You can also add more fields but to do this you need to create and add a JLogEntry object directly. The following
example shows you how to do this.</para>
<example>
<title>Adding custom fields to a log entry</title>
<programlisting>// Add the logger.
JLog::addLogger(
array(
'text_file' => 'com_shop.sales.php',
'text_entry_format' => '{DATETIME} {PRICE} {QUANTITY} {MESSAGE}'
),
JLog::INFO,
'Shop'
);
$logEntry = new JLogEntry('T- Shirt', JLog::INFO, 'Shop');
$logEntry->price = '7.99';
$logEntry->quantity = 10;
JLog::add($logEntry);</programlisting>
</example>
<para>It is strongly recommended that, when using a custom format, you bind the log entries to a specific and unique
category, otherwise log files with different format (columns) could become mixed.</para>
</section>
</section>
<section>
<title>Logging to the database</title>
<para>The "database" logger allows you to log message to a database table. The create syntax for the default table is as
follows:</para>
<example>
<title>Example schema for a database logger</title>
<programlisting>CREATE TABLE `jos_log_entries` (
`priority` int(11) DEFAULT NULL,
`message` varchar(512) DEFAULT NULL,
`date` datetime DEFAULT NULL,
`category` varchar(255) DEFAULT NULL,
KEY `idx_category_date_priority` (`category`,`date`,`priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;</programlisting>
</example>
<para>To log messages using the "database" logger, you the following code as a guide.</para>
<example>
<title>Using a database logger</title>
<programlisting>// Add the logger.
JLog::addLogger(
array(
'logger' => 'database'
),
JLog::ALL,
'dblog'
);
// Add the message.
JLog::add('Database log', JLog::INFO, 'dblog');</programlisting>
</example>
<para>Notice that the example binds the logger to all message priorities, but only those with a category of "dblog".</para>
<para>If you are wanting to store additional information in the message, you can do so using a JSON encoded string. For
example:</para>
<example>
<title>Storing variable information in a log message</title>
<programlisting>// Assemble the log message.
$user = JFactory::getUser();
$log = array(
'userId' => $user->get('id'),
'userName' => $user->get('name'),
'stockId' => 'SKU123',
'price' => '7.49',
'quantity' => 10
);
// Add the message.
JLog::add(json_encode($log), JLog::INFO, 'dblog');</programlisting>
</example>
<para>This makes it possible to retrieve detailed information for display.</para>
</section>
<section>
<title>Logging to email</title>
<para>The "email" logger allows you to send a log message via email. </para>
<para>To log messages using the "email" logger, you the following code as a guide.</para>
<example>
<title>Using an email logger</title>
<programlisting>// Add the logger.
JLog::addLogger(
array(
'logger' => 'email',
'from' => 'admin@example.com',
'to' => 'support@example.com'
),
JLog::EMERGENCY,
'Alert'
);
// Add the message.
JLog::add('Database Connection Failed.', JLog::EMERGENCY, 'alert');</programlisting>
</example>
<para>Notice that the example binds the logger to the emergency message priority for a category of "alert".</para>
<para>The mail logger works with JMail. For the Joomla CMS, if the "from" and "to" values are omitted
from the addLogger call, JLog will retrieve the configuration "mailfrom" value and use it for both parameters.</para>
</section>
</section>