1<?php declare(strict_types=1);
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Formatter;
13
14/**
15 * Encodes message information into JSON in a format compatible with Logmatic.
16 *
17 * @author Julien Breux <julien.breux@gmail.com>
18 */
19class LogmaticFormatter extends JsonFormatter
20{
21    protected const MARKERS = ["sourcecode", "php"];
22
23    /**
24     * @var string
25     */
26    protected $hostname = '';
27
28    /**
29     * @var string
30     */
31    protected $appname = '';
32
33    public function setHostname(string $hostname): self
34    {
35        $this->hostname = $hostname;
36
37        return $this;
38    }
39
40    public function setAppname(string $appname): self
41    {
42        $this->appname = $appname;
43
44        return $this;
45    }
46
47    /**
48     * Appends the 'hostname' and 'appname' parameter for indexing by Logmatic.
49     *
50     * @see http://doc.logmatic.io/docs/basics-to-send-data
51     * @see \Monolog\Formatter\JsonFormatter::format()
52     */
53    public function format(array $record): string
54    {
55        if (!empty($this->hostname)) {
56            $record["hostname"] = $this->hostname;
57        }
58        if (!empty($this->appname)) {
59            $record["appname"] = $this->appname;
60        }
61
62        $record["@marker"] = static::MARKERS;
63
64        return parent::format($record);
65    }
66}
67