Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for Monolog 2 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# .travis.yml

language: php
dist: bionic
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.2
- 7.3
- 7.4
- 8.0

# Install dependencies before testing
before_script:
- composer selfupdate
- composer install --dev
- composer install

# Use our custom version of phpunit
script:
Expand All @@ -19,4 +20,4 @@ script:

# Run coveralls.io code coverage report
after_script:
- vendor/bin/coveralls -v
- vendor/bin/php-coveralls -v
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=7.2"
},
"conflict": {
"monolog/monolog": "<2.0"
},
"require-dev": {
"monolog/monolog": "~1.0",
"phpunit/phpunit": "~4.0",
"satooshi/php-coveralls": "dev-master"
"monolog/monolog": "^2.0",
"phpunit/phpunit": "^8.5",
"php-coveralls/php-coveralls": "^2.4"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<testsuites>
<testsuite name="monolog-splunk-formatter Unit Tests">
<directory phpVersion="5.3.0" phpVersionOperator=">=">test/unit</directory>
<directory>test</directory>
</testsuite>
</testsuites>

Expand All @@ -19,8 +19,8 @@
</filter>

<logging>
<log type="coverage-html" target="build/coverage" charset="UTF-8" highlight="true" lowUpperBound="50" highUpperBound="90"/>
<log type="coverage-html" target="build/coverage" lowUpperBound="50" highLowerBound="90"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

</phpunit>
</phpunit>
37 changes: 17 additions & 20 deletions src/SplunkLineFormatter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
<?php declare(strict_types=1);

/*
* @copyright 2015 Vubeology, Inc.
* @license MIT
Expand All @@ -15,18 +16,18 @@
*/
class SplunkLineFormatter extends LineFormatter
{
const SPLUNK_FORMAT = "%datetime% %channel%.%level_name% L=%level% %message% %context% %extra%\n";
public const SPLUNK_FORMAT = "%datetime% %channel%.%level_name% L=%level% %message% %context% %extra%\n";

protected $quoteReplacement;

/**
* @param string $format The format of the message
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
* @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
* @param bool $ignoreEmptyContextAndExtra
* @param string|null $format The format of the message
* @param string|null $dateFormat The format of the timestamp: one supported by DateTime::format
* @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
* @param bool $ignoreEmptyContextAndExtra
* @param string $quoteReplacement
*/
public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false,
$quoteReplacement = '^')
public function __construct(?string $format = null, ?string $dateFormat = null, bool $allowInlineLineBreaks = false, bool $ignoreEmptyContextAndExtra = false, string $quoteReplacement = '^')
{
if($format === null) {
$format = self::SPLUNK_FORMAT;
Expand All @@ -40,29 +41,25 @@ public function __construct($format = null, $dateFormat = null, $allowInlineLine
parent::__construct($format, $dateFormat, $allowInlineLineBreaks, $ignoreEmptyContextAndExtra);
}

public function setQuoteReplacement($quoteReplacement)
public function setQuoteReplacement($quoteReplacement): void
{
$this->quoteReplacement = $quoteReplacement;
}

protected function jsonEncode($data)
protected function jsonEncode($data): string
{
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return $this->toJson($data, true);
}

return str_replace('\\/', '/', @json_encode($data));
return $this->toJson($data, true);
}

/**
* Public so we can run unit tests against it
*/
public function publicConvertToString($data)
public function publicConvertToString($data): string
{
return $this->convertToString($data);
}

protected function convertToString($data)
protected function convertToString($data): string
{
if (null === $data || is_bool($data)) {
return var_export($data, true);
Expand All @@ -74,7 +71,7 @@ protected function convertToString($data)

if (is_array($data)) {

$vals = array();
$vals = [];

foreach ($data as $n => $v) {
if (null === $v || is_bool($v)) {
Expand Down Expand Up @@ -103,9 +100,9 @@ protected function convertToString($data)
return $this->jsonEncode($data);
}

protected function toQuoteSafeString($string)
protected function toQuoteSafeString($string): string
{
return str_replace('"', $this->quoteReplacement, (string) $string);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

use PHPUnit\Framework\TestCase;
use \Vube\Monolog\Formatter\SplunkLineFormatter;
use \Monolog\Logger;
use \Monolog\Handler\TestHandler;


class SplunkLineFormatterTest extends \PHPUnit_Framework_TestCase
class SplunkLineFormatterTest extends TestCase
{
private $log;
private $handler;
private $slf;

protected function setUp()
protected function setUp(): void
{
$this->slf = new SplunkLineFormatter();

Expand Down Expand Up @@ -153,7 +153,7 @@ public function testLogFormat()
$context = array(
'a' => 'A A',
);
$this->log->addNotice('message here', $context);
$this->log->notice('message here', $context);
$records = $this->handler->getRecords();
$this->assertSame(1, count($records));
$message = $records[0]['formatted'];
Expand All @@ -166,7 +166,7 @@ public function testLogFormatWithQuotesInMessage()
$context = array(
'a' => 'A A',
);
$this->log->addWarning('message "with quotes"', $context);
$this->log->warning('message "with quotes"', $context);
$records = $this->handler->getRecords();
$this->assertSame(1, count($records));
$message = $records[0]['formatted'];
Expand All @@ -181,7 +181,7 @@ public function testLogWithAssocArrayInContext()
'ca' => 'CA',
),
);
$this->log->addWarning('message', $context);
$this->log->warning('message', $context);
$records = $this->handler->getRecords();
$this->assertSame(1, count($records));
$message = $records[0]['formatted'];
Expand Down