View source
<?php
define('JSONLOG_TRUNCATE_DEFAULT', 64);
function jsonlog_form_system_logging_settings_alter(&$form, &$form_state) {
module_load_include('inc', 'jsonlog');
return _jsonlog_form_system_logging_settings_alter($form, $form_state);
}
function jsonlog_form_system_logging_settings_validate($form, $form_state) {
$values =& $form_state['values'];
if ($values['jsonlog_truncate'] !== '' && ($value = trim($values['jsonlog_truncate'])) !== '') {
if (!preg_match('/^\\d+$/', $value)) {
form_set_error('jsonlog_truncate', t('\'@field\' is not a non-negative integer.', array(
'@field' => t('Truncate events to'),
)));
}
}
}
function jsonlog_form_system_logging_settings_submit($form, &$form_state) {
module_load_include('inc', 'jsonlog');
$values =& $form_state['values'];
$fields = array(
'jsonlog_severity_threshold',
'jsonlog_truncate',
'jsonlog_siteid',
'jsonlog_dir',
'jsonlog_file_time',
);
foreach ($fields as $name) {
$values[$name] = trim($values[$name]);
}
if (!$values['jsonlog_truncate']) {
$values['jsonlog_truncate'] = 0;
}
if (!$values['jsonlog_dir']) {
$values['jsonlog_dir'] = jsonlog_default_dir();
}
if (($values['jsonlog_tags'] = $v = trim($values['jsonlog_tags'])) !== '') {
$v = str_replace(array(
"\r",
"\n",
), '', $v);
if ($v !== '') {
if ($v[0] === ',') {
$v = substr($v, 1);
}
if ($v !== '') {
if ($v[strlen($v) - 1] === ',') {
$v = substr($v, 0, strlen($v) - 1);
}
}
if (strpos($v, ',')) {
$v = preg_replace('/ *, */', ',', $v);
}
}
$values['jsonlog_tags'] = trim($v);
}
if ($values['jsonlog_test_filing'] && $values['jsonlog_siteid'] && $values['jsonlog_dir'] && $values['jsonlog_file_time']) {
module_load_include('inc', 'jsonlog');
jsonlog_test_filing($values['jsonlog_dir'] . '/' . $values['jsonlog_siteid'] . ($values['jsonlog_file_time'] == 'none' ? '' : '.' . date($values['jsonlog_file_time'])) . '.json.log');
}
unset($form_state['input']['jsonlog_test_filing'], $values['jsonlog_test_filing']);
}
function jsonlog_watchdog(array $log_entry) {
static $_threshold, $_site_id, $_canonical, $_file, $_truncate, $_severity, $_tags;
if (!$_threshold) {
if (!($_threshold = getenv('drupal_jsonlog_severity_threshold'))) {
$_threshold = variable_get('jsonlog_severity_threshold', WATCHDOG_WARNING);
}
}
if ($log_entry['severity'] > $_threshold) {
return;
}
if (!$_site_id) {
if (!($_site_id = getenv('drupal_jsonlog_siteid'))) {
if (!($_site_id = variable_get('jsonlog_siteid'))) {
module_load_include('inc', 'jsonlog');
variable_set('jsonlog_siteid', $_site_id = jsonlog_default_site_id());
}
}
if (!($_canonical = getenv('drupal_jsonlog_canonical'))) {
$_canonical = variable_get('jsonlog_canonical', '');
}
if (!($dir = getenv('drupal_jsonlog_dir'))) {
if (!($dir = variable_get('jsonlog_dir'))) {
module_load_include('inc', 'jsonlog');
if ($dir = jsonlog_default_dir()) {
variable_set('jsonlog_dir', $dir);
}
else {
error_log('Drupal jsonlog, site ID[' . $_site_id . '], failed to establish server\'s default log dir.');
}
}
}
if (!($file_time = getenv('drupal_jsonlog_file_time'))) {
$file_time = variable_get('jsonlog_file_time', 'Ymd');
}
$_file = $dir . '/' . $_site_id . ($file_time == 'none' ? '' : '.' . date($file_time)) . '.json.log';
if (($_truncate = getenv('drupal_jsonlog_truncate')) === FALSE) {
$_truncate = variable_get('jsonlog_truncate', JSONLOG_TRUNCATE_DEFAULT);
}
if ($_truncate) {
$_truncate *= 1024;
$_truncate -= 768;
$_truncate *= 7 / 8;
}
$_severity = array(
'emergency',
'alert',
'critical',
'error',
'warning',
'notice',
'info',
'debug',
);
$tags_server = ($tags = getenv('drupal_jsonlog_tags')) !== FALSE ? $tags : '';
$tags_site = variable_get('jsonlog_tags', '');
if ($tags_server) {
$tags = $tags_server;
if ($tags_site) {
$tags .= ',' . $tags_site;
}
}
else {
$tags = $tags_site;
}
if ($tags) {
$_tags = explode(',', $tags);
}
}
$entry = new stdClass();
$truncation = NULL;
if ($message = $log_entry['message']) {
if ($variables = $log_entry['variables']) {
foreach ($variables as $key => $value) {
if ($key[0] != '!') {
$variables[$key] = check_plain($value);
}
}
$message = strtr($message, $variables);
unset($variables);
}
if ($message[0] === '<') {
$message = strip_tags($message);
}
$message = str_replace("\0", '_NUL_', $message);
if ($_truncate && ($le = strlen($message)) > $_truncate) {
$truncation = array(
$le,
strlen($message = drupal_truncate_bytes($message, (int) $_truncate)),
);
}
}
$entry->message = $message;
unset($message);
$millis = round(microtime(TRUE) * 1000);
$seconds = (int) floor($millis / 1000);
$millis -= $seconds * 1000;
$millis = str_pad($millis, 3, '0', STR_PAD_LEFT);
$entry->{'@timestamp'} = substr(gmdate('c', $seconds), 0, 19) . '.' . $millis . 'Z';
$entry->{'@version'} = 1;
$entry->message_id = uniqid($_site_id, TRUE);
$entry->site_id = $_site_id;
$entry->canonical = $_canonical;
$entry->tags = $_tags;
$entry->type = 'drupal';
$entry->subtype = $log_entry['type'];
$entry->severity = $_severity[$log_entry['severity']];
$entry->method = !empty($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'cli';
$entry->request_uri = $log_entry['request_uri'];
$entry->referer = $log_entry['referer'];
$entry->uid = $uid = $log_entry['uid'];
$entry->username = $uid && !empty($GLOBALS['user']->name) ? $GLOBALS['user']->name : '';
$entry->client_ip = $log_entry['ip'];
if (!($link = $log_entry['link'])) {
$entry->link = NULL;
$entry->code = 0;
}
elseif (ctype_digit($link)) {
$entry->link = NULL;
$entry->code = (int) $link;
}
else {
$entry->link = strip_tags($link);
$entry->code = 0;
}
$entry->variables = NULL;
$entry->trunc = $truncation;
if (!file_put_contents($_file, "\n" . drupal_json_encode($entry), FILE_APPEND | LOCK_EX)) {
error_log('Drupal jsonlog, site ID[' . $_site_id . '], failed to write to file[' . $_file . '].');
}
}