xref: /plugin/combinedlogs/action.php (revision 244ef61d2f1fe1defdf9d188047f3d320ded4df4)
1<?php
2
3/**
4 * DokuWiki Plugin combinedlogs (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author Iain Hallam <first.last@example.com>
8 */
9
10use dokuwiki\Extension\ActionPlugin;
11use dokuwiki\Extension\EventHandler;
12use dokuwiki\Extension\Event;
13use dokuwiki\Logger;
14
15// phpcs:disable PSR1.Classes.ClassDeclaration -- DokuWiki plugins must not be in a namespace
16// phpcs:disable Squiz.Classes.ValidClassName -- DokuWiki requires snake case class names
17class action_plugin_combinedlogs extends ActionPlugin
18{
19    private Logger $logger;
20
21    public function __construct()
22    {
23        $this->logger = Logger::getInstance($this->getConf('facility'));
24    }
25
26    /** @inheritDoc */
27    public function register(EventHandler $controller)
28    {
29        $controller->register_hook('LOGGER_DATA_FORMAT', 'AFTER', $this, 'handleLoggerDataFormat');
30    }
31
32    /**
33     * Event handler for LOGGER_DATA_FORMAT
34     *
35     * @see https://www.dokuwiki.org/devel:events:LOGGER_DATA_FORMAT
36     * @param Event $event Event object by reference
37     * @param mixed $param optional parameter passed when event was registered
38     * @return void
39     */
40    public function handleLoggerDataFormat(Event $event)
41    {
42        global $conf;
43
44        // Work out the file name
45        if ($this->getConf('by_date') == 1) {
46            $event->data['logfile'] = $this->logger->getLogFile($event->data['datetime']);
47        } else {
48            $event->data['logfile'] = $conf['logdir'] . '/' . $this->getConf('facility') . '.log';
49        }
50
51        // Modify the first log line to include the facility name
52        $event->data['loglines'][0] = preg_replace(
53            '/^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})\t/',  // YYYY-MM-DD HH:MM:SS
54            "$1\t" . strtoupper($event->data['facility']) . "\t",
55            $event->data['loglines'][0]
56        );
57    }
58}
59