-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathLogger.php
91 lines (82 loc) · 2.2 KB
/
Logger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* Copyright © Bazaarvoice, Inc. All rights reserved.
* See LICENSE.md for license details.
*/
declare(strict_types=1);
namespace Bazaarvoice\Connector\Logger;
use Bazaarvoice\Connector\Api\ConfigProviderInterface;
use Exception;
use Magento\Framework\App\State;
use Monolog\DateTimeImmutable;
/**
* Class Logger
*
* @package Bazaarvoice\Connector\Logger
*/
class Logger extends \Monolog\Logger
{
/**
* @var bool
*/
protected $admin = false;
/**
* @var ConfigProviderInterface
*/
private $configProvider;
/**
* Logger constructor.
*
* @param string $name
* @param ConfigProviderInterface $configProvider
* @param State $state
*
* @param array|\Monolog\Handler\HandlerInterface[] $handlers
*
* @codingStandardsIgnoreStart
*/
public function __construct(
$name,
ConfigProviderInterface $configProvider,
State $state,
array $handlers = array()
) {
try {
$this->admin = $state->getAreaCode() === 'adminhtml';
} catch (Exception $e) {
}
parent::__construct($name, $handlers);
$this->configProvider = $configProvider;
/** @codingStandardsIgnoreEnd */
}
/**
* @param string|array $message
* @param array $context
*
* @return bool
*/
public function debug($message, array $context = []): void
{
if ($this->configProvider->isDebugEnabled()) {
$this->addRecord(static::DEBUG, $message, $context);
}
}
/**
* @param int $level
* @param string $message
* @param array $context
* @param DateTimeImmutable $datetime
*
* @return bool
*/
public function addRecord($level, $message, array $context = [], DateTimeImmutable $datetime = null): bool
{
if (is_array($message)) {
$message = print_r($message, $return = true);
}
if (php_sapi_name() == "cli" || $this->admin) {
print_r($message."\n");
}
return parent::addRecord($level, $message, $context, $datetime);
}
}