├── .phpunit-watcher.yml
├── .styleci.yml
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── composer.json
├── config
├── di.php
└── params.php
├── infection.json.dist
├── psalm.xml
├── rector.php
└── src
└── SyslogTarget.php
/.phpunit-watcher.yml:
--------------------------------------------------------------------------------
1 | watch:
2 | directories:
3 | - src
4 | - tests
5 | fileMask: '*.php'
6 | notifications:
7 | passingTests: false
8 | failingTests: false
9 | phpunit:
10 | binaryPath: vendor/bin/phpunit
11 | timeout: 180
12 |
--------------------------------------------------------------------------------
/.styleci.yml:
--------------------------------------------------------------------------------
1 | preset: psr12
2 | risky: true
3 |
4 | version: 8.1
5 |
6 | finder:
7 | exclude:
8 | - docs
9 | - vendor
10 |
11 | enabled:
12 | - alpha_ordered_traits
13 | - array_indentation
14 | - array_push
15 | - combine_consecutive_issets
16 | - combine_consecutive_unsets
17 | - combine_nested_dirname
18 | - declare_strict_types
19 | - dir_constant
20 | - fully_qualified_strict_types
21 | - function_to_constant
22 | - hash_to_slash_comment
23 | - is_null
24 | - logical_operators
25 | - magic_constant_casing
26 | - magic_method_casing
27 | - method_separation
28 | - modernize_types_casting
29 | - native_function_casing
30 | - native_function_type_declaration_casing
31 | - no_alias_functions
32 | - no_empty_comment
33 | - no_empty_phpdoc
34 | - no_empty_statement
35 | - no_extra_block_blank_lines
36 | - no_short_bool_cast
37 | - no_superfluous_elseif
38 | - no_unneeded_control_parentheses
39 | - no_unneeded_curly_braces
40 | - no_unneeded_final_method
41 | - no_unset_cast
42 | - no_unused_imports
43 | - no_unused_lambda_imports
44 | - no_useless_else
45 | - no_useless_return
46 | - normalize_index_brace
47 | - php_unit_dedicate_assert
48 | - php_unit_dedicate_assert_internal_type
49 | - php_unit_expectation
50 | - php_unit_mock
51 | - php_unit_mock_short_will_return
52 | - php_unit_namespaced
53 | - php_unit_no_expectation_annotation
54 | - phpdoc_no_empty_return
55 | - phpdoc_no_useless_inheritdoc
56 | - phpdoc_order
57 | - phpdoc_property
58 | - phpdoc_scalar
59 | - phpdoc_singular_inheritdoc
60 | - phpdoc_trim
61 | - phpdoc_trim_consecutive_blank_line_separation
62 | - phpdoc_type_to_var
63 | - phpdoc_types
64 | - phpdoc_types_order
65 | - print_to_echo
66 | - regular_callable_call
67 | - return_assignment
68 | - self_accessor
69 | - self_static_accessor
70 | - set_type_to_cast
71 | - short_array_syntax
72 | - short_list_syntax
73 | - simplified_if_return
74 | - single_quote
75 | - standardize_not_equals
76 | - ternary_to_null_coalescing
77 | - trailing_comma_in_multiline_array
78 | - unalign_double_arrow
79 | - unalign_equals
80 | - empty_loop_body_braces
81 | - integer_literal_case
82 | - union_type_without_spaces
83 |
84 | disabled:
85 | - function_declaration
86 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Yii Logging Library - Syslog Target Change Log
2 |
3 | ## 2.1.1 under development
4 |
5 | - no changes in this release.
6 |
7 | ## 2.1.0 December 13, 2025
8 |
9 | - New #54: Add optional `$levels` parameter to `SyslogTarget` constructor allowing log level filtering at instantiation (@samdark)
10 | - Enh #45: Remove dead code that check case when `syslog()` returns false (@vjik)
11 |
12 | ## 2.0.0 February 17, 2023
13 |
14 | - Chg #32: Adapt configuration group names to Yii conventions (@vjik)
15 |
16 | ## 1.1.0 May 23, 2022
17 |
18 | - Chg #24: Raise the minimum `yiisoft/log` version to `^2.0` and the minimum PHP version to 8.0 (@rustamwin)
19 |
20 | ## 1.0.2 August 26, 2021
21 |
22 | - Bug #20: Remove `Psr\Log\LoggerInterface` definition from configuration for using multiple targets to application (@devanych)
23 |
24 | ## 1.0.1 March 23, 2021
25 |
26 | - Chg: Adjust config for new config plugin (@samdark)
27 |
28 | ## 1.0.0 February 11, 2021
29 |
30 | Initial release.
31 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright © 2008 by Yii Software (https://www.yiiframework.com/)
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions
6 | are met:
7 |
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in
12 | the documentation and/or other materials provided with the
13 | distribution.
14 | * Neither the name of Yii Software nor the names of its
15 | contributors may be used to endorse or promote products derived
16 | from this software without specific prior written permission.
17 |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 | POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
Yii Logging Library - Syslog Target
6 |
7 |
8 |
9 | [](https://packagist.org/packages/yiisoft/log-target-syslog)
10 | [](https://packagist.org/packages/yiisoft/log-target-syslog)
11 | [](https://github.com/yiisoft/log-target-syslog/actions?query=workflow%3Abuild)
12 | [](https://scrutinizer-ci.com/g/yiisoft/log-target-syslog/?branch=master)
13 | [](https://scrutinizer-ci.com/g/yiisoft/log-target-syslog/?branch=master)
14 | [](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/log-target-syslog/master)
15 | [](https://github.com/yiisoft/log-target-syslog/actions?query=workflow%3A%22static+analysis%22)
16 | [](https://shepherd.dev/github/yiisoft/log-target-syslog)
17 |
18 | This package provides the Syslog target for the [yiisoft/log](https://github.com/yiisoft/log) library.
19 |
20 | ## Requirements
21 |
22 | - PHP 8.0 or higher.
23 |
24 | ## Installation
25 |
26 | The package could be installed with [Composer](https://getcomposer.org):
27 |
28 | ```shell
29 | composer require yiisoft/log-target-syslog
30 | ```
31 |
32 | ## General usage
33 |
34 | Creating a target:
35 |
36 | ```php
37 | use Yiisoft\Log\Target\Syslog\SyslogTarget;
38 |
39 | $syslogTarget = new SyslogTarget($identity, $options, $facility, $levels);
40 | ```
41 |
42 | - `$identity (string)` - The `openlog()` identity.
43 | - `$options (int)` - The `openlog()` options. Defaults to `LOG_ODELAY | LOG_PID`.
44 | - `$facility (int)` - The `openlog()` facility. Defaults to `LOG_USER`.
45 | - `$levels (array)` - The log message levels that this target is interested in. Defaults to `[]` (all levels).
46 |
47 | For example, to log only errors and warnings:
48 |
49 | ```php
50 | $syslogTarget = new SyslogTarget(
51 | 'app',
52 | LOG_ODELAY | LOG_PID,
53 | LOG_USER,
54 | [LogLevel::ERROR, LogLevel::WARNING]
55 | );
56 | ```
57 |
58 | For more information about `$identity`, `$options`, and `$facility`, see the description of the [`openlog()`](https://www.php.net/openlog) function.
59 |
60 | Creating a logger:
61 |
62 | ```php
63 | $logger = new \Yiisoft\Log\Logger([$syslogTarget]);
64 | ```
65 | For use in the [Yii framework](https://www.yiiframework.com/), see the configuration files:
66 | - [`config/di.php`](https://github.com/yiisoft/log-target-syslog/blob/master/config/di.php)
67 | - [`config/params.php`](https://github.com/yiisoft/log-target-syslog/blob/master/config/params.php)
68 |
69 | ## Documentation
70 |
71 | For a description of using the logger, see the [yiisoft/log](https://github.com/yiisoft/log) package.
72 |
73 | - [Yii guide to logging](https://github.com/yiisoft/docs/blob/master/guide/en/runtime/logging.md)
74 | - [Internals](docs/internals.md)
75 |
76 | If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place
77 | for that. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
78 |
79 | ## License
80 |
81 | The Yii Logging Library - Syslog Target is free software. It is released under the terms of the BSD License.
82 | Please see [`LICENSE`](./LICENSE.md) for more information.
83 |
84 | Maintained by [Yii Software](https://www.yiiframework.com/).
85 |
86 | ### Support the project
87 |
88 | [](https://opencollective.com/yiisoft)
89 |
90 | ### Follow updates
91 |
92 | [](https://www.yiiframework.com/)
93 | [](https://twitter.com/yiiframework)
94 | [](https://t.me/yii3en)
95 | [](https://www.facebook.com/groups/yiitalk)
96 | [](https://yiiframework.com/go/slack)
97 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "yiisoft/log-target-syslog",
3 | "type": "library",
4 | "description": "Yii Logging Library - Syslog Target",
5 | "keywords": [
6 | "yii",
7 | "framework",
8 | "log",
9 | "logger",
10 | "syslog"
11 | ],
12 | "homepage": "https://www.yiiframework.com/",
13 | "license": "BSD-3-Clause",
14 | "support": {
15 | "issues": "https://github.com/yiisoft/log-target-syslog/issues?state=open",
16 | "source": "https://github.com/yiisoft/log-target-syslog",
17 | "forum": "https://www.yiiframework.com/forum/",
18 | "wiki": "https://www.yiiframework.com/wiki/",
19 | "irc": "ircs://irc.libera.chat:6697/yii",
20 | "chat": "https://t.me/yii3en"
21 | },
22 | "funding": [
23 | {
24 | "type": "opencollective",
25 | "url": "https://opencollective.com/yiisoft"
26 | },
27 | {
28 | "type": "github",
29 | "url": "https://github.com/sponsors/yiisoft"
30 | }
31 | ],
32 | "require": {
33 | "php": "^8.0",
34 | "psr/log": "^3.0",
35 | "yiisoft/log": "^2.2"
36 | },
37 | "require-dev": {
38 | "maglnet/composer-require-checker": "^4.2",
39 | "php-mock/php-mock-phpunit": "^2.6",
40 | "phpunit/phpunit": "^9.5",
41 | "rector/rector": "^2.0.3",
42 | "roave/infection-static-analysis-plugin": "^1.25",
43 | "spatie/phpunit-watcher": "^1.23",
44 | "vimeo/psalm": "^4.30|^5.6",
45 | "yiisoft/di": "^1.2"
46 | },
47 | "autoload": {
48 | "psr-4": {
49 | "Yiisoft\\Log\\Target\\Syslog\\": "src"
50 | }
51 | },
52 | "autoload-dev": {
53 | "psr-4": {
54 | "Yiisoft\\Log\\Target\\Syslog\\Tests\\": "tests"
55 | }
56 | },
57 | "extra": {
58 | "config-plugin-options": {
59 | "source-directory": "config"
60 | },
61 | "config-plugin": {
62 | "di": "di.php",
63 | "params": "params.php"
64 | }
65 | },
66 | "config": {
67 | "sort-packages": true,
68 | "allow-plugins": {
69 | "infection/extension-installer": true,
70 | "composer/package-versions-deprecated": true
71 | }
72 | },
73 | "scripts": {
74 | "test": "phpunit --testdox --no-interaction",
75 | "test-watch": "phpunit-watcher watch"
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/config/di.php:
--------------------------------------------------------------------------------
1 | static fn () => new SyslogTarget(
11 | identity: $params['yiisoft/log-target-syslog']['syslogTarget']['identity'],
12 | levels: $params['yiisoft/log-target-syslog']['syslogTarget']['levels'] ?? [],
13 | ),
14 | ];
15 |
--------------------------------------------------------------------------------
/config/params.php:
--------------------------------------------------------------------------------
1 | [
7 | 'syslogTarget' => [
8 | 'identity' => 'app',
9 | ],
10 | ],
11 | ];
12 |
--------------------------------------------------------------------------------
/infection.json.dist:
--------------------------------------------------------------------------------
1 | {
2 | "source": {
3 | "directories": [
4 | "src"
5 | ]
6 | },
7 | "logs": {
8 | "text": "php:\/\/stderr",
9 | "stryker": {
10 | "report": "master"
11 | }
12 | },
13 | "mutators": {
14 | "@default": true
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/psalm.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/rector.php:
--------------------------------------------------------------------------------
1 | paths([
12 | __DIR__ . '/src',
13 | __DIR__ . '/tests',
14 | ]);
15 |
16 | // register a single rule
17 | $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
18 |
19 | // define sets of rules
20 | $rectorConfig->sets([
21 | LevelSetList::UP_TO_PHP_80,
22 | ]);
23 |
24 | $rectorConfig->skip([
25 | ClosureToArrowFunctionRector::class,
26 | ]);
27 | };
28 |
--------------------------------------------------------------------------------
/src/SyslogTarget.php:
--------------------------------------------------------------------------------
1 | LOG_EMERG,
31 | LogLevel::ALERT => LOG_ALERT,
32 | LogLevel::CRITICAL => LOG_CRIT,
33 | LogLevel::ERROR => LOG_ERR,
34 | LogLevel::WARNING => LOG_WARNING,
35 | LogLevel::NOTICE => LOG_NOTICE,
36 | LogLevel::INFO => LOG_INFO,
37 | LogLevel::DEBUG => LOG_DEBUG,
38 | ];
39 |
40 | /**
41 | * @param string $identity The string that is prefixed to each message.
42 | * @param int $options Bit options to be used when generating a log message.
43 | * @param int $facility Used to specify what type of program is logging the message. This allows you to specify
44 | * (in your machine's syslog configuration) how messages coming from different facilities will be handled.
45 | * @param string[] $levels The {@see \Psr\Log\LogLevel log message levels} that this target is interested in.
46 | *
47 | * @link https://www.php.net/openlog
48 | */
49 | public function __construct(
50 | private string $identity,
51 | private int $options = LOG_ODELAY | LOG_PID,
52 | private int $facility = LOG_USER,
53 | array $levels = []
54 | ) {
55 | parent::__construct($levels);
56 |
57 | $this->setFormat(static function (Message $message) {
58 | return "[{$message->level()}][{$message->context('category', '')}] {$message->message()}";
59 | });
60 | }
61 |
62 | /**
63 | * Writes log messages to syslog.
64 | *
65 | * @see https://www.php.net/openlog
66 | * @see https://www.php.net/syslog
67 | * @see https://www.php.net/closelog
68 | *
69 | * @throws RuntimeException If unable to export log through system log.
70 | */
71 | protected function export(): void
72 | {
73 | $formattedMessages = $this->getFormattedMessages();
74 | openlog($this->identity, $this->options, $this->facility);
75 |
76 | foreach ($this->getMessages() as $key => $message) {
77 | syslog(self::SYSLOG_LEVELS[$message->level()], $formattedMessages[$key]);
78 | }
79 |
80 | closelog();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------