Skip to content

Commit

Permalink
add admin menu and confirable format string
Browse files Browse the repository at this point in the history
  • Loading branch information
bhtak committed Aug 13, 2020
1 parent c31c484 commit 9247ee5
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
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
11 changes: 11 additions & 0 deletions composer.json
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+"
}
1 change: 1 addition & 0 deletions config/install/log_stdout.settings.yml
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'
9 changes: 9 additions & 0 deletions config/schema/log_stdout.schema.yml
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'
11 changes: 11 additions & 0 deletions log_stdout.info.yml
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
6 changes: 6 additions & 0 deletions log_stdout.links.menu.yml
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
7 changes: 7 additions & 0 deletions log_stdout.routing.yml
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'
6 changes: 6 additions & 0 deletions log_stdout.services.yml
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 }
64 changes: 64 additions & 0 deletions src/Form/LogStdoutConfigForm.php
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',
];
}
}

?>
83 changes: 83 additions & 0 deletions src/Logger/Stdout.php
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);
}

}

0 comments on commit 9247ee5

Please sign in to comment.