-
-
Save totten/4120637 to your computer and use it in GitHub Desktop.
LoggerInterface PSR Proposal (with fluent options)
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
<?php | |
namespace PSR\Log; | |
/** | |
* Describes a logger instance | |
* | |
* The $message MUST be a string. | |
* The $message MAY contain variable expressions like "%var%" which reference $data. | |
* The $message SHOULD NOT include runtime data using plain string concatenation. | |
* | |
* The $data keys are determined solely at the discretion of the log-consumer. | |
* The $data array SHOULD contain only primitive types and arrays. However, data | |
* values of other types MUST NOT cause a crash or exception. | |
* | |
* Log-consumers MAY supply rich details using the fluent helpers -- exception(), | |
* context(), and backtrace(). Log-implementations SHOULD record these details, | |
* but they MAY discard or override details. | |
* | |
* <code> | |
* $log->info("Authentication failed"); | |
* | |
* $log->exception($e)->alert("Authentication failed due to unhandled exception"); | |
* | |
* $log->context('auth') | |
* ->exception($e) | |
* ->info('LDAP authentication for "%user%" failed due to LDAP communication error with "%server%"', array( | |
* 'user' => $username, | |
* 'server' => $ldapServer, | |
* 'basedn' => $ldapBaseDn, // extra data | |
* )) | |
* </code> | |
*/ | |
interface LoggerInterface | |
{ | |
/** | |
* Fluently indicate the context in which the next log message is generated. | |
* These details may be used or ignored by different implementations. | |
* | |
* @param string|null $category a category or tag name determined at the | |
* discretion of the library author | |
* @param string|null $class | |
* @param string|null $file | |
* @param int|null $line | |
* @return LoggerInterface | |
*/ | |
public function context($category = null, $class = null, $file = null, $line = null); | |
/** | |
* Fluently indicate an exception which led to recording the next log message. | |
* These details may be used or ignored by different implementations. | |
* | |
* @param Exception $e | |
* @return LoggerInterface | |
*/ | |
public function exception(Exception $e); | |
/** | |
* Fluently recommend that a full backtrace of the log-call be recorded. | |
* These details may be used or ignored by different implementations. | |
* | |
* @return LoggerInterface | |
*/ | |
public function backtrace(); | |
/** | |
* System is unusable. | |
* | |
* @param string $message | |
* @param array $data | |
* @return null | |
*/ | |
public function emerg($message, array $data = array()); | |
/** | |
* Action must be taken immediately. | |
* | |
* Example: Entire website down, database unavailable, etc. This should | |
* trigger the SMS alerts and wake you up. | |
* | |
* @param string $message | |
* @param array $data | |
* @return null | |
*/ | |
public function alert($message, array $data = array()); | |
/** | |
* Critical conditions. | |
* | |
* Example: Application component unavailable, unexpected exception. | |
* | |
* @param string $message | |
* @param array $data | |
* @return null | |
*/ | |
public function crit($message, array $data = array()); | |
/** | |
* Runtime errors that do not require immediate action but should typically | |
* be logged and monitored. | |
* | |
* @param string $message | |
* @param array $data | |
* @return null | |
*/ | |
public function err($message, array $data = array()); | |
/** | |
* Exceptional occurrences that are not errors. | |
* | |
* Example: Use of deprecated APIs, poor use of an API, undesirable things | |
* that are not necessarily wrong. | |
* | |
* @param string $message | |
* @param array $data | |
* @return null | |
*/ | |
public function warn($message, array $data = array()); | |
/** | |
* Normal but significant events. | |
* | |
* @param string $message | |
* @param array $data | |
* @return null | |
*/ | |
public function notice($message, array $data = array()); | |
/** | |
* Interesting events. | |
* | |
* Example: User logs in, SQL logs. | |
* | |
* @param string $message | |
* @param array $data | |
* @return null | |
*/ | |
public function info($message, array $data = array()); | |
/** | |
* Detailed debug information. | |
* | |
* @param string $message | |
* @param array $data | |
* @return null | |
*/ | |
public function debug($message, array $data = array()); | |
} |
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
<?php | |
namespace PSR\Log; | |
/** | |
* This Logger can be used to avoid conditional log calls | |
* | |
* Logging should always be optional, and if no logger is provided to your | |
* library creating a NullLogger instance to have something to throw logs at | |
* is a good way to avoid littering your code with `if ($this->logger) { }` | |
* blocks. | |
*/ | |
class NullLogger implements LoggerInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function emerg($message, array $data = array()) | |
{ | |
// noop | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function alert($message, array $data = array()) | |
{ | |
// noop | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function crit($message, array $data = array()) | |
{ | |
// noop | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function err($message, array $data = array()) | |
{ | |
// noop | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function warn($message, array $data = array()) | |
{ | |
// noop | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function notice($message, array $data = array()) | |
{ | |
// noop | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function info($message, array $data = array()) | |
{ | |
// noop | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function debug($message, array $data = array()) | |
{ | |
// noop | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function context($category = null, $class = null, $file = null, $line = null) { | |
return $this; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function exception(Exception $e) { | |
return $this; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function backtrace() { | |
return $this; | |
} | |
} |
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
<?php | |
namespace PSR\Log; | |
/** | |
* An example logger implementation which immediately prints messages using var_dump() | |
*/ | |
class VarDumpLogger implements LoggerInterface | |
{ | |
/** | |
* @var array list of extra data fields supplied via fluent interface | |
*/ | |
protected $pendingData = array(); | |
/** | |
* {@inheritDoc} | |
*/ | |
public function emerg($message, array $data = array()) | |
{ | |
$this->addLogRecord('emerg', $message, $data); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function alert($message, array $data = array()) | |
{ | |
$this->addLogRecord('alert', $message, $data); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function crit($message, array $data = array()) | |
{ | |
$this->addLogRecord('crit', $message, $data); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function err($message, array $data = array()) | |
{ | |
$this->addLogRecord('err', $message, $data); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function warn($message, array $data = array()) | |
{ | |
$this->addLogRecord('warn', $message, $data); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function notice($message, array $data = array()) | |
{ | |
$this->addLogRecord('notice', $message, $data); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function info($message, array $data = array()) | |
{ | |
$this->addLogRecord('info', $message, $data); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function debug($message, array $data = array()) | |
{ | |
$this->addLogRecord('debug', $message, $data); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function context($category = null, $class = null, $file = null, $line = null) { | |
$this->pendingData['context'] = array( | |
'category' => $category, | |
'class' => $class, | |
'file' => $file, | |
'line' => $line, | |
); | |
return $this; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function exception(Exception $e) { | |
$this->pendingData['exception'] = $e->getTraceAsString(); | |
return $this; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function backtrace() { | |
$this->pendingData['backtrace'] = debug_backtrace(); | |
return $this; | |
} | |
protected function addLogRecord($level, $message, $data) { | |
$logRecord = array( | |
'level' => $level, | |
'message' => $message, | |
'data' => $data, | |
); | |
$logRecord = $logRecord + $this->pendingData; | |
var_dump($logRecord); | |
$this->pendingData = array(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment