Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of multi-line logs (e.g., with backtrace) #311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/SimpleSAML/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function saveError()
$etrace = implode("\n", $data);

$reportId = bin2hex(openssl_random_pseudo_bytes(4));
SimpleSAML_Logger::error('Error report with id '.$reportId.' generated.');
SimpleSAML_Logger::notice('Error report with id '.$reportId.' generated.');

$config = SimpleSAML_Configuration::getInstance();
$session = SimpleSAML_Session::getSessionFromRequest();
Expand Down
31 changes: 15 additions & 16 deletions lib/SimpleSAML/Error/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ public function format()
public function logError()
{

$lines = $this->format();
foreach ($lines as $line) {
SimpleSAML_Logger::error($line);
}
$this->log('error');
}


Expand All @@ -197,10 +194,7 @@ public function logError()
public function logWarning()
{

$lines = $this->format();
foreach ($lines as $line) {
SimpleSAML_Logger::warning($line);
}
$this->log('warning');
}


Expand All @@ -212,10 +206,18 @@ public function logWarning()
public function logInfo()
{

$lines = $this->format();
foreach ($lines as $line) {
SimpleSAML_Logger::debug($line);
}
$this->log('debug');
}


/**
* Abstracted logging method.
*
* @param string $level The log level (method to be called.)
*/
private function log($level) {

SimpleSAML_Logger::$level($this->format());
}


Expand All @@ -227,10 +229,7 @@ public function logInfo()
public function logDebug()
{

$lines = $this->format();
foreach ($lines as $line) {
SimpleSAML_Logger::debug($line);
}
$this->log('debug');
}


Expand Down
5 changes: 5 additions & 0 deletions lib/SimpleSAML/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,14 @@ private static function log($level, $string, $statsLog = false)
}

if (self::$logLevel >= $level || $statsLog) {
$raw_input = array($string);
if (is_array($string)) {
$raw_input = $string;
$string = implode(",", $string);
}
if ((self::$format == 'json') && method_exists(self::$loggingHandler, 'setArray')) {
self::$loggingHandler->setArray($raw_input);
}

$formats = array('%trackid', '%msg', '%srcip', '%stat');
$replacements = array(self::$trackid, $string, $_SERVER['REMOTE_ADDR']);
Expand Down
32 changes: 26 additions & 6 deletions lib/SimpleSAML/Logger/LoggingHandlerSyslog.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SimpleSAML_Logger_LoggingHandlerSyslog implements SimpleSAML_Logger_Loggin
{
private $isWindows = FALSE;
private $format;
private $arrayData;


/**
Expand All @@ -36,6 +37,16 @@ public function __construct()
}


/**
* Set the Array data for use when logging JSON.
*
* @param array $array Array of data to log with JSON.
*/
public function setArray($array) {
$this->arrayData = $array;
}


/**
* Set the format desired for the logs.
*
Expand Down Expand Up @@ -64,12 +75,21 @@ public function log($level, $string)
}
}

$formats = array('%process', '%level');
$replacements = array('', $level);
$string = str_replace($formats, $replacements, $string);
$string = preg_replace('/%\w+(\{[^\}]+\})?/', '', $string);
$string = trim($string);
if ($this->format == 'json') {
$data = $this->arrayData;
// Send a single line as text, not an array.
if (count($this->arrayData) == 1) {
$data = reset($this->arrayData);
}
$message = json_encode(array('message' => $data));
} else {
$formats = array('%process', '%level');
$replacements = array('', $level);
$string = str_replace($formats, $replacements, $string);
$string = preg_replace('/%\w+(\{[^\}]+\})?/', '', $string);
$message = trim($string);
}

syslog($level, $string);
syslog($level, $message);
}
}