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\Handler;
13
14use Monolog\Logger;
15use Monolog\Formatter\FormatterInterface;
16use Monolog\Formatter\LineFormatter;
17
18/**
19 * Common syslog functionality
20 *
21 * @phpstan-import-type Level from \Monolog\Logger
22 */
23abstract class AbstractSyslogHandler extends AbstractProcessingHandler
24{
25    /** @var int */
26    protected $facility;
27
28    /**
29     * Translates Monolog log levels to syslog log priorities.
30     * @var array
31     * @phpstan-var array<Level, int>
32     */
33    protected $logLevels = [
34        Logger::DEBUG     => LOG_DEBUG,
35        Logger::INFO      => LOG_INFO,
36        Logger::NOTICE    => LOG_NOTICE,
37        Logger::WARNING   => LOG_WARNING,
38        Logger::ERROR     => LOG_ERR,
39        Logger::CRITICAL  => LOG_CRIT,
40        Logger::ALERT     => LOG_ALERT,
41        Logger::EMERGENCY => LOG_EMERG,
42    ];
43
44    /**
45     * List of valid log facility names.
46     * @var array<string, int>
47     */
48    protected $facilities = [
49        'auth'     => LOG_AUTH,
50        'authpriv' => LOG_AUTHPRIV,
51        'cron'     => LOG_CRON,
52        'daemon'   => LOG_DAEMON,
53        'kern'     => LOG_KERN,
54        'lpr'      => LOG_LPR,
55        'mail'     => LOG_MAIL,
56        'news'     => LOG_NEWS,
57        'syslog'   => LOG_SYSLOG,
58        'user'     => LOG_USER,
59        'uucp'     => LOG_UUCP,
60    ];
61
62    /**
63     * @param string|int $facility Either one of the names of the keys in $this->facilities, or a LOG_* facility constant
64     */
65    public function __construct($facility = LOG_USER, $level = Logger::DEBUG, bool $bubble = true)
66    {
67        parent::__construct($level, $bubble);
68
69        if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
70            $this->facilities['local0'] = LOG_LOCAL0;
71            $this->facilities['local1'] = LOG_LOCAL1;
72            $this->facilities['local2'] = LOG_LOCAL2;
73            $this->facilities['local3'] = LOG_LOCAL3;
74            $this->facilities['local4'] = LOG_LOCAL4;
75            $this->facilities['local5'] = LOG_LOCAL5;
76            $this->facilities['local6'] = LOG_LOCAL6;
77            $this->facilities['local7'] = LOG_LOCAL7;
78        } else {
79            $this->facilities['local0'] = 128; // LOG_LOCAL0
80            $this->facilities['local1'] = 136; // LOG_LOCAL1
81            $this->facilities['local2'] = 144; // LOG_LOCAL2
82            $this->facilities['local3'] = 152; // LOG_LOCAL3
83            $this->facilities['local4'] = 160; // LOG_LOCAL4
84            $this->facilities['local5'] = 168; // LOG_LOCAL5
85            $this->facilities['local6'] = 176; // LOG_LOCAL6
86            $this->facilities['local7'] = 184; // LOG_LOCAL7
87        }
88
89        // convert textual description of facility to syslog constant
90        if (is_string($facility) && array_key_exists(strtolower($facility), $this->facilities)) {
91            $facility = $this->facilities[strtolower($facility)];
92        } elseif (!in_array($facility, array_values($this->facilities), true)) {
93            throw new \UnexpectedValueException('Unknown facility value "'.$facility.'" given');
94        }
95
96        $this->facility = $facility;
97    }
98
99    /**
100     * {@inheritDoc}
101     */
102    protected function getDefaultFormatter(): FormatterInterface
103    {
104        return new LineFormatter('%channel%.%level_name%: %message% %context% %extra%');
105    }
106}
107