diff options
-rw-r--r-- | README.markdown | 43 | ||||
-rwxr-xr-x | src/Logger.php | 47 |
2 files changed, 76 insertions, 14 deletions
diff --git a/README.markdown b/README.markdown index d9697c7..358fa8a 100644 --- a/README.markdown +++ b/README.markdown @@ -135,7 +135,50 @@ Here's the full list: | filename | [prefix][date].[extension] | Set the filename for the log file. **This overrides the prefix and extention options.** | | flushFrequency | `false` (disabled) | How many lines to flush the output buffer after | | prefix | 'log_' | The log file prefix | +| logFormat | `false` | Format of log entries | +| appendContext | `true` | When `false`, don't append context to log entries | +### Log Formatting + +The `logFormat` option lets you define what each line should look like and can contain parameters representing the date, message, etc. + +When a string is provided, it will be parsed for variables wrapped in braces (`{` and `}`) and replace them with the appropriate value: + +| Parameter | Description | +| --------- | ----------- | +| date | Current date (uses `dateFormat` option) | +| level | The PSR log level | +| message | The message being logged | +| context | JSON-encoded context | + +#### Tab-separated + +Same as default format but separates parts with tabs rather than spaces: + + $logFormat = "[{date}]\t[{level}]\t{message}"; + +#### Custom variables and static text + +Inject custom content into log messages: + + $logFormat = "[{date}] [$var] StaticText {message}"; + +#### JSON + +To output pure JSON, set `appendContext` to `false` and provide something like the below as the value of the `logFormat` option: + +``` +$logFormat = json_encode([ + 'datetime' => '{date}', + 'logLevel' => '{level}', + 'message' => '{message}', + 'context' => '{context}', +]); +``` + +The output will look like: + + {"datetime":"2015-04-16 10:28:41.186728","logLevel":"INFO","message":"Message content","context":"{"1":"foo","2":"bar"}"} ## Why use KLogger? diff --git a/src/Logger.php b/src/Logger.php index 4dfcb3d..87b3dc2 100755 --- a/src/Logger.php +++ b/src/Logger.php @@ -38,11 +38,13 @@ class Logger extends AbstractLogger * @var array
*/
private $options = array (
- 'extension' => 'txt',
- 'dateFormat' => 'Y-m-d G:i:s.u',
- 'filename' => false,
+ 'extension' => 'txt',
+ 'dateFormat' => 'Y-m-d G:i:s.u',
+ 'filename' => false,
'flushFrequency' => false,
- 'prefix' => 'log_'
+ 'prefix' => 'log_',
+ 'logFormat' => false,
+ 'appendContext' => true,
);
/**
@@ -178,7 +180,7 @@ class Logger extends AbstractLogger /**
* Sets the date format used by all instances of KLogger
- *
+ *
* @param string $dateFormat Valid format string for date()
*/
public function setDateFormat($dateFormat)
@@ -188,7 +190,7 @@ class Logger extends AbstractLogger /**
* Sets the Log Level Threshold
- *
+ *
* @param string $logLevelThreshold The log level threshold
*/
public function setLogLevelThreshold($logLevelThreshold)
@@ -209,7 +211,7 @@ class Logger extends AbstractLogger if ($this->logLevels[$this->logLevelThreshold] < $this->logLevels[$level]) {
return;
}
- $message = $this->formatMessage($level, $message, $context);
+ $message = $this->formatMessage($level, $message, $context);
$this->write($message);
}
@@ -265,19 +267,36 @@ class Logger extends AbstractLogger */
private function formatMessage($level, $message, $context)
{
- $level = strtoupper($level);
- if (! empty($context)) {
+ if ($this->options['logFormat']) {
+ $parts = [
+ 'date' => $this->getTimestamp(),
+ 'level' => strtoupper($level),
+ 'message' => $message,
+ 'context' => json_encode($context),
+ ];
+ $message = $this->options['logFormat'];
+ foreach ($parts as $part => $value) {
+ $message = str_replace('{'.$part.'}', $value, $message);
+ }
+
+ } else {
+ $message = "[{$this->getTimestamp()}] [{$level}] {$message}";
+ }
+
+ if ($this->options['appendContext'] && ! empty($context)) {
$message .= PHP_EOL.$this->indent($this->contextToString($context));
}
- return "[{$this->getTimestamp()}] [{$level}] {$message}".PHP_EOL;
+
+ return $message.PHP_EOL;
+
}
/**
* Gets the correctly formatted Date/Time for the log entry.
- *
+ *
* PHP DateTime is dump, and you have to resort to trickery to get microseconds
* to work correctly, so here it is.
- *
+ *
* @return string
*/
private function getTimestamp()
@@ -291,7 +310,7 @@ class Logger extends AbstractLogger /**
* Takes the given context and coverts it to a string.
- *
+ *
* @param array $context The Context
* @return string
*/
@@ -316,7 +335,7 @@ class Logger extends AbstractLogger /**
* Indents the given string with the given indent.
- *
+ *
* @param string $string The string to indent
* @param string $indent What to use as the indent.
* @return string
|