forked from mrhanlon/log_stdout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add admin menu and confirable format string
- Loading branch information
Showing
10 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Drupal Watchdog stdout | ||
# Drupal Logger stdout | ||
|
||
Log [Drupal Watchdog][1] messages to `php://stdout` or `php://stderr` for better log handling with Docker. | ||
Write [Drupal Logging API][1] messages to `php://stdout` or `php://stderr` for better log handling with Docker. | ||
|
||
[1]: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/watchdog/7 | ||
[1]: https://www.drupal.org/node/2595985 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "drupal/log_stdout", | ||
"description": "This module provides an hook for Drupal Watchdog that directs log messages to php://stdout or php://stderr for better log handling with Docker.", | ||
"type": "drupal-module", | ||
"homepage": "https://www.drupal.org/project/log_stdout", | ||
"support": { | ||
"issues": "https://www.drupal.org/project/issues/log_stdout", | ||
"irc": "irc://irc.freenode.org/drupal-support" | ||
}, | ||
"license": "GPL-2.0+" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
format: '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Schema for the configuration files of the syslog module. | ||
|
||
syslog.settings: | ||
type: config_object | ||
label: 'Log stdout settings' | ||
mapping: | ||
format: | ||
type: string | ||
label: 'Format' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: log_stdout | ||
description: Log watchdog messages to stdout | ||
package: Logging | ||
type: module | ||
# core: 8.x | ||
|
||
# Information added by Drupal.org packaging script on 2017-08-08 | ||
version: '8.x-1.1' | ||
core: '8.x' | ||
project: 'log_stdout' | ||
datestamp: 1502196546 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
log_stdout.settings: | ||
title: 'Log Stdout module settings' | ||
description: 'Log Stdout module configuration' | ||
parent: system.admin_config_development | ||
route_name: log_stdout.settings | ||
weight: 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
log_stdout.settings: | ||
path: '/admin/config/development/log_stdout' | ||
defaults: | ||
_form: 'Drupal\log_stdout\Form\LogStdoutConfigForm' | ||
_title: 'Log Stdout configuration' | ||
requirements: | ||
_permission: 'administer site configuration' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
logger.stdout: | ||
class: Drupal\log_stdout\Logger\Stdout | ||
arguments: ['@logger.log_message_parser'] | ||
tags: | ||
- { name: logger } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Drupal\log_stdout\Form; | ||
|
||
use Drupal\Core\Form\ConfigFormBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
|
||
class LogStdoutConfigForm extends ConfigFormBase { | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormId() { | ||
return 'log_stdout_config_form'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(array $form, FormStateInterface $form_state) { | ||
// Form constructor. | ||
$form = parent::buildForm($form, $form_state); | ||
// Default settings. | ||
$config = $this->config('log_stdout.settings'); | ||
|
||
$form['format'] = [ | ||
'#type' => 'textarea', | ||
'#title' => $this->t('Log format'), | ||
'#default_value' => $config->get('format'), | ||
'#description' => t('Specify the format of the log entry. Available variables are: <dl><dt><code>!base_url</code></dt><dd>Base URL of the site.</dd><dt><code>!timestamp</code></dt><dd>Unix timestamp of the log entry.</dd><dt><code>!type</code></dt><dd>The category to which this message belongs.</dd><dt><code>!ip</code></dt><dd>IP address of the user triggering the message.</dd><dt><code>!request_uri</code></dt><dd>The requested URI.</dd><dt><code>!referer</code></dt><dd>HTTP Referer if available.</dd><dt><code>!severity</code></dt><dd>The severity level of the event; ranges from 0 (Emergency) to 7 (Debug).</dd><dt><code>!uid</code></dt><dd>User ID.</dd><dt><code>!link</code></dt><dd>A link to associate with the message.</dd><dt><code>!message</code></dt><dd>The message to store in the log.</dd></dl>'), | ||
]; | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validateForm(array &$form, FormStateInterface $form_state) { | ||
|
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitForm(array &$form, FormStateInterface $form_state) { | ||
$config = $this->config('log_stdout.settings'); | ||
|
||
$config->set('format', $form_state->getValue('format')); | ||
$config->save(); | ||
|
||
return parent::submitForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getEditableConfigNames() { | ||
return [ | ||
'log_stdout.settings', | ||
]; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Drupal\log_stdout\Logger; | ||
|
||
use Drupal\Core\Logger\RfcLoggerTrait; | ||
use Psr\Log\LoggerInterface; | ||
use Drupal\Core\Logger\RfcLogLevel; | ||
use Drupal\Core\Session\AccountProxy; | ||
use Drupal\Core\Session\AccountInterface; | ||
use Drupal\Core\Logger\LogMessageParserInterface; | ||
|
||
class Stdout implements LoggerInterface { | ||
use RfcLoggerTrait; | ||
|
||
/** | ||
* The message's placeholders parser. | ||
* | ||
* @var \Drupal\Core\Logger\LogMessageParserInterface | ||
*/ | ||
protected $parser; | ||
|
||
/** | ||
* Constructs a Stdout object. | ||
* | ||
* @param \Drupal\Core\Logger\LogMessageParserInterface $parser | ||
* The parser to use when extracting message variables. | ||
*/ | ||
public function __construct(LogMessageParserInterface $parser) { | ||
$this->parser = $parser; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function log($level, $message, array $context = []) { | ||
$config = \Drupal::config('log_stdout.settings'); | ||
|
||
if ($level <= RfcLogLevel::WARNING) { | ||
$output = fopen('php://stderr', 'w'); | ||
} | ||
else { | ||
$output = fopen('php://stdout', 'w'); | ||
} | ||
$severity = strtoupper(RfcLogLevel::getLevels()[$level]); | ||
$username = ''; | ||
if (isset($context['user']) && !empty($context['user'])) { | ||
$username = $context['user']->getAccountName(); | ||
} | ||
if (empty($username)) { | ||
$username = 'anonymous'; | ||
} | ||
|
||
$variables = $this->parser->parseMessagePlaceholders($message, $context); | ||
$input_message = strip_tags(t($message, $variables)); | ||
|
||
/* context | ||
[channel] => cron | ||
[link] => | ||
[uid] => 0 | ||
[request_uri] => http://oci.rokebi.com/cron/v5HeY2s06H-VAp14afM9cWWej0S9AwUpmLjJ7KYfzZEnJ691-ZkJVDKca5wU0QsxQyrCyz0b-g | ||
[referer] => | ||
[ip] => 10.244.1.128 | ||
[timestamp] => 1597300569 | ||
*/ | ||
|
||
$message = t( $config->get('format'), [ | ||
'@timestamp' => $context['timestamp'], | ||
'@severity' => $severity, | ||
'@type' => $context['channel'], | ||
'@message' => $input_message, | ||
'@uid' => $context['uid'], | ||
'@request_uri' => $context['request_uri'], | ||
'@referer' => $context['referer'], | ||
'@ip' => $context['ip'], | ||
'@link' => $context['link'], | ||
'@date' => date('Y-m-d\TH:i:s', $context['timestamp']), | ||
]); | ||
|
||
fwrite($output, $message . "\r\n"); | ||
fclose($output); | ||
} | ||
|
||
} |