Skip to content

Commit

Permalink
Implement disableTimestamp, enableLocking and prefixString
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Mar 4, 2020
1 parent 4671773 commit a9198c0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ return [
(Since 1.2.0)
* (*string|null*) `$replaceNewline` a string that should replace all newline characters in a log message.
Default ist `null` for no replacement. (Since 1.1.0)
* (*bool*) `$disableTimestamp` whether to omit the timestamp prefix. The default is `false` which
will prepend every message with a timestamp generated by `yii\log\Target::getTime()`.
* (*string*) `$prefixString` a string that will be prefixed to every message at the first position
(even before the timestamp - if not disabled). Default is an empty string.
* (*bool*) `$enableLocking` whether enable locking with `flock()` on the target. Note, that this is not
supported by all stream types. Default is `false`.

60 changes: 49 additions & 11 deletions src/Target.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace codemix\streamlog;

use yii\log\LogRuntimeException;
use yii\log\Target as BaseTarget;
use yii\base\InvalidConfigException;

Expand All @@ -21,8 +22,28 @@ class Target extends BaseTarget
*/
public $replaceNewline;

protected $fp;
/**
* @var bool whether to disable the timestamp. The default is `false` which
* will prepend every message with a timestamp created with
* [yii\log\Target::getTime()].
*/
public $disableTimestamp = false;

/**
* @var bool whether to use flock() to lock/unlock the stream before/after
* writing. This can be used to ensure that the stream is written by 2
* processes simultaniously. Note though, that not all stream types support
* locking. The default is `false`.
*/
public $enableLocking = false;

/**
* @var string a string to prepend to all messages. The string will be
* added to the very beginning (even before the timestamp).
*/
public $prefixString = '';

protected $fp;
protected $openedFp = false;

/**
Expand All @@ -31,7 +52,7 @@ class Target extends BaseTarget
public function __destruct()
{
if ($this->openedFp) {
fclose($this->fp);
@fclose($this->fp);
}
}

Expand Down Expand Up @@ -66,9 +87,9 @@ public function setFp($value)
*/
public function getFp()
{
if ($this->fp===null) {
if ($this->fp === null) {
$this->fp = @fopen($this->url,'w');
if ($this->fp===false) {
if ($this->fp === false) {
throw new InvalidConfigException("Unable to open '{$this->url}' for writing.");
}
$this->openedFp = true;
Expand All @@ -79,23 +100,40 @@ public function getFp()
/**
* Writes a log message to the given target URL
* @throws InvalidConfigException if unable to open the stream for writing
* @throws LogRuntimeException if unable to write log
*/
public function export()
{
$text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";
fwrite($this->getFp(), $text);
$fp = $this->getFp();
if ($this->enableLocking) {
@flock($fp, LOCK_EX);
}
if (@fwrite($fp, $text) === false) {
$error = error_get_last();
throw new LogRuntimeException("Unable to export log!: {$error['message']}");
}
if ($this->enableLocking) {
@flock($fp, LOCK_UN);
}
}

/**
* @inheritdoc
*/
public function formatMessage($message)
{
$text = parent::formatMessage($message);
if ($this->replaceNewline===null) {
return $text;
} else {
return str_replace("\n", $this->replaceNewline, $text);
}
$text = $this->prefixString . trim(parent::formatMessage($message));
return $this->replaceNewline === null ?
$text :
str_replace("\n", $this->replaceNewline, $text);
}

/**
* @inheritdoc
*/
protected function getTime($timestamp)
{
return $this->disableTimestamp ? '' : parent::getTime($timestamp);
}
}

0 comments on commit a9198c0

Please sign in to comment.