1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-07-31 02:10:22 +02:00

Check if monolog runs in web context (#1024)

* Check if monolog runs in web context
This commit is contained in:
Pascal
2018-06-11 17:36:34 +02:00
committed by Jordi Boggiano
parent ff5ae5cead
commit 5bb1c5fb4f
5 changed files with 41 additions and 1 deletions

View File

@@ -24,6 +24,9 @@ use Monolog\Logger;
*/
class ChromePHPHandler extends AbstractProcessingHandler
{
use WebRequestRecognizerTrait;
/**
* Version of the extension
*/
@@ -75,6 +78,10 @@ class ChromePHPHandler extends AbstractProcessingHandler
*/
public function handleBatch(array $records)
{
if (!$this->isWebRequest()) {
return;
}
$messages = [];
foreach ($records as $record) {
@@ -108,6 +115,10 @@ class ChromePHPHandler extends AbstractProcessingHandler
*/
protected function write(array $record)
{
if (!$this->isWebRequest()) {
return;
}
self::$json['rows'][] = $record['formatted'];
$this->send();

View File

@@ -21,6 +21,9 @@ use Monolog\Formatter\FormatterInterface;
*/
class FirePHPHandler extends AbstractProcessingHandler
{
use WebRequestRecognizerTrait;
/**
* WildFire JSON header message format
*/
@@ -130,7 +133,7 @@ class FirePHPHandler extends AbstractProcessingHandler
*/
protected function write(array $record)
{
if (!self::$sendHeaders) {
if (!self::$sendHeaders || !$this->isWebRequest()) {
return;
}

View File

@@ -0,0 +1,16 @@
<?php declare(strict_types=1);
namespace Monolog\Handler;
trait WebRequestRecognizerTrait
{
/**
* Checks if PHP's serving a web request
* @return bool
*/
protected function isWebRequest(): bool
{
return 'cli' !== \PHP_SAPI && 'phpdbg' !== \PHP_SAPI;
}
}

View File

@@ -153,4 +153,9 @@ class TestChromePHPHandler extends ChromePHPHandler
{
return $this->headers;
}
protected function isWebRequest(): bool
{
return true;
}
}

View File

@@ -93,4 +93,9 @@ class TestFirePHPHandler extends FirePHPHandler
{
return $this->headers;
}
protected function isWebRequest(): bool
{
return true;
}
}