mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-12 08:04:02 +02:00
Merge branch 'master' into feature/elasticsearch
# Conflicts: # composer.json # src/Monolog/Handler/ElasticSearchHandler.php
This commit is contained in:
@@ -153,4 +153,9 @@ class TestChromePHPHandler extends ChromePHPHandler
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
protected function isWebRequest(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -201,6 +201,7 @@ class ElasticSearchHandlerTest extends TestCase
|
||||
->setHosts($hosts)
|
||||
->build();
|
||||
$handler = new ElasticSearchHandler($client, $this->options);
|
||||
|
||||
try {
|
||||
$handler->handleBatch([$msg]);
|
||||
} catch (\RuntimeException $e) {
|
||||
|
@@ -32,7 +32,7 @@ class ErrorLogHandlerTest extends TestCase
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedExceptionMessage The given message type "42" is not supported
|
||||
*/
|
||||
public function testShouldNotAcceptAnInvalidTypeOnContructor()
|
||||
public function testShouldNotAcceptAnInvalidTypeOnConstructor()
|
||||
{
|
||||
new ErrorLogHandler(42);
|
||||
}
|
||||
|
@@ -146,7 +146,10 @@ class FilterHandlerTest extends TestCase
|
||||
$handler = new FilterHandler(
|
||||
function ($record, $handler) use ($test) {
|
||||
return $test;
|
||||
}, Logger::INFO, Logger::NOTICE, false
|
||||
},
|
||||
Logger::INFO,
|
||||
Logger::NOTICE,
|
||||
false
|
||||
);
|
||||
$handler->handle($this->getRecord(Logger::DEBUG));
|
||||
$handler->handle($this->getRecord(Logger::INFO));
|
||||
|
@@ -93,4 +93,9 @@ class TestFirePHPHandler extends FirePHPHandler
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
protected function isWebRequest(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
79
tests/Monolog/Handler/InsightOpsHandlerTest.php
Normal file
79
tests/Monolog/Handler/InsightOpsHandlerTest.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Test\TestCase;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* @author Robert Kaufmann III <rok3@rok3.me>
|
||||
* @author Gabriel Machado <gabriel.ms1@hotmail.com>
|
||||
*/
|
||||
class InsightOpsHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
/**
|
||||
* @var LogEntriesHandler
|
||||
*/
|
||||
private $handler;
|
||||
|
||||
public function testWriteContent()
|
||||
{
|
||||
$this->createHandler();
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test'));
|
||||
|
||||
fseek($this->resource, 0);
|
||||
$content = fread($this->resource, 1024);
|
||||
|
||||
$this->assertRegexp('/testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] test.CRITICAL: Critical write test/', $content);
|
||||
}
|
||||
|
||||
public function testWriteBatchContent()
|
||||
{
|
||||
$this->createHandler();
|
||||
$this->handler->handleBatch($this->getMultipleRecords());
|
||||
|
||||
fseek($this->resource, 0);
|
||||
$content = fread($this->resource, 1024);
|
||||
|
||||
$this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] .* \[\] \[\]\n){3}/', $content);
|
||||
}
|
||||
|
||||
private function createHandler()
|
||||
{
|
||||
$useSSL = extension_loaded('openssl');
|
||||
$args = array('testToken', 'us', $useSSL, Logger::DEBUG, true);
|
||||
$this->resource = fopen('php://memory', 'a');
|
||||
$this->handler = $this->getMockBuilder(InsightOpsHandler::class)
|
||||
->setMethods(array('fsockopen', 'streamSetTimeout', 'closeSocket'))
|
||||
->setConstructorArgs($args)
|
||||
->getMock();
|
||||
|
||||
$reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$reflectionProperty->setValue($this->handler, 'localhost:1234');
|
||||
|
||||
$this->handler->expects($this->any())
|
||||
->method('fsockopen')
|
||||
->will($this->returnValue($this->resource));
|
||||
$this->handler->expects($this->any())
|
||||
->method('streamSetTimeout')
|
||||
->will($this->returnValue(true));
|
||||
$this->handler->expects($this->any())
|
||||
->method('closeSocket')
|
||||
->will($this->returnValue(true));
|
||||
}
|
||||
}
|
@@ -37,7 +37,7 @@ class LogmaticHandlerTest extends TestCase
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$this->assertRegexp('/testToken {"message":"Critical write test","context":\[\],"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":\[\],"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
|
||||
$this->assertRegexp('/testToken {"message":"Critical write test","context":{},"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
|
||||
}
|
||||
|
||||
public function testWriteBatchContent()
|
||||
@@ -53,7 +53,7 @@ class LogmaticHandlerTest extends TestCase
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$this->assertRegexp('/testToken {"message":"test","context":\[\],"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":\[\],"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
|
||||
$this->assertRegexp('/testToken {"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
|
||||
}
|
||||
|
||||
private function createHandler()
|
||||
|
@@ -77,7 +77,7 @@ class ProcessHandlerTest extends TestCase
|
||||
*/
|
||||
public function testConstructWithInvalidCommandThrowsInvalidArgumentException($invalidCommand, $expectedExcep)
|
||||
{
|
||||
$this->setExpectedException($expectedExcep);
|
||||
$this->expectException($expectedExcep);
|
||||
new ProcessHandler($invalidCommand, Logger::DEBUG);
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ class ProcessHandlerTest extends TestCase
|
||||
*/
|
||||
public function testConstructWithInvalidCwdThrowsInvalidArgumentException($invalidCwd, $expectedExcep)
|
||||
{
|
||||
$this->setExpectedException($expectedExcep);
|
||||
$this->expectException($expectedExcep);
|
||||
new ProcessHandler(self::DUMMY_COMMAND, Logger::DEBUG, true, $invalidCwd);
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ class ProcessHandlerTest extends TestCase
|
||||
->method('selectErrorStream')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$this->setExpectedException('\UnexpectedValueException');
|
||||
$this->expectException('\UnexpectedValueException');
|
||||
/** @var ProcessHandler $handler */
|
||||
$handler->handle($this->getRecord(Logger::WARNING, 'stream failing, whoops'));
|
||||
}
|
||||
@@ -147,7 +147,7 @@ class ProcessHandlerTest extends TestCase
|
||||
public function testStartupWithErrorsThrowsUnexpectedValueException()
|
||||
{
|
||||
$handler = new ProcessHandler('>&2 echo "some fake error message"');
|
||||
$this->setExpectedException('\UnexpectedValueException');
|
||||
$this->expectException('\UnexpectedValueException');
|
||||
$handler->handle($this->getRecord(Logger::WARNING, 'some warning in the house'));
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ class ProcessHandlerTest extends TestCase
|
||||
->method('readProcessErrors')
|
||||
->willReturnOnConsecutiveCalls('', $this->returnValue('some fake error message here'));
|
||||
|
||||
$this->setExpectedException('\UnexpectedValueException');
|
||||
$this->expectException('\UnexpectedValueException');
|
||||
/** @var ProcessHandler $handler */
|
||||
$handler->handle($this->getRecord(Logger::WARNING, 'some test stuff'));
|
||||
}
|
||||
|
@@ -15,6 +15,7 @@ use Exception;
|
||||
use Monolog\Test\TestCase;
|
||||
use Monolog\Logger;
|
||||
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
||||
use Rollbar\RollbarLogger;
|
||||
|
||||
/**
|
||||
* @author Erik Johansson <erik.pm.johansson@gmail.com>
|
||||
@@ -27,7 +28,7 @@ class RollbarHandlerTest extends TestCase
|
||||
/**
|
||||
* @var MockObject
|
||||
*/
|
||||
private $rollbarNotifier;
|
||||
private $rollbarLogger;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
@@ -38,7 +39,7 @@ class RollbarHandlerTest extends TestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->setupRollbarNotifierMock();
|
||||
$this->setupRollbarLoggerMock();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,15 +55,21 @@ class RollbarHandlerTest extends TestCase
|
||||
$this->assertEquals('debug', $this->reportedExceptionArguments['payload']['level']);
|
||||
}
|
||||
|
||||
private function setupRollbarNotifierMock()
|
||||
private function setupRollbarLoggerMock()
|
||||
{
|
||||
$this->rollbarNotifier = $this->getMockBuilder('RollbarNotifier')
|
||||
->setMethods(array('report_message', 'report_exception', 'flush'))
|
||||
$config = array(
|
||||
'access_token' => 'ad865e76e7fb496fab096ac07b1dbabb',
|
||||
'environment' => 'test',
|
||||
);
|
||||
|
||||
$this->rollbarLogger = $this->getMockBuilder(RollbarLogger::class)
|
||||
->setConstructorArgs(array($config))
|
||||
->setMethods(array('log'))
|
||||
->getMock();
|
||||
|
||||
$this->rollbarNotifier
|
||||
$this->rollbarLogger
|
||||
->expects($this->any())
|
||||
->method('report_exception')
|
||||
->method('log')
|
||||
->willReturnCallback(function ($exception, $context, $payload) {
|
||||
$this->reportedExceptionArguments = compact('exception', 'context', 'payload');
|
||||
});
|
||||
@@ -70,7 +77,7 @@ class RollbarHandlerTest extends TestCase
|
||||
|
||||
private function createHandler(): RollbarHandler
|
||||
{
|
||||
return new RollbarHandler($this->rollbarNotifier, Logger::DEBUG);
|
||||
return new RollbarHandler($this->rollbarLogger, Logger::DEBUG);
|
||||
}
|
||||
|
||||
private function createExceptionRecord($level = Logger::DEBUG, $message = 'test', $exception = null): array
|
||||
|
@@ -102,10 +102,10 @@ class RotatingFileHandlerTest extends TestCase
|
||||
$dayCallback = function ($ago) use ($now) {
|
||||
return $now + 86400 * $ago;
|
||||
};
|
||||
$monthCallback = function($ago) {
|
||||
$monthCallback = function ($ago) {
|
||||
return gmmktime(0, 0, 0, (int) (date('n') + $ago), 1, (int) date('Y'));
|
||||
};
|
||||
$yearCallback = function($ago) {
|
||||
$yearCallback = function ($ago) {
|
||||
return gmmktime(0, 0, 0, 1, 1, (int) (date('Y') + $ago));
|
||||
};
|
||||
|
||||
@@ -134,7 +134,8 @@ class RotatingFileHandlerTest extends TestCase
|
||||
{
|
||||
$handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
|
||||
if (!$valid) {
|
||||
$this->setExpectedExceptionRegExp(InvalidArgumentException::class, '~^Invalid date format~');
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessageRegExp('~^Invalid date format~');
|
||||
}
|
||||
$handler->setFilenameFormat('{filename}-{date}', $dateFormat);
|
||||
$this->assertTrue(true);
|
||||
@@ -174,7 +175,8 @@ class RotatingFileHandlerTest extends TestCase
|
||||
{
|
||||
$handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
|
||||
if (!$valid) {
|
||||
$this->setExpectedExceptionRegExp(InvalidArgumentException::class, '~^Invalid filename format~');
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessageRegExp('~^Invalid filename format~');
|
||||
}
|
||||
|
||||
$handler->setFilenameFormat($filenameFormat, RotatingFileHandler::FILE_PER_DAY);
|
||||
@@ -193,6 +195,39 @@ class RotatingFileHandlerTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider rotationWhenSimilarFilesExistTests
|
||||
*/
|
||||
public function testRotationWhenSimilarFileNamesExist($dateFormat)
|
||||
{
|
||||
touch($old1 = __DIR__.'/Fixtures/foo-foo-'.date($dateFormat).'.rot');
|
||||
touch($old2 = __DIR__.'/Fixtures/foo-bar-'.date($dateFormat).'.rot');
|
||||
|
||||
$log = __DIR__.'/Fixtures/foo-'.date($dateFormat).'.rot';
|
||||
|
||||
$handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2);
|
||||
$handler->setFormatter($this->getIdentityFormatter());
|
||||
$handler->setFilenameFormat('{filename}-{date}', $dateFormat);
|
||||
$handler->handle($this->getRecord());
|
||||
$handler->close();
|
||||
|
||||
$this->assertTrue(file_exists($log));
|
||||
}
|
||||
|
||||
public function rotationWhenSimilarFilesExistTests()
|
||||
{
|
||||
return array(
|
||||
'Rotation is triggered when the file of the current day is not present but similar exists'
|
||||
=> array(RotatingFileHandler::FILE_PER_DAY),
|
||||
|
||||
'Rotation is triggered when the file of the current month is not present but similar exists'
|
||||
=> array(RotatingFileHandler::FILE_PER_MONTH),
|
||||
|
||||
'Rotation is triggered when the file of the current year is not present but similar exists'
|
||||
=> array(RotatingFileHandler::FILE_PER_YEAR),
|
||||
);
|
||||
}
|
||||
|
||||
public function testReuseCurrentFile()
|
||||
{
|
||||
$log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot';
|
||||
|
@@ -324,12 +324,12 @@ class SlackRecordTest extends TestCase
|
||||
'short' => false,
|
||||
),
|
||||
array(
|
||||
'title' => 'tags',
|
||||
'title' => 'Tags',
|
||||
'value' => sprintf('```%s```', json_encode($extra['tags'])),
|
||||
'short' => false,
|
||||
),
|
||||
array(
|
||||
'title' => 'test',
|
||||
'title' => 'Test',
|
||||
'value' => $context['test'],
|
||||
'short' => false,
|
||||
),
|
||||
@@ -357,6 +357,14 @@ class SlackRecordTest extends TestCase
|
||||
$this->assertSame($record['datetime']->getTimestamp(), $attachment['ts']);
|
||||
}
|
||||
|
||||
public function testContextHasException()
|
||||
{
|
||||
$record = $this->getRecord(Logger::CRITICAL, 'This is a critical message.', array('exception' => new \Exception()));
|
||||
$slackRecord = new SlackRecord(null, null, true, null, false, true);
|
||||
$data = $slackRecord->getSlackData($record);
|
||||
$this->assertInternalType('string', $data['attachments'][0]['fields'][1]['value']);
|
||||
}
|
||||
|
||||
public function testExcludeExtraAndContextFields()
|
||||
{
|
||||
$record = $this->getRecord(
|
||||
@@ -372,12 +380,12 @@ class SlackRecordTest extends TestCase
|
||||
|
||||
$expected = array(
|
||||
array(
|
||||
'title' => 'info',
|
||||
'title' => 'Info',
|
||||
'value' => sprintf('```%s```', json_encode(array('author' => 'Jordi'), $this->jsonPrettyPrintFlag)),
|
||||
'short' => false,
|
||||
),
|
||||
array(
|
||||
'title' => 'tags',
|
||||
'title' => 'Tags',
|
||||
'value' => sprintf('```%s```', json_encode(array('web'))),
|
||||
'short' => false,
|
||||
),
|
||||
|
@@ -77,6 +77,13 @@ class SocketHandlerTest extends TestCase
|
||||
$this->assertEquals(10.25, $this->handler->getWritingTimeout());
|
||||
}
|
||||
|
||||
public function testSetChunkSize()
|
||||
{
|
||||
$this->createHandler('localhost:1234');
|
||||
$this->handler->setChunkSize(1025);
|
||||
$this->assertEquals(1025, $this->handler->getChunkSize());
|
||||
}
|
||||
|
||||
public function testSetConnectionString()
|
||||
{
|
||||
$this->createHandler('tcp://localhost:9090');
|
||||
@@ -120,6 +127,19 @@ class SocketHandlerTest extends TestCase
|
||||
$this->writeRecord('Hello world');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException UnexpectedValueException
|
||||
*/
|
||||
public function testExceptionIsThrownIfCannotSetChunkSize()
|
||||
{
|
||||
$this->setMockHandler(array('streamSetChunkSize'));
|
||||
$this->handler->setChunkSize(8192);
|
||||
$this->handler->expects($this->once())
|
||||
->method('streamSetChunkSize')
|
||||
->will($this->returnValue(false));
|
||||
$this->writeRecord('Hello world');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
@@ -277,7 +297,7 @@ class SocketHandlerTest extends TestCase
|
||||
{
|
||||
$this->res = fopen('php://memory', 'a');
|
||||
|
||||
$defaultMethods = ['fsockopen', 'pfsockopen', 'streamSetTimeout'];
|
||||
$defaultMethods = ['fsockopen', 'pfsockopen', 'streamSetTimeout', 'streamSetChunkSize'];
|
||||
$newMethods = array_diff($methods, $defaultMethods);
|
||||
|
||||
$finalMethods = array_merge($defaultMethods, $newMethods);
|
||||
@@ -305,6 +325,12 @@ class SocketHandlerTest extends TestCase
|
||||
->will($this->returnValue(true));
|
||||
}
|
||||
|
||||
if (!in_array('streamSetChunkSize', $methods)) {
|
||||
$this->handler->expects($this->any())
|
||||
->method('streamSetChunkSize')
|
||||
->will($this->returnValue(8192));
|
||||
}
|
||||
|
||||
$this->handler->setFormatter($this->getIdentityFormatter());
|
||||
}
|
||||
}
|
||||
|
@@ -54,6 +54,54 @@ class TestHandlerTest extends TestCase
|
||||
$this->assertEquals([$record], $records);
|
||||
}
|
||||
|
||||
public function testHandlerAssertEmptyContext()
|
||||
{
|
||||
$handler = new TestHandler;
|
||||
$record = $this->getRecord(Logger::WARNING, 'test', []);
|
||||
$this->assertFalse($handler->hasWarning([
|
||||
'message' => 'test',
|
||||
'context' => [],
|
||||
]));
|
||||
|
||||
$handler->handle($record);
|
||||
|
||||
$this->assertTrue($handler->hasWarning([
|
||||
'message' => 'test',
|
||||
'context' => [],
|
||||
]));
|
||||
$this->assertFalse($handler->hasWarning([
|
||||
'message' => 'test',
|
||||
'context' => [
|
||||
'foo' => 'bar',
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
public function testHandlerAssertNonEmptyContext()
|
||||
{
|
||||
$handler = new TestHandler;
|
||||
$record = $this->getRecord(Logger::WARNING, 'test', ['foo' => 'bar']);
|
||||
$this->assertFalse($handler->hasWarning([
|
||||
'message' => 'test',
|
||||
'context' => [
|
||||
'foo' => 'bar',
|
||||
],
|
||||
]));
|
||||
|
||||
$handler->handle($record);
|
||||
|
||||
$this->assertTrue($handler->hasWarning([
|
||||
'message' => 'test',
|
||||
'context' => [
|
||||
'foo' => 'bar',
|
||||
],
|
||||
]));
|
||||
$this->assertFalse($handler->hasWarning([
|
||||
'message' => 'test',
|
||||
'context' => [],
|
||||
]));
|
||||
}
|
||||
|
||||
public function methodProvider()
|
||||
{
|
||||
return [
|
||||
|
@@ -87,6 +87,29 @@ class WhatFailureGroupHandlerTest extends TestCase
|
||||
$this->assertTrue($records[0]['extra']['foo']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Monolog\Handler\WhatFailureGroupHandler::handleBatch
|
||||
*/
|
||||
public function testHandleBatchUsesProcessors()
|
||||
{
|
||||
$testHandlers = array(new TestHandler(), new TestHandler());
|
||||
$handler = new WhatFailureGroupHandler($testHandlers);
|
||||
$handler->pushProcessor(function ($record) {
|
||||
$record['extra']['foo'] = true;
|
||||
|
||||
return $record;
|
||||
});
|
||||
$handler->handleBatch(array($this->getRecord(Logger::DEBUG), $this->getRecord(Logger::INFO)));
|
||||
foreach ($testHandlers as $test) {
|
||||
$this->assertTrue($test->hasDebugRecords());
|
||||
$this->assertTrue($test->hasInfoRecords());
|
||||
$this->assertTrue(count($test->getRecords()) === 2);
|
||||
$records = $test->getRecords();
|
||||
$this->assertTrue($records[0]['extra']['foo']);
|
||||
$this->assertTrue($records[1]['extra']['foo']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Monolog\Handler\WhatFailureGroupHandler::handle
|
||||
*/
|
||||
|
Reference in New Issue
Block a user