Skip to content

Commit 2458535

Browse files
devanychsamdark
andauthored
Cleanup and refactoring (#14)
* Change Psalm level from 3 to 2 * Fix phpunit config * Fix .gitignore * Remove tests/bootstrap.php * Update composer.json * Add config * Refactoring SyslogTarget * Add description to readme * Fix phpdoc Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
1 parent 8588028 commit 2458535

10 files changed

Lines changed: 250 additions & 242 deletions

File tree

.gitignore

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
# IDE & OS files
1+
# phpstorm project files
22
.idea
3+
4+
# netbeans project files
35
nbproject
6+
7+
# zend studio for eclipse project files
48
.buildpath
59
.project
610
.settings
7-
*.sublime-project
8-
*.sublime-workspace
11+
12+
# windows thumbnail cache
913
Thumbs.db
10-
.DS_Store
1114

1215
# composer vendor dir
1316
/vendor
1417

18+
/composer.lock
19+
1520
# composer itself is not needed
1621
composer.phar
17-
composer.lock
22+
23+
# Mac DS_Store Files
24+
.DS_Store
1825

1926
# phpunit itself is not needed
2027
phpunit.phar
2128
# local phpunit config
2229
/phpunit.xml
2330
# phpunit cache
2431
.phpunit.result.cache
25-
26-
# tests runtime
27-
/runtime

README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
<br>
77
</p>
88

9-
This library provides the Syslog target for the [yiisoft/log] library.
10-
11-
[yiisoft/log]: https://github.com/yiisoft/log
12-
139
[![Latest Stable Version](https://poser.pugx.org/yiisoft/log-target-syslog/v/stable.png)](https://packagist.org/packages/yiisoft/log-target-syslog)
1410
[![Total Downloads](https://poser.pugx.org/yiisoft/log-target-syslog/downloads.png)](https://packagist.org/packages/yiisoft/log-target-syslog)
1511
[![Build status](https://github.com/yiisoft/log-target-syslog/workflows/build/badge.svg)](https://github.com/yiisoft/log-target-syslog/actions?query=workflow%3Abuild)
@@ -19,6 +15,47 @@ This library provides the Syslog target for the [yiisoft/log] library.
1915
[![static analysis](https://github.com/yiisoft/log-target-syslog/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/log-target-syslog/actions?query=workflow%3A%22static+analysis%22)
2016
[![type-coverage](https://shepherd.dev/github/yiisoft/log-target-syslog/coverage.svg)](https://shepherd.dev/github/yiisoft/log-target-syslog)
2117

18+
This package provides the Syslog target for the [yiisoft/log](https://github.com/yiisoft/log) library.
19+
20+
## Installation
21+
22+
The package could be installed with composer:
23+
24+
```
25+
composer install yiisoft/log-target-syslog
26+
```
27+
28+
## General usage
29+
30+
Creating a target:
31+
32+
```php
33+
use Yiisoft\Log\Target\Syslog\SyslogTarget;
34+
35+
$syslogTarget = new SyslogTarget($identity, $options, $facility);
36+
```
37+
38+
- `$identity (string)` - The `openlog()` identity.
39+
- `$options (int)` - The `openlog()` options. Defaults to `LOG_ODELAY | LOG_PID`.
40+
- `$facility (int)` - The `openlog()` facility. Defaults to `LOG_USER`.
41+
42+
For more information, see the description of the [`openlog()`](https://www.php.net/openlog) function.
43+
44+
Creating a logger:
45+
46+
```php
47+
$logger = new \Yiisoft\Log\Logger([$syslogTarget]);
48+
```
49+
50+
For a description of using the logger, see the [yiisoft/log](https://github.com/yiisoft/log) package.
51+
52+
For use in the [Yii framework](http://www.yiiframework.com/), see the configuration files:
53+
54+
- [`config/common.php`](https://github.com/yiisoft/log-target-syslog/blob/master/config/common.php)
55+
- [`config/params.php`](https://github.com/yiisoft/log-target-syslog/blob/master/config/params.php)
56+
57+
See [Yii guide to logging](https://github.com/yiisoft/docs/blob/master/guide/en/runtime/logging.md) for more info.
58+
2259
### Unit testing
2360

2461
The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
"prefer-stable": true,
2323
"require": {
2424
"php": "^7.4|^8.0",
25-
"psr/log": "^1.1",
2625
"yiisoft/log": "^3.0@dev"
2726
},
2827
"require-dev": {
28+
"php-mock/php-mock-phpunit": "^2.6",
2929
"phpunit/phpunit": "^9.4",
3030
"roave/infection-static-analysis-plugin": "^1.5",
3131
"spatie/phpunit-watcher": "^1.23",
@@ -44,6 +44,10 @@
4444
"extra": {
4545
"branch-alias": {
4646
"dev-master": "3.0.x-dev"
47+
},
48+
"config-plugin": {
49+
"common": "config/common.php",
50+
"params": "config/params.php"
4751
}
4852
},
4953
"config": {

config/common.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Psr\Log\LoggerInterface;
6+
use Yiisoft\Aliases\Aliases;
7+
use Yiisoft\Log\Logger;
8+
use Yiisoft\Log\Target\Syslog\SyslogTarget;
9+
10+
/* @var $params array */
11+
12+
return [
13+
LoggerInterface::class => static fn (SyslogTarget $syslogTarget) => new Logger([$syslogTarget]),
14+
15+
SyslogTarget::class => static fn (Aliases $aliases) => new SyslogTarget(
16+
$aliases->get($params['yiisoft/log-target-syslog']['syslogTarget']['identity']),
17+
),
18+
];

config/params.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'yiisoft/log-target-syslog' => [
7+
'syslogTarget' => [
8+
'identity' => 'app',
9+
],
10+
],
11+
];

phpunit.xml.dist

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="./tests/bootstrap.php"
3-
backupGlobals="true"
4-
colors="true"
5-
verbose="true"
6-
failOnRisky="true"
7-
failOnWarning="true"
8-
convertErrorsToExceptions="true"
9-
convertNoticesToExceptions="true"
10-
convertWarningsToExceptions="true"
11-
stopOnFailure="false">
2+
3+
<phpunit bootstrap="vendor/autoload.php"
4+
colors="true"
5+
verbose="true"
6+
failOnRisky="true"
7+
failOnWarning="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
stopOnFailure="false"
12+
executionOrder="default"
13+
resolveDependencies="true"
14+
>
15+
<php>
16+
<ini name="error_reporting" value="-1"/>
17+
</php>
18+
1219
<testsuites>
1320
<testsuite name="Yii Log - Syslog Target tests">
1421
<directory>./tests</directory>
1522
</testsuite>
1623
</testsuites>
1724

18-
<filter>
19-
<whitelist>
20-
<directory>./</directory>
21-
<exclude>
22-
<directory>./tests</directory>
23-
<directory>./vendor</directory>
24-
</exclude>
25-
</whitelist>
26-
</filter>
25+
<coverage>
26+
<include>
27+
<directory>./src</directory>
28+
</include>
29+
</coverage>
2730
</phpunit>

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3-
errorLevel="3"
3+
errorLevel="2"
44
resolveFromConfigFile="true"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66
xmlns="https://getpsalm.org/schema/config"

src/SyslogTarget.php

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,51 @@
99
use Yiisoft\Log\Message;
1010
use 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

Comments
 (0)