summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenny Katzgrau <katzgrau@gmail.com>2015-04-27 10:06:51 -0400
committerKenny Katzgrau <katzgrau@gmail.com>2015-04-27 10:06:51 -0400
commit6e530b7c0579c69488fabbff9f307d2d2591039e (patch)
tree296381502d2c41172d0e468cf4fbad22769f3c23
parent609527e67b455aa17cdb4e869fb6d04c84c35814 (diff)
parent07fc6e4b8b4b09109cbc0accab50e1e5dd97d287 (diff)
downloadKLogger-6e530b7c0579c69488fabbff9f307d2d2591039e.zip
KLogger-6e530b7c0579c69488fabbff9f307d2d2591039e.tar.gz
KLogger-6e530b7c0579c69488fabbff9f307d2d2591039e.tar.bz2
Merge pull request #54 from richjenks/develop
Added support for custom log line format and JSON output
-rw-r--r--README.markdown44
-rwxr-xr-xsrc/Logger.php48
2 files changed, 78 insertions, 14 deletions
diff --git a/README.markdown b/README.markdown
index d9697c7..b6b31e8 100644
--- a/README.markdown
+++ b/README.markdown
@@ -135,7 +135,51 @@ 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 |
+| priority | Integer value for log level (see `$logLevels`) |
+| 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..108aefa 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,37 @@ class Logger extends AbstractLogger
*/
private function formatMessage($level, $message, $context)
{
- $level = strtoupper($level);
- if (! empty($context)) {
+ if ($this->options['logFormat']) {
+ $parts = array(
+ 'date' => $this->getTimestamp(),
+ 'level' => strtoupper($level),
+ 'priority' => $this->logLevels[$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 +311,7 @@ class Logger extends AbstractLogger
/**
* Takes the given context and coverts it to a string.
- *
+ *
* @param array $context The Context
* @return string
*/
@@ -316,7 +336,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