99use Yiisoft \Log \Message ;
1010use Yiisoft \Log \Target ;
1111
12+ use const LOG_ALERT ;
13+ use const LOG_CRIT ;
14+ use const LOG_DEBUG ;
15+ use const LOG_EMERG ;
16+ use const LOG_ERR ;
17+ use const LOG_INFO ;
18+ use const LOG_NOTICE ;
19+ use const LOG_ODELAY ;
20+ use const LOG_PID ;
21+ use const LOG_USER ;
22+ use const LOG_WARNING ;
23+
1224/**
1325 * SyslogTarget writes log to syslog.
1426 */
15- class SyslogTarget extends Target
27+ final class SyslogTarget extends Target
1628{
1729 /**
18- * @var string syslog identity
30+ * @var string The string that is prefixed to each message.
31+ *
32+ * @see https://www.php.net/openlog
1933 */
2034 private string $ identity ;
2135
2236 /**
23- * @var int syslog facility.
24- */
25- private int $ facility = LOG_USER ;
26-
27- /**
28- * @var int openlog options. This is a bitfield passed as the `$option` parameter to [openlog()](http://php.net/openlog).
37+ * @var int Bit options to be used when generating a log message.
38+ *
2939 * Defaults to `LOG_ODELAY | LOG_PID`.
3040 *
31- * @see http ://php.net/openlog for available options.
41+ * @see https ://www. php.net/openlog
3242 */
33- private int $ options = LOG_ODELAY | LOG_PID ;
43+ private int $ options ;
3444
3545 /**
36- * @var bool Whether the message format was previously set.
46+ * @var int Used to specify what type of program is logging the message. This allows you to specify (in your
47+ * machine's syslog configuration) how messages coming from different facilities will be handled.
48+ *
49+ * Defaults to `LOG_USER`.
50+ *
51+ * @see https://www.php.net/openlog
3752 */
38- private bool $ isMessageFormatSet = false ;
53+ private int $ facility ;
3954
4055 /**
41- * @var array syslog levels
56+ * @var array Syslog levels.
4257 */
4358 private array $ syslogLevels = [
4459 LogLevel::EMERGENCY => LOG_EMERG ,
@@ -51,47 +66,38 @@ class SyslogTarget extends Target
5166 LogLevel::DEBUG => LOG_DEBUG ,
5267 ];
5368
54- public function setIdentity (string $ identity ): self
69+ /**
70+ * @param string $identity The string that is prefixed to each message.
71+ * @param int $options Bit options to be used when generating a log message.
72+ * @param int $facility Used to specify what type of program is logging the message. This allows you to specify (in your
73+ * machine's syslog configuration) how messages coming from different facilities will be handled.
74+ */
75+ public function __construct (string $ identity , int $ options = LOG_ODELAY | LOG_PID , int $ facility = LOG_USER )
5576 {
5677 $ this ->identity = $ identity ;
57- return $ this ;
58- }
59-
60- public function setFacility (int $ facility ): self
61- {
62- $ this ->facility = $ facility ;
63- return $ this ;
64- }
65-
66- public function setOptions (int $ options ): self
67- {
6878 $ this ->options = $ options ;
69- return $ this ;
70- }
79+ $ this -> facility = $ facility ;
80+ parent :: __construct ();
7181
72- public function setFormat (callable $ format ): Target
73- {
74- $ this ->isMessageFormatSet = true ;
75- return parent ::setFormat ($ format );
82+ $ this ->setFormat (static function (Message $ message ) {
83+ return "[ {$ message ->level ()}][ {$ message ->context ('category ' , '' )}] {$ message ->message ()}" ;
84+ });
7685 }
7786
7887 /**
7988 * Writes log messages to syslog.
80- * Starting from version 2.0.14, this method throws RuntimeException in case the log can not be exported.
8189 *
82- * @throws RuntimeException
90+ * @see https://www.php.net/openlog
91+ * @see https://www.php.net/syslog
92+ * @see https://www.php.net/closelog
93+ *
94+ * @throws RuntimeException If unable to export log through system log.
8395 */
84- public function export (): void
96+ protected function export (): void
8597 {
8698 $ formattedMessages = $ this ->getFormattedMessages ();
8799 openlog ($ this ->identity , $ this ->options , $ this ->facility );
88100
89- if (!$ this ->isMessageFormatSet ) {
90- $ this ->setFormat (static function (Message $ message ) {
91- return "[ {$ message ->level ()}][ {$ message ->context ('category ' , '' )}] {$ message ->message ()}" ;
92- });
93- }
94-
95101 foreach ($ this ->getMessages () as $ key => $ message ) {
96102 if (syslog ($ this ->syslogLevels [$ message ->level ()], $ formattedMessages [$ key ]) === false ) {
97103 throw new RuntimeException ('Unable to export log through system log. ' );
0 commit comments