Repository navigation
Expand file tree
/
Copy pathlog.module
More file actions
304 lines (241 loc) · 8.08 KB
/
Copy pathlog.module
File metadata and controls
304 lines (241 loc) · 8.08 KB
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
<?php
function log_menu() {
$items = array();
$items['admin/settings/log'] = array(
'title' => t('Log4Drupal'),
'description' => t('Administer log4drupal, a file-based logging framework.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('log_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* We want this module to be loaded during the boostrap phase, so add an empty
* hook_boot method.
*
*/
function log_boot() {
log_notice("RUNNING BOOT FOR LOG MODULE");
}
function log_admin_settings() {
$form['log_loglevel'] = array(
'#type' => 'radios',
'#title' => t('Log priority level'),
'#default_value' => variable_get('log_loglevel', Log::MAX(PEAR_LOG_DEBUG)),
'#options' => array (
Log::MAX(PEAR_LOG_EMERG) => t('emergency'),
Log::MAX(PEAR_LOG_ALERT) => t('alert'),
Log::MAX(PEAR_LOG_CRIT) => t('critical'),
Log::MAX(PEAR_LOG_ERR) => t('error'),
Log::MAX(PEAR_LOG_WARNING) => t('warning'),
Log::MAX(PEAR_LOG_NOTICE) => t('notice'),
Log::MAX(PEAR_LOG_INFO) => t('info'),
Log::MAX(PEAR_LOG_DEBUG) => t('debug')
),
'#description' => t('All messages for specified level and above will appear in log file')
);
$form['log_stacklevel'] = array(
'#type' => 'radios',
'#title' => t('Stack trace priority level'),
'#default_value' => variable_get('log_stacklevel', Log::MAX(PEAR_LOG_ERROR)),
'#options' => array (
Log::MAX(PEAR_LOG_EMERG) => t('emergency'),
Log::MAX(PEAR_LOG_ALERT) => t('alert'),
Log::MAX(PEAR_LOG_CRIT) => t('critical'),
Log::MAX(PEAR_LOG_ERR) => t('error'),
Log::MAX(PEAR_LOG_WARNING) => t('warning'),
Log::MAX(PEAR_LOG_NOTICE) => t('notice'),
Log::MAX(PEAR_LOG_INFO) => t('info'),
Log::MAX(PEAR_LOG_DEBUG) => t('debug')
),
'#description' => t('All messages for specified level and above will include a stack trace in the log file')
);
$form['log_precision'] = array(
'#type' => 'radios',
'#title' => t('Filename precision'),
'#default_value' => variable_get('log_precision', 0),
'#options' => array (
t('Absolute path'),
t('Path relative to Drupal root'),
t('Filename only'),
),
'#description' => t('For log messages, how long should the filename be? Stack traces always include absolute paths.')
);
$form['log_logfile'] = array(
'#type' => 'textfield',
'#title' => t('Log file location'),
'#default_value' => variable_get('log_logfile', '/var/log/drupal/drupal.log'),
'#description' => t('Specify the absolute path for the file you would like to log to.')
);
return system_settings_form($form);
}
function log_help($section='')
{
$output = '';
switch ($section)
{
case "admin/help#log":
$output = '<p>'. t("Blah blah two"). '</p>';
break;
}
return $output;
}
/**
* Generate a logger.
*
* @return unknown
*/
function _log_get_logger()
{
static $logger;
if ( isset($logger))
return $logger;
$conf = array(
'lineFormat' => '[%1$s] [%3$s] %4$s',
'timeFormat' => '%X %x'
);
$logger = &Log::singleton(
'file',
variable_get('log_logfile', '/var/log/drupal/drupal.log'),
NULL,
$conf
);
$logger->setMask(variable_get('log_loglevel', Log::MAX(PEAR_LOG_DEBUG)));
return $logger;
}
/**
* Actually write the log message to the log file
*
* @param unknown_type $message The message to be written to the log file
* @param unknown_type $level The log level of the message
* @param unknown_type $include_stacktrace Should a full stacktrace be included?
*/
function _log_write_log_message($message, $level) {
$stacklevel = variable_get('log_stacklevel', Log::MAX(PEAR_LOG_ERROR));
if($stacklevel & Log::MASK($level)) {
$include_stacktrace = TRUE;
}
$trace = debug_backtrace();
$log_message = _log_get_top($trace). $message;
if($include_stacktrace) {
$log_message .= _log_get_stacktrace($trace);
}
_log_get_logger()->log($log_message, $level);
}
/**
* Return a simple string containing the file name and line number from which
* the log statement was generated.
*
* @param unknown_type $trace The output generated by debug_backtrace
* @return unknown A string of format "[/var/www/drupal/sites/all/modules/example/example.module:238]"
*/
function _log_get_top($trace) {
// Special case messages originating from our implementation of hook_watchdog
if($trace[1]['function'] == 'log_watchdog') {
$file = $trace[4]['file'];
$line = $trace[4]['line'];
}
else {
// The very top of the trace (the 0th element) always comes from
// this module. This is not very useful information for the end user
$file = $trace[1]['file'];
$line = $trace[1]['line'];
}
switch(variable_get('log_precision', 0)) {
case 1: $file = substr($file, strlen($_SERVER['DOCUMENT_ROOT'] . $GLOBALS['base_path'] )); break;
case 2: $file = ltrim(strrchr($file, "/"), "/"); break;
}
return "[$file:$line] ";
}
/**
* Return a "Java style" full stacktrace
*
* @param unknown_type $trace
* @return unknown
*/
function _log_get_stacktrace($trace) {
$output = '';
// Special case messages originating from our implementation of hook_watchdog
if($trace[1]['function'] == 'log_watchdog') {
$start_trace = 4;
}
else {
$start_trace = 1;
}
foreach($trace as $i => $step) {
if($step['file']) {
// The top of the trace always comes from this module.
// This is not very useful information for the end user so we skip it.
if($i >= $start_trace ) {
$output .= "\n at " . $step['file'] . ':' . $step['line'];
}
}
}
return $output;
}
function _log_get_message($args) {
$message = $args[0];
foreach ($args as $count => $arg){
if($count > 0) {
$message .= "\n" . print_r($arg, true);
}
}
return $message;
}
function log_debug($message) {
$message = _log_get_message(func_get_args());
if(_log_get_logger()->getMask() & Log::MASK(PEAR_LOG_DEBUG)) {
_log_write_log_message($message, PEAR_LOG_DEBUG);
}
}
function log_info($message) {
$message = _log_get_message(func_get_args());
if(_log_get_logger()->getMask() & Log::MASK(PEAR_LOG_INFO)){
_log_write_log_message($message, PEAR_LOG_INFO);
}
}
function log_notice($message) {
$message = _log_get_message(func_get_args());
if(_log_get_logger()->getMask() & Log::MASK(PEAR_LOG_NOTICE)){
_log_write_log_message($message, PEAR_LOG_NOTICE);
}
}
function log_warn($message) {
$message = _log_get_message(func_get_args());
if(_log_get_logger()->getMask() & Log::MASK(PEAR_LOG_WARNING)) {
_log_write_log_message($message, PEAR_LOG_WARNING);
}
}
function log_error($message) {
$message = _log_get_message(func_get_args());
if(_log_get_logger()->getMask() & Log::MASK(PEAR_LOG_ERROR)){
_log_write_log_message($message, PEAR_LOG_ERR);
}
}
function log_critical($message) {
$message = _log_get_message(func_get_args());
if(_log_get_logger()->getMask() & Log::MASK(PEAR_LOG_CRIT)){
_log_write_log_message($message, PEAR_LOG_CRIT);
}
}
function log_alert($message) {
$message = _log_get_message(func_get_args());
if(_log_get_logger()->getMask() & Log::MASK(PEAR_LOG_ALERT)){
_log_write_log_message($message, PEAR_LOG_ALERT);
}
}
function log_emergency($message) {
$message = _log_get_message(func_get_args());
if(_log_get_logger()->getMask() & Log::MASK(PEAR_LOG_EMERG)){
_log_write_log_message($message, PEAR_LOG_EMERG);
}
}
function log_watchdog($entry) {
$message = strip_tags(is_null($entry['variables']) ? $entry['message'] : strtr($entry['message'], $entry['variables']));
$severity = $entry['severity'];
_log_write_log_message($message, $severity);
}
include_once 'Log.php';
include_once 'Log/file.php';