summaryrefslogtreecommitdiffstats
path: root/library/Audit/ProvidedHook/AuditStream.php
blob: b47968db0ad70b2b3a2bd5770bac2f3991d36a16 (plain)
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
<?php

/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Audit\ProvidedHook;

use InvalidArgumentException;
use Icinga\Application\Config;
use Icinga\Application\Hook\AuditHook;
use Icinga\Util\File;

class AuditStream extends AuditHook
{
    public function logMessage($time, $identity, $type, $message, array $data = null)
    {
        $activityData = [
            'activity_time' => $time,
            'activity'      => $type,
            'message'       => $message,
            'identity'      => $identity
        ];
        if (! empty($data)) {
            $activityData['data'] = $data;
        }

        $logConfig = Config::module('audit')->getSection('stream');
        if ($logConfig->format === 'json') {
            $json = json_encode($activityData, JSON_FORCE_OBJECT);
            if ($json === false) {
                throw new InvalidArgumentException('Failed to encode message data to JSON: ' . json_last_error_msg());
            }

            $file = new File($logConfig->get('path', '/var/log/icingaweb2/audit.json'), 'a');
            $file->fwrite($json . PHP_EOL);
            $file->fflush();
        }
    }
}