ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
25 {
31  protected $appName;
32 
38  protected $transactionName;
39 
46  protected $explodeArrays;
47 
55  public function __construct(
57  $bubble = true,
58  $appName = null,
59  $explodeArrays = false,
60  $transactionName = null
61  ) {
62  parent::__construct($level, $bubble);
63 
64  $this->appName = $appName;
65  $this->explodeArrays = $explodeArrays;
66  $this->transactionName = $transactionName;
67  }
68 
72  protected function write(array $record)
73  {
74  if (!$this->isNewRelicEnabled()) {
75  throw new MissingExtensionException('The newrelic PHP extension is required to use the NewRelicHandler');
76  }
77 
78  if ($appName = $this->getAppName($record['context'])) {
80  }
81 
82  if ($transactionName = $this->getTransactionName($record['context'])) {
84  unset($record['formatted']['context']['transaction_name']);
85  }
86 
87  if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
88  newrelic_notice_error($record['message'], $record['context']['exception']);
89  unset($record['formatted']['context']['exception']);
90  } else {
91  newrelic_notice_error($record['message']);
92  }
93 
94  if (isset($record['formatted']['context']) && is_array($record['formatted']['context'])) {
95  foreach ($record['formatted']['context'] as $key => $parameter) {
96  if (is_array($parameter) && $this->explodeArrays) {
97  foreach ($parameter as $paramKey => $paramValue) {
98  $this->setNewRelicParameter('context_' . $key . '_' . $paramKey, $paramValue);
99  }
100  } else {
101  $this->setNewRelicParameter('context_' . $key, $parameter);
102  }
103  }
104  }
105 
106  if (isset($record['formatted']['extra']) && is_array($record['formatted']['extra'])) {
107  foreach ($record['formatted']['extra'] as $key => $parameter) {
108  if (is_array($parameter) && $this->explodeArrays) {
109  foreach ($parameter as $paramKey => $paramValue) {
110  $this->setNewRelicParameter('extra_' . $key . '_' . $paramKey, $paramValue);
111  }
112  } else {
113  $this->setNewRelicParameter('extra_' . $key, $parameter);
114  }
115  }
116  }
117  }
118 
124  protected function isNewRelicEnabled()
125  {
126  return extension_loaded('newrelic');
127  }
128 
136  protected function getAppName(array $context)
137  {
138  if (isset($context['appname'])) {
139  return $context['appname'];
140  }
141 
142  return $this->appName;
143  }
144 
153  protected function getTransactionName(array $context)
154  {
155  if (isset($context['transaction_name'])) {
156  return $context['transaction_name'];
157  }
158 
159  return $this->transactionName;
160  }
161 
167  protected function setNewRelicAppName($appName)
168  {
170  }
171 
178  {
180  }
181 
186  protected function setNewRelicParameter($key, $value)
187  {
188  if (null === $value || is_scalar($value)) {
189  newrelic_add_custom_parameter($key, $value);
190  } else {
191  newrelic_add_custom_parameter($key, @json_encode($value));
192  }
193  }
194 
198  protected function getDefaultFormatter()
199  {
200  return new NormalizerFormatter();
201  }
202 }
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:57
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...
Create styles array
The data for the language used.
newrelic_name_transaction($transactionName)
isNewRelicEnabled()
Checks whether the NewRelic extension is enabled in the system.
newrelic_set_appname($appname)