aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/sprinkles/core/src/Log
diff options
context:
space:
mode:
authormarvin-borner@live.com2018-04-16 21:09:05 +0200
committermarvin-borner@live.com2018-04-16 21:09:05 +0200
commitcf14306c2b3f82a81f8d56669a71633b4d4b5fce (patch)
tree86700651aa180026e89a66064b0364b1e4346f3f /main/app/sprinkles/core/src/Log
parent619b01b3615458c4ed78bfaeabb6b1a47cc8ad8b (diff)
Main merge to user management system - files are now at /main/public/
Diffstat (limited to 'main/app/sprinkles/core/src/Log')
-rwxr-xr-xmain/app/sprinkles/core/src/Log/DatabaseHandler.php53
-rwxr-xr-xmain/app/sprinkles/core/src/Log/MixedFormatter.php59
2 files changed, 112 insertions, 0 deletions
diff --git a/main/app/sprinkles/core/src/Log/DatabaseHandler.php b/main/app/sprinkles/core/src/Log/DatabaseHandler.php
new file mode 100755
index 0000000..c78308c
--- /dev/null
+++ b/main/app/sprinkles/core/src/Log/DatabaseHandler.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Sprinkle\Core\Log;
+
+use Monolog\Logger;
+use Monolog\Handler\AbstractProcessingHandler;
+
+/**
+ * Monolog handler for storing the record to a database.
+ *
+ * @author Alex Weissman (https://alexanderweissman.com)
+ */
+class DatabaseHandler extends AbstractProcessingHandler
+{
+ /**
+ * @var UserFrosting\Sprinkle\Core\Util\ClassMapper
+ */
+ protected $classMapper;
+
+ /**
+ * @var string
+ */
+ protected $modelIdentifier;
+
+ /**
+ * Create a new DatabaseHandler object.
+ *
+ * @param ClassMapper $classMapper Maps the modelIdentifier to the specific Eloquent model.
+ * @param string $modelIdentifier
+ * @param int $level The minimum logging level at which this handler will be triggered
+ * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
+ */
+ public function __construct($classMapper, $modelIdentifier, $level = Logger::DEBUG, $bubble = true)
+ {
+ $this->classMapper = $classMapper;
+ $this->modelName = $modelIdentifier;
+ parent::__construct($level, $bubble);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected function write(array $record)
+ {
+ $log = $this->classMapper->createInstance($this->modelName, $record['extra']);
+ $log->save();
+ }
+}
diff --git a/main/app/sprinkles/core/src/Log/MixedFormatter.php b/main/app/sprinkles/core/src/Log/MixedFormatter.php
new file mode 100755
index 0000000..beae788
--- /dev/null
+++ b/main/app/sprinkles/core/src/Log/MixedFormatter.php
@@ -0,0 +1,59 @@
+<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Sprinkle\Core\Log;
+
+use Monolog\Formatter\LineFormatter;
+
+/**
+ * Monolog formatter for pretty-printing arrays and objects.
+ *
+ * This class extends the basic Monolog LineFormatter class, and provides basically the same functionality but with one exception:
+ * if the second parameter of any logging method (debug, error, info, etc) is an array, it will print it as a nicely formatted,
+ * multi-line JSON object instead of all on a single line.
+ * @author Alex Weissman (https://alexanderweissman.com)
+ */
+class MixedFormatter extends LineFormatter
+{
+
+ /**
+ * Return the JSON representation of a value
+ *
+ * @param mixed $data
+ * @param bool $ignoreErrors
+ * @throws \RuntimeException if encoding fails and errors are not ignored
+ * @return string
+ */
+ protected function toJson($data, $ignoreErrors = false)
+ {
+ // suppress json_encode errors since it's twitchy with some inputs
+ if ($ignoreErrors) {
+ return @$this->jsonEncodePretty($data);
+ }
+
+ $json = $this->jsonEncodePretty($data);
+
+ if ($json === false) {
+ $json = $this->handleJsonError(json_last_error(), $data);
+ }
+
+ return $json;
+ }
+
+ /**
+ * @param mixed $data
+ * @return string JSON encoded data or null on failure
+ */
+ private function jsonEncodePretty($data)
+ {
+ if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
+ return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
+ }
+
+ return json_encode($data);
+ }
+}