function sendSentryMessage($message) { $CI =& get_instance(); $client = new Raven_Client($CI->config->item('sentry_client_id')); $client->getIdent($client->captureMessage($message)); $client->context->clear(); }
/** * Log a message to the logs. * * @param string $level * @param mixed $message * @param array $context * * @return void */ public function log($level, $message, array $context = []) { $this->sentry->context->clear(); if ($user = $this->resolveCurrentUser()) { $this->sentry->user_context($user); } $this->sentry->extra_context($context); $level = $this->getSeverity($level); if ($message instanceof Exception) { $this->sentry->getIdent($this->sentry->captureException($message, ['level' => $level])); } else { $msg = $this->formatMessage($message); $this->sentry->getIdent($this->sentry->captureMessage($msg, [], ['level' => $level])); } }
/** * Logging of errors * * @param $errorMessage * @param $response */ public static function log($errorMessage, $response) { if (is_null(self::$ravenClient)) { self::$ravenClient = new Raven_Client('http://*****:*****@logs.getsitecontrol.com/14'); } self::$ravenClient->captureMessage($errorMessage, array('server' => !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null, 'code' => !empty($response->info->http_code) ? $response->info->http_code : null, 'response' => !empty($response->response) ? $response->response : null)); }
protected function execute(InputInterface $input, OutputInterface $output) { $config = $this->setConfiguraton($input); $client = new \GuzzleHttp\Client(['defaults' => ['allow_redirects' => false, 'timeout' => 5, 'connect_timeout' => 5]]); /** @var Instance $instance */ foreach ($config->getInstances() as $instance) { $requests[] = $client->createRequest('HEAD', $instance->getUrl()); } $options = []; Pool::send($client, $requests, ['complete' => function (CompleteEvent $event) { }, 'error' => function (ErrorEvent $event) use($config) { $instance = $config->findInstanceByUrl($event->getRequest()->getUrl()); if ($instance == null) { throw new \RuntimeException('Instance not found'); } if (!$event->getException()->hasResponse()) { $raven = new \Raven_Client($instance->getSentryDsn()); $event_id = $raven->getIdent($raven->captureMessage(sprintf('The website %s with url -> %s is down or has a problem', $instance->getName(), $event->getRequest()->getUrl()), [], \Raven_Client::FATAL)); if ($raven->getLastError() !== null) { printf('There was an error sending the event to Sentry: %s', $raven->getLastError()); } $error_handler = new \Raven_ErrorHandler($raven); $error_handler->registerExceptionHandler(); $error_handler->registerErrorHandler(); $error_handler->registerShutdownFunction(); } }]); }
/** * @param $message * @param string $priority * @return null|string */ public function log($message, $priority = self::INFO) { if ($this->enabled) { $exceptionFile = ''; if ($this->directory && is_dir($this->directory)) { // Compability with nette 2.2.0, Tracy\Logger has no getExceptionFile in 2.2.0 if (method_exists($this, 'getExceptionFile')) { $exceptionFile = $message instanceof Exception ? $this->getExceptionFile($message) : null; } else { $exceptionFile = null; } // Compability with nette 2.2.0, Tracy\Logger has no formatLogLine in 2.2.0 if (method_exists($this, 'formatLogLine')) { $line = $this->formatLogLine($message, $exceptionFile); } else { if (is_array($message)) { $message = implode(' ', $message); } $line = preg_replace('#\\s*\\r?\\n\\s*#', ' ', trim($message)); } $file = $this->directory . '/' . strtolower($priority ?: self::INFO) . '.log'; if (!@file_put_contents($file, $line . PHP_EOL, FILE_APPEND | LOCK_EX)) { throw new \RuntimeException("Unable to write to log file '{$file}'. Is directory writable?"); } } if ($message instanceof Exception) { $this->raven->captureException($message); if ($this->directory && is_dir($this->directory) && method_exists($this, 'logException')) { $this->logException($message, $exceptionFile); } } else { if (in_array($priority, array(self::ERROR, self::EXCEPTION, self::CRITICAL, self::WARNING), true)) { $this->raven->captureMessage($message, array(), $priority); } } // Compability with nette 2.2.0, Tracy\Logger has no sendEmail in 2.2.0 if (in_array($priority, array(self::ERROR, self::EXCEPTION, self::CRITICAL), true) && method_exists($this, 'sendEmail')) { $this->sendEmail($message); } return $exceptionFile; } else { return parent::log($message, $priority); } }
/** * Logs a message to Sentry. * @param string $message message to log. * @param array $params message parameters. * @param array $options capture options that can contain the following structure: * culprit: (string) function call that caused the event * extra: (array) additional metadata to store with the event * @param bool $stack whether to send the stack trace. * @param mixed $context message context. * @return string event id (or null if not captured). * @throws Exception if logging the message fails. */ public function captureMessage($message, $params = [], $options = [], $stack = false, $context = null) { if (strlen($message) > self::MAX_MESSAGE_LENGTH) { throw new InvalidParamException(sprintf('SentryClient cannot send messages that contain more than %d characters.', self::MAX_MESSAGE_LENGTH)); } if (!$this->isEnvironmentEnabled()) { return null; } $this->processOptions($options); try { $eventId = $this->_client->getIdent($this->_client->captureMessage($message, $params, $options, $stack, $context)); } catch (\Exception $e) { if (YII_DEBUG) { throw new Exception('SentryClient failed to log message: ' . $e->getMessage(), (int) $e->getCode()); } else { $this->log($e->getMessage(), Logger::LEVEL_ERROR); throw new Exception('SentryClient failed to log message.', (int) $e->getCode()); } } $this->_loggedEventIds[] = $eventId; $this->log(sprintf('Message logged to Sentry with event id: %d', $eventId), Logger::LEVEL_INFO); return $eventId; }
/** * Send errors to a remote Sentry server with level = warning * * @param array $ferrors * * @return void */ private function sentrySend($ferrors) { //ignore if no username or nagios request if (EGS_USERNAME or strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'nagios') !== false) { try { $client = new Raven_Client(SENTRY_DSN, array('curl_method' => 'async', 'verify_ssl' => FALSE)); // Capture the flash errors and send to Sentry $client->user_context(array('username' => EGS_USERNAME)); $client->tags_context(array('source' => 'flash')); $cc = 0; foreach ($ferrors as $ferror) { $cc++; $client->extra_context(array('error ' . $cc => $ferror)); } if ($cc != 0) { $event_id = $client->getIdent($client->captureMessage($ferrors[0], array(), 'warning')); } } catch (Exception $e) { //If something went wrong, just continue. } } }
/** * Export the message * * @param array $content Array containing the info about the message * @return void */ public function export($content) { global $conf; $dsn = $conf->global->SYSLOG_SENTRY_DSN; $client = new Raven_Client($dsn, array('curl_method' => 'exec')); $client->user_context(array('username' => $content['user'] ? $content['user'] : '', 'ip_address' => $content['ip'])); $client->tags_context(array('version' => DOL_VERSION)); $client->registerSeverityMap(array(LOG_EMERG => Raven_Client::FATAL, LOG_ALERT => Raven_Client::FATAL, LOG_CRIT => Raven_Client::ERROR, LOG_ERR => Raven_Client::ERROR, LOG_WARNING => Raven_Client::WARNING, LOG_NOTICE => Raven_Client::WARNING, LOG_INFO => Raven_Client::INFO, LOG_DEBUG => Raven_Client::DEBUG)); if (substr($content['message'], 0, 3) === 'sql') { global $db; $query = substr($content['message'], 4, strlen($content['message'])); $client->captureQuery($query, $client->translateSeverity($content['level']), $db->type); } else { $client->captureMessage($content['message'], null, $client->translateSeverity($content['level'])); } }
/** * Log a message to sentry * * @param string $message Message to log * @param array $params Parameters to post * @param array $level_or_options Options * @param boolean $stack Stack sending of this message? * @param array $vars Additional vars * * @return integer Captured event ID */ public function captureMessage($message, $params = array(), $level_or_options = array(), $stack = false, $vars = null) { return $this->_client->captureMessage($message, $params, $level_or_options, $stack, $vars); }