Skip to content

Commit

Permalink
Code Style: Apply PHPCS fixes
Browse files Browse the repository at this point in the history
This file failed to be processed by phpcfb last time, so needed to be manually modified to allow the parser to continue.
  • Loading branch information
mapkyca committed Sep 24, 2018
1 parent 58772c3 commit 92b065a
Showing 1 changed file with 62 additions and 45 deletions.
107 changes: 62 additions & 45 deletions Idno/Core/Logging.php
Expand Up @@ -13,7 +13,8 @@
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class Logging extends Component implements LoggerInterface {
class Logging extends Component implements LoggerInterface
{

public $loglevel_filter = 4;
private $identifier;
Expand All @@ -24,7 +25,8 @@ class Logging extends Component implements LoggerInterface {
* @param type $loglevel_filter Log levels to show 0 - off, 1 - errors, 2 - errors & warnings, 3 - errors, warnings and info, 4 - 3 + debug
* @param type $identifier Identify this site in the log (defaults to current domain)
*/
public function __construct($loglevel_filter = 0, $identifier = null) {
public function __construct($loglevel_filter = 0, $identifier = null)
{
if (!$identifier)
$identifier = Idno::site()->config->host;
if (isset(Idno::site()->config->loglevel)) {
Expand All @@ -36,38 +38,41 @@ public function __construct($loglevel_filter = 0, $identifier = null) {

// Set log to handle PHP errors
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {

if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}

$message = "PHP [{$errno}] {$errstr} in {$errfile}:{$errline}";

// Pedantic mode
if (\Idno\Core\Idno::site()->config()->pedantic_mode) {
$this->error($message);
throw new \ErrorException($message, 0, $errno, $errfile, $errline);
}

switch ($errno) {
case E_PARSE:
case E_PARSE:
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR: $this->error($message); break;
case E_USER_ERROR: $this->error($message);
break;

case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING: $this->warning($message); break;
case E_USER_WARNING: $this->warning($message);
break;

case E_STRICT:
case E_NOTICE:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_STRICT:
case E_USER_NOTICE: $this->notice($message); break;
case E_USER_NOTICE: $this->notice($message);
break;

default:
$this->notice("Unknown error type: {$message}");
Expand All @@ -83,7 +88,8 @@ public function __construct($loglevel_filter = 0, $identifier = null) {
* Sets the log level
* @param $loglevel
*/
public function setLogLevel($loglevel) {
public function setLogLevel($loglevel)
{
$this->loglevel_filter = $loglevel;
}

Expand All @@ -92,10 +98,11 @@ public function setLogLevel($loglevel) {
* @param string $level
* @return boolean true if messages at this level should be logged
*/
private function passesFilter($level) {
private function passesFilter($level)
{
switch ($level) {
case LogLevel::EMERGENCY: case LogLevel::ALERT:
case LogLevel::CRITICAL: case LogLevel::ERROR:
case LogLevel::CRITICAL: case LogLevel::ERROR:
return $this->loglevel_filter >= LOGLEVEL_ERROR;
case LogLevel::WARNING:
return $this->loglevel_filter >= LOGLEVEL_WARNING;
Expand All @@ -113,7 +120,8 @@ private function passesFilter($level) {
* @param string $message
* @param array $context
*/
public function log($level, $message = LOGLEVEL_INFO, array $context = array()) {
public function log($level, $message = LOGLEVEL_INFO, array $context = array())
{
// backward compatibility
if (is_string($level) && is_int($message)) {
// TODO in a future version, warn that this
Expand All @@ -138,13 +146,13 @@ public function log($level, $message = LOGLEVEL_INFO, array $context = array())
if (!empty($stats)) {
$stats->increment("log.$level");
}

// Construct log message
// Trace for debug (when filtering is set to debug, always add a trace)
$trace = "";
if ($this->loglevel_filter == LOGLEVEL_DEBUG) {
$backtrace = @debug_backtrace(false, 3);
foreach (array_reverse($backtrace) as $frame) {
foreach (array_reverse($backtrace) as $frame) {
if (isset($frame['class']) && isset($frame['file']) && isset($frame['line']) /*&& $frame['class'] !== 'Idno\Core\Logging'*/) {
$trace = " [{$frame['file']}:{$frame['line']}]";
break;
Expand All @@ -165,15 +173,15 @@ public function log($level, $message = LOGLEVEL_INFO, array $context = array())
}

$lines = 0;
$message = explode("\n", $message);
$message = explode("\n", $message);
foreach ($message as $log) {
$logline = "Known ({$this->identifier}): $level - " . rtrim($log);

if ($lines == count($message)-1)
$logline.=$trace;

error_log($logline);

// Have we enabled local capture (not recommended, but handy for debugging situations where you can't retrieve apache/php logs)
if (!empty(\Idno\Core\Idno::site()->config()->capture_logs) && \Idno\Core\Idno::site()->config()->capture_logs) {
$f = fopen(\Idno\Core\Idno::site()->config()->getTempDir() . \Idno\Core\Idno::site()->config()->host . '.log', 'a');
Expand All @@ -187,7 +195,7 @@ public function log($level, $message = LOGLEVEL_INFO, array $context = array())

fclose($f);
}

$lines ++;
}
}
Expand All @@ -201,7 +209,8 @@ public function log($level, $message = LOGLEVEL_INFO, array $context = array())
*
* @return null
*/
public function emergency($message, array $context = array()) {
public function emergency($message, array $context = array())
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}

Expand All @@ -216,7 +225,8 @@ public function emergency($message, array $context = array()) {
*
* @return null
*/
public function alert($message, array $context = array()) {
public function alert($message, array $context = array())
{
$this->log(LogLevel::ALERT, $message, $context);
}

Expand All @@ -230,7 +240,8 @@ public function alert($message, array $context = array()) {
*
* @return null
*/
public function critical($message, array $context = array()) {
public function critical($message, array $context = array())
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

Expand All @@ -243,7 +254,8 @@ public function critical($message, array $context = array()) {
*
* @return null
*/
public function error($message, array $context = array()) {
public function error($message, array $context = array())
{
$this->log(LogLevel::ERROR, $message, $context);
}

Expand All @@ -258,7 +270,8 @@ public function error($message, array $context = array()) {
*
* @return null
*/
public function warning($message, array $context = array()) {
public function warning($message, array $context = array())
{
$this->log(LogLevel::WARNING, $message, $context);
}

Expand All @@ -270,7 +283,8 @@ public function warning($message, array $context = array()) {
*
* @return null
*/
public function notice($message, array $context = array()) {
public function notice($message, array $context = array())
{
$this->log(LogLevel::NOTICE, $message, $context);
}

Expand All @@ -284,7 +298,8 @@ public function notice($message, array $context = array()) {
*
* @return null
*/
public function info($message, array $context = array()) {
public function info($message, array $context = array())
{
$this->log(LogLevel::INFO, $message, $context);
}

Expand All @@ -296,61 +311,63 @@ public function info($message, array $context = array()) {
*
* @return null
*/
public function debug($message, array $context = array()) {
public function debug($message, array $context = array())
{
$this->log(LogLevel::DEBUG, $message, $context);
}


/**
* (attempt) to send, if configured, a message when a fatal error occurs, or an exception is caught.
* @param type $message
* @param type $title
*/
public static function oopsAlert($message, $title = "") {

public static function oopsAlert($message, $title = "")
{

$config = \Idno\Core\idno::site()->config();

if (!empty($config) && !empty($config->oops_notify)) {

$notify = $config->oops_notify;
if (!is_array($notify)) $notify = [$notify];

$title = $config->host . ": $title";

foreach ($notify as $emailaddress) {
if (!empty($emailaddress) && filter_var($emailaddress, FILTER_VALIDATE_EMAIL)) {

$vars = [
'site' => $config->getDisplayURL(),
'message' => $message,
'user' => 'UNKNOWN',

'agent' => '',
'qs' => '',
'referrer' => ''
'referrer' => '',
];

$uuid = \Idno\Core\Idno::site()->session()->currentUserUUID();
if (!empty($uuid))
$vars['user'] = \Idno\Core\Idno::site()->session()->currentUserUUID();

if (!empty($_SERVER['HTTP_USER_AGENT']))
$vars['agent'] = $_SERVER['HTTP_USER_AGENT'];

if (!empty($_SERVER['QUERY_STRING']))
$vars['qs'] = $_SERVER['QUERY_STRING'];

if (!empty($_SERVER['HTTP_REFERER']))
$vars['referrer'] = $_SERVER['HTTP_REFERER'];

$email = new Email();
if (!empty($title))
$email->setSubject($title);
$email->setHTMLBodyFromTemplate('admin/oops', $vars);
$email->setTextBodyFromTemplate('admin/oops', $vars);
$email->addTo($emailaddress);
$email->send();

}
}
}
Expand Down

0 comments on commit 92b065a

Please sign in to comment.