-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
924277d
commit ed6d75b
Showing
4 changed files
with
180 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
preset: psr2 | ||
|
||
enabled: | ||
- duplicate_semicolon | ||
- empty_return | ||
- extra_empty_lines | ||
- operators_spaces | ||
- phpdoc_indent | ||
- remove_leading_slash_use | ||
- spaces_cast | ||
- ternary_spaces | ||
- unused_use | ||
|
||
disabled: | ||
- braces | ||
- parenthesis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,160 @@ | ||
<?php namespace Jenssegers\Rollbar; | ||
|
||
use Exception, RollbarNotifier; | ||
use Exception; | ||
use RollbarNotifier; | ||
use Monolog\Logger as Monolog; | ||
use Illuminate\Foundation\Application; | ||
|
||
class RollbarLogHandler { | ||
|
||
/** | ||
* The rollbar client instance. | ||
* | ||
* @var RollbarNotifier | ||
*/ | ||
protected $rollbar; | ||
|
||
/** | ||
* The Laravel application. | ||
* | ||
* @var \Illuminate\Foundation\Application | ||
*/ | ||
protected $app; | ||
|
||
/** | ||
* The minimum log level at which messages are sent to Rollbar. | ||
* | ||
* @var string | ||
*/ | ||
protected $level; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(RollbarNotifier $rollbar, Application $app, $level = 'debug') | ||
{ | ||
$this->rollbar = $rollbar; | ||
|
||
$this->app = $app; | ||
|
||
$this->level = $this->parseLevel($level ?: 'debug'); | ||
|
||
// Set Laravel information | ||
$this->rollbar->environment = $this->app->environment(); | ||
$this->rollbar->root = base_path(); | ||
} | ||
|
||
/** | ||
* Log a message to Rollbar. | ||
* | ||
* @param mixed $level | ||
* @param string $message | ||
* @param array $context | ||
*/ | ||
public function log($level, $message, array $context = array()) | ||
{ | ||
// Check if we want to log this message. | ||
if ($this->parseLevel($level) < $this->level) | ||
{ | ||
return; | ||
} | ||
|
||
$context = $this->addContext($context); | ||
|
||
if ($message instanceof Exception) | ||
{ | ||
$this->rollbar->report_exception($message); | ||
} | ||
else | ||
{ | ||
$this->rollbar->report_message($message, $level, $context); | ||
} | ||
} | ||
|
||
/** | ||
* Add Laravel specific information to the context. | ||
* | ||
* @param array $context | ||
*/ | ||
protected function addContext(array $context = array()) | ||
{ | ||
// Add session data. | ||
if ($session = $this->app->session->all()) | ||
{ | ||
if (empty($this->rollbar->person) or ! is_array($this->rollbar->person)) | ||
{ | ||
$this->rollbar->person = []; | ||
} | ||
|
||
// Merge person context. | ||
if (isset($context['person']) and is_array($context['person'])) | ||
{ | ||
$this->rollbar->person = $context['person']; | ||
unset($context['person']); | ||
} | ||
|
||
// Add user session information. | ||
if (isset($this->rollbar->person['session'])) | ||
{ | ||
$this->rollbar->person['session'] = array_merge($session, $this->rollbar->person['session']); | ||
} | ||
else | ||
{ | ||
$this->rollbar->person['session'] = $session; | ||
} | ||
|
||
// User session id as user id if not set. | ||
if ( ! isset($this->rollbar->person['id'])) | ||
{ | ||
$this->rollbar->person['id'] = $this->app->session->getId(); | ||
} | ||
} | ||
|
||
return $context; | ||
} | ||
|
||
/** | ||
* Parse the string level into a Monolog constant. | ||
* | ||
* @param string $level | ||
* @return int | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
protected function parseLevel($level) | ||
{ | ||
switch ($level) | ||
{ | ||
case 'debug': | ||
return Monolog::DEBUG; | ||
|
||
case 'info': | ||
return Monolog::INFO; | ||
|
||
case 'notice': | ||
return Monolog::NOTICE; | ||
|
||
case 'warning': | ||
return Monolog::WARNING; | ||
|
||
case 'error': | ||
return Monolog::ERROR; | ||
|
||
case 'critical': | ||
return Monolog::CRITICAL; | ||
|
||
case 'alert': | ||
return Monolog::ALERT; | ||
|
||
case 'emergency': | ||
return Monolog::EMERGENCY; | ||
|
||
case 'none': | ||
return 1000; | ||
|
||
default: | ||
throw new \InvalidArgumentException("Invalid log level."); | ||
} | ||
} | ||
/** | ||
* The rollbar client instance. | ||
* | ||
* @var RollbarNotifier | ||
*/ | ||
protected $rollbar; | ||
|
||
/** | ||
* The Laravel application. | ||
* | ||
* @var \Illuminate\Foundation\Application | ||
*/ | ||
protected $app; | ||
|
||
/** | ||
* The minimum log level at which messages are sent to Rollbar. | ||
* | ||
* @var string | ||
*/ | ||
protected $level; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct(RollbarNotifier $rollbar, Application $app, $level = 'debug') | ||
{ | ||
$this->rollbar = $rollbar; | ||
|
||
$this->app = $app; | ||
|
||
$this->level = $this->parseLevel($level ?: 'debug'); | ||
|
||
// Set Laravel information | ||
$this->rollbar->environment = $this->app->environment(); | ||
$this->rollbar->root = base_path(); | ||
} | ||
|
||
/** | ||
* Log a message to Rollbar. | ||
* | ||
* @param mixed $level | ||
* @param string $message | ||
* @param array $context | ||
*/ | ||
public function log($level, $message, array $context = array()) | ||
{ | ||
// Check if we want to log this message. | ||
if ($this->parseLevel($level) < $this->level) | ||
{ | ||
return; | ||
} | ||
|
||
$context = $this->addContext($context); | ||
|
||
if ($message instanceof Exception) | ||
{ | ||
$this->rollbar->report_exception($message); | ||
} | ||
else | ||
{ | ||
$this->rollbar->report_message($message, $level, $context); | ||
} | ||
} | ||
|
||
/** | ||
* Add Laravel specific information to the context. | ||
* | ||
* @param array $context | ||
*/ | ||
protected function addContext(array $context = array()) | ||
{ | ||
// Add session data. | ||
if ($session = $this->app->session->all()) | ||
{ | ||
if (empty($this->rollbar->person) or ! is_array($this->rollbar->person)) | ||
{ | ||
$this->rollbar->person = []; | ||
} | ||
|
||
// Merge person context. | ||
if (isset($context['person']) and is_array($context['person'])) | ||
{ | ||
$this->rollbar->person = $context['person']; | ||
unset($context['person']); | ||
} | ||
|
||
// Add user session information. | ||
if (isset($this->rollbar->person['session'])) | ||
{ | ||
$this->rollbar->person['session'] = array_merge($session, $this->rollbar->person['session']); | ||
} | ||
else | ||
{ | ||
$this->rollbar->person['session'] = $session; | ||
} | ||
|
||
// User session id as user id if not set. | ||
if ( ! isset($this->rollbar->person['id'])) | ||
{ | ||
$this->rollbar->person['id'] = $this->app->session->getId(); | ||
} | ||
} | ||
|
||
return $context; | ||
} | ||
|
||
/** | ||
* Parse the string level into a Monolog constant. | ||
* | ||
* @param string $level | ||
* @return int | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
protected function parseLevel($level) | ||
{ | ||
switch ($level) | ||
{ | ||
case 'debug': | ||
return Monolog::DEBUG; | ||
|
||
case 'info': | ||
return Monolog::INFO; | ||
|
||
case 'notice': | ||
return Monolog::NOTICE; | ||
|
||
case 'warning': | ||
return Monolog::WARNING; | ||
|
||
case 'error': | ||
return Monolog::ERROR; | ||
|
||
case 'critical': | ||
return Monolog::CRITICAL; | ||
|
||
case 'alert': | ||
return Monolog::ALERT; | ||
|
||
case 'emergency': | ||
return Monolog::EMERGENCY; | ||
|
||
case 'none': | ||
return 1000; | ||
|
||
default: | ||
throw new \InvalidArgumentException("Invalid log level."); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.