ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
NewRelicHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
14 use Monolog\Logger;
16 
27 {
33  protected $appName;
34 
40  protected $transactionName;
41 
48  protected $explodeArrays;
49 
57  public function __construct(
59  $bubble = true,
60  $appName = null,
61  $explodeArrays = false,
62  $transactionName = null
63  ) {
64  parent::__construct($level, $bubble);
65 
66  $this->appName = $appName;
67  $this->explodeArrays = $explodeArrays;
68  $this->transactionName = $transactionName;
69  }
70 
74  protected function write(array $record)
75  {
76  if (!$this->isNewRelicEnabled()) {
77  throw new MissingExtensionException('The newrelic PHP extension is required to use the NewRelicHandler');
78  }
79 
80  if ($appName = $this->getAppName($record['context'])) {
82  }
83 
84  if ($transactionName = $this->getTransactionName($record['context'])) {
86  unset($record['formatted']['context']['transaction_name']);
87  }
88 
89  if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) {
90  newrelic_notice_error($record['message'], $record['context']['exception']);
91  unset($record['formatted']['context']['exception']);
92  } else {
93  newrelic_notice_error($record['message']);
94  }
95 
96  if (isset($record['formatted']['context']) && is_array($record['formatted']['context'])) {
97  foreach ($record['formatted']['context'] as $key => $parameter) {
98  if (is_array($parameter) && $this->explodeArrays) {
99  foreach ($parameter as $paramKey => $paramValue) {
100  $this->setNewRelicParameter('context_' . $key . '_' . $paramKey, $paramValue);
101  }
102  } else {
103  $this->setNewRelicParameter('context_' . $key, $parameter);
104  }
105  }
106  }
107 
108  if (isset($record['formatted']['extra']) && is_array($record['formatted']['extra'])) {
109  foreach ($record['formatted']['extra'] as $key => $parameter) {
110  if (is_array($parameter) && $this->explodeArrays) {
111  foreach ($parameter as $paramKey => $paramValue) {
112  $this->setNewRelicParameter('extra_' . $key . '_' . $paramKey, $paramValue);
113  }
114  } else {
115  $this->setNewRelicParameter('extra_' . $key, $parameter);
116  }
117  }
118  }
119  }
120 
126  protected function isNewRelicEnabled()
127  {
128  return extension_loaded('newrelic');
129  }
130 
138  protected function getAppName(array $context)
139  {
140  if (isset($context['appname'])) {
141  return $context['appname'];
142  }
143 
144  return $this->appName;
145  }
146 
155  protected function getTransactionName(array $context)
156  {
157  if (isset($context['transaction_name'])) {
158  return $context['transaction_name'];
159  }
160 
161  return $this->transactionName;
162  }
163 
169  protected function setNewRelicAppName($appName)
170  {
172  }
173 
180  {
182  }
183 
188  protected function setNewRelicParameter($key, $value)
189  {
190  if (null === $value || is_scalar($value)) {
192  } else {
193  newrelic_add_custom_parameter($key, @json_encode($value));
194  }
195  }
196 
200  protected function getDefaultFormatter()
201  {
202  return new NormalizerFormatter();
203  }
204 }
setNewRelicTransactionName($transactionName)
Overwrites the name of the current transaction.
setNewRelicAppName($appName)
Sets the NewRelic application that should receive this log.
const ERROR
Runtime errors.
Definition: Logger.php:58
$context
Definition: webdav.php:25
getTransactionName(array $context)
Returns the name of the current transaction.
Base Handler class providing the Handler structure.
__construct( $level=Logger::ERROR, $bubble=true, $appName=null, $explodeArrays=false, $transactionName=null)
getAppName(array $context)
Returns the appname where this log should be sent.
Class to record a log on a NewRelic application.
newrelic_add_custom_parameter($key, $value)
Exception can be thrown if an extension for an handler is missing.
Normalizes incoming records to remove objects/resources so it&#39;s easier to dump to various targets...
newrelic_name_transaction($transactionName)
isNewRelicEnabled()
Checks whether the NewRelic extension is enabled in the system.
newrelic_set_appname($appname)
$key
Definition: croninfo.php:18