Skip to content

Commit

Permalink
Add attribute to set the openlog $option attribute for SyslogTarget
Browse files Browse the repository at this point in the history
Fixes issue yiisoft#13074
close yiisoft#13075
  • Loading branch information
timbeks authored and cebe committed Nov 28, 2016
1 parent 3ce698b commit f23dc5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Yii Framework 2 Change Log
- Enh #13035: Use ArrayHelper::getValue() in SluggableBehavior::getValue() (thyseus)
- Enh #13020: Added `disabledListItemSubTagOptions` attribute for `yii\widgets\LinkPager` in order to customize the disabled list item sub tag element (nadar)
- Enh #12988: Changed `textarea` method within the `yii\helpers\BaseHtml` class to allow users to control whether html entities found within `$value` will be double-encoded or not (cyphix333)
- Enh #13074: Improved `\yii\log\SyslogTarget` with `$options` to be able to change the default `openlog` options. (timbeks)
- Enh: Added constants for specifying `yii\validators\CompareValidator::$type` (cebe)


Expand Down
19 changes: 18 additions & 1 deletion framework/log/SyslogTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class SyslogTarget extends Target
* @var int syslog facility.
*/
public $facility = LOG_USER;
/**
* @var int openlog options. This is a bitfield passed as the `$option` parameter to [openlog()](http://php.net/openlog).
* Defaults to `null` which means to use the default options `LOG_ODELAY | LOG_PID`.
* @see http://php.net/openlog for available options.
* @since 2.0.11
*/
public $options;

/**
* @var array syslog levels
Expand All @@ -40,13 +47,23 @@ class SyslogTarget extends Target
Logger::LEVEL_ERROR => LOG_ERR,
];

/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->options === null) {
$this->options = LOG_ODELAY | LOG_PID;
}
}

/**
* Writes log messages to syslog
*/
public function export()
{
openlog($this->identity, LOG_ODELAY | LOG_PID, $this->facility);
openlog($this->identity, $this->options, $this->facility);
foreach ($this->messages as $message) {
syslog($this->_syslogLevels[$message[1]], $this->formatMessage($message));
}
Expand Down
4 changes: 3 additions & 1 deletion tests/framework/log/SyslogTargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected function setUp()
public function testExport()
{
$identity = 'identity string';
$options = LOG_ODELAY | LOG_PID;
$facility = 'facility string';
$messages = [
['info message', Logger::LEVEL_INFO],
Expand All @@ -73,14 +74,15 @@ public function testExport()
->getMock('yii\\log\\SyslogTarget', ['openlog', 'syslog', 'formatMessage', 'closelog']);

$syslogTarget->identity = $identity;
$syslogTarget->options = $options;
$syslogTarget->facility = $facility;
$syslogTarget->messages = $messages;

$syslogTarget->expects($this->once())
->method('openlog')
->with(
$this->equalTo($identity),
$this->equalTo(LOG_ODELAY | LOG_PID),
$this->equalTo($options),
$this->equalTo($facility)
);

Expand Down

0 comments on commit f23dc5a

Please sign in to comment.