summaryrefslogtreecommitdiff
path: root/vendor/fguillot/simpleLogger/src/SimpleLogger
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/fguillot/simpleLogger/src/SimpleLogger')
-rw-r--r--vendor/fguillot/simpleLogger/src/SimpleLogger/Base.php89
-rw-r--r--vendor/fguillot/simpleLogger/src/SimpleLogger/File.php48
-rw-r--r--vendor/fguillot/simpleLogger/src/SimpleLogger/Logger.php94
-rw-r--r--vendor/fguillot/simpleLogger/src/SimpleLogger/Stderr.php25
-rw-r--r--vendor/fguillot/simpleLogger/src/SimpleLogger/Stdout.php25
-rw-r--r--vendor/fguillot/simpleLogger/src/SimpleLogger/Syslog.php72
6 files changed, 353 insertions, 0 deletions
diff --git a/vendor/fguillot/simpleLogger/src/SimpleLogger/Base.php b/vendor/fguillot/simpleLogger/src/SimpleLogger/Base.php
new file mode 100644
index 00000000..c662a1a3
--- /dev/null
+++ b/vendor/fguillot/simpleLogger/src/SimpleLogger/Base.php
@@ -0,0 +1,89 @@
+<?php
+
+namespace SimpleLogger;
+
+use Psr\Log\AbstractLogger;
+use Psr\Log\LogLevel;
+
+/**
+ * Base class for loggers
+ *
+ * @package SimpleLogger
+ * @author Frédéric Guillot
+ */
+abstract class Base extends AbstractLogger
+{
+ /**
+ * Minimum log level for the logger
+ *
+ * @access private
+ * @var string
+ */
+ private $level = LogLevel::DEBUG;
+
+ /**
+ * Set minimum log level
+ *
+ * @access public
+ * @param string $level
+ */
+ public function setLevel($level)
+ {
+ $this->level = $level;
+ }
+
+ /**
+ * Get minimum log level
+ *
+ * @access public
+ * @return string
+ */
+ public function getLevel()
+ {
+ return $this->level;
+ }
+
+ /**
+ * Dump to log a variable (by example an array)
+ *
+ * @param mixed $variable
+ */
+ public function dump($variable)
+ {
+ $this->log(LogLevel::DEBUG, var_export($variable, true));
+ }
+
+ /**
+ * Interpolates context values into the message placeholders.
+ *
+ * @access protected
+ * @param string $message
+ * @param array $context
+ * @return string
+ */
+ protected function interpolate($message, array $context = array())
+ {
+ // build a replacement array with braces around the context keys
+ $replace = array();
+
+ foreach ($context as $key => $val) {
+ $replace['{' . $key . '}'] = $val;
+ }
+
+ // interpolate replacement values into the message and return
+ return strtr($message, $replace);
+ }
+
+ /**
+ * Format log message
+ *
+ * @param mixed $level
+ * @param string $message
+ * @param array $context
+ * @return string
+ */
+ protected function formatMessage($level, $message, array $context = array())
+ {
+ return '['.date('Y-m-d H:i:s').'] ['.$level.'] '.$this->interpolate($message, $context).PHP_EOL;
+ }
+}
diff --git a/vendor/fguillot/simpleLogger/src/SimpleLogger/File.php b/vendor/fguillot/simpleLogger/src/SimpleLogger/File.php
new file mode 100644
index 00000000..be3bfa85
--- /dev/null
+++ b/vendor/fguillot/simpleLogger/src/SimpleLogger/File.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace SimpleLogger;
+
+use RuntimeException;
+
+/**
+ * File logger
+ *
+ * @package SimpleLogger
+ * @author Frédéric Guillot
+ */
+class File extends Base
+{
+ /**
+ * Filename
+ *
+ * @access protected
+ * @var string
+ */
+ protected $filename = '';
+
+ /**
+ * Setup logger configuration
+ *
+ * @param string $filename Output file
+ */
+ public function __construct($filename)
+ {
+ $this->filename = $filename;
+ }
+
+ /**
+ * Logs with an arbitrary level.
+ *
+ * @param mixed $level
+ * @param string $message
+ * @param array $context
+ */
+ public function log($level, $message, array $context = array())
+ {
+ $line = $this->formatMessage($level, $message, $context);
+
+ if (file_put_contents($this->filename, $line, FILE_APPEND | LOCK_EX) === false) {
+ throw new RuntimeException('Unable to write to the log file.');
+ }
+ }
+}
diff --git a/vendor/fguillot/simpleLogger/src/SimpleLogger/Logger.php b/vendor/fguillot/simpleLogger/src/SimpleLogger/Logger.php
new file mode 100644
index 00000000..dc340cde
--- /dev/null
+++ b/vendor/fguillot/simpleLogger/src/SimpleLogger/Logger.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace SimpleLogger;
+
+use Psr\Log\AbstractLogger;
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\LogLevel;
+
+/**
+ * Handler for multiple loggers
+ *
+ * @package SimpleLogger
+ * @author Frédéric Guillot
+ */
+class Logger extends AbstractLogger implements LoggerAwareInterface
+{
+ /**
+ * Logger instances
+ *
+ * @access private
+ */
+ private $loggers = array();
+
+ /**
+ * Get level priority
+ *
+ * @param mixed $level
+ * @return integer
+ */
+ public function getLevelPriority($level)
+ {
+ switch ($level) {
+ case LogLevel::EMERGENCY:
+ return 600;
+ case LogLevel::ALERT:
+ return 550;
+ case LogLevel::CRITICAL:
+ return 500;
+ case LogLevel::ERROR:
+ return 400;
+ case LogLevel::WARNING:
+ return 300;
+ case LogLevel::NOTICE:
+ return 250;
+ case LogLevel::INFO:
+ return 200;
+ }
+
+ return 100;
+ }
+
+ /**
+ * Sets a logger instance on the object
+ *
+ * @param LoggerInterface $logger
+ * @return null
+ */
+ public function setLogger(LoggerInterface $logger)
+ {
+ $this->loggers[] = $logger;
+ }
+
+ /**
+ * Proxy method to the real loggers
+ *
+ * @param mixed $level
+ * @param string $message
+ * @param array $context
+ * @return null
+ */
+ public function log($level, $message, array $context = array())
+ {
+ foreach ($this->loggers as $logger) {
+ if ($this->getLevelPriority($level) >= $this->getLevelPriority($logger->getLevel())) {
+ $logger->log($level, $message, $context);
+ }
+ }
+ }
+
+ /**
+ * Dump variables for debugging
+ *
+ * @param mixed $variable
+ */
+ public function dump($variable)
+ {
+ foreach ($this->loggers as $logger) {
+ if ($this->getLevelPriority(LogLevel::DEBUG) >= $this->getLevelPriority($logger->getLevel())) {
+ $logger->dump($variable);
+ }
+ }
+ }
+}
diff --git a/vendor/fguillot/simpleLogger/src/SimpleLogger/Stderr.php b/vendor/fguillot/simpleLogger/src/SimpleLogger/Stderr.php
new file mode 100644
index 00000000..2573177e
--- /dev/null
+++ b/vendor/fguillot/simpleLogger/src/SimpleLogger/Stderr.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace SimpleLogger;
+
+/**
+ * Stderr logger
+ *
+ * @package SimpleLogger
+ * @author Frédéric Guillot
+ */
+class Stderr extends Base
+{
+ /**
+ * Logs with an arbitrary level.
+ *
+ * @param mixed $level
+ * @param string $message
+ * @param array $context
+ * @return null
+ */
+ public function log($level, $message, array $context = array())
+ {
+ file_put_contents('php://stderr', $this->formatMessage($level, $message, $context), FILE_APPEND);
+ }
+}
diff --git a/vendor/fguillot/simpleLogger/src/SimpleLogger/Stdout.php b/vendor/fguillot/simpleLogger/src/SimpleLogger/Stdout.php
new file mode 100644
index 00000000..82d181b2
--- /dev/null
+++ b/vendor/fguillot/simpleLogger/src/SimpleLogger/Stdout.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace SimpleLogger;
+
+/**
+ * Stdout logger
+ *
+ * @package SimpleLogger
+ * @author Frédéric Guillot
+ */
+class Stdout extends Base
+{
+ /**
+ * Logs with an arbitrary level.
+ *
+ * @param mixed $level
+ * @param string $message
+ * @param array $context
+ * @return null
+ */
+ public function log($level, $message, array $context = array())
+ {
+ file_put_contents('php://stdout', $this->formatMessage($level, $message, $context), FILE_APPEND);
+ }
+}
diff --git a/vendor/fguillot/simpleLogger/src/SimpleLogger/Syslog.php b/vendor/fguillot/simpleLogger/src/SimpleLogger/Syslog.php
new file mode 100644
index 00000000..c4e26a7a
--- /dev/null
+++ b/vendor/fguillot/simpleLogger/src/SimpleLogger/Syslog.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace SimpleLogger;
+
+use RuntimeException;
+use Psr\Log\LogLevel;
+
+/**
+ * Syslog Logger
+ *
+ * @package SimpleLogger
+ * @author Frédéric Guillot
+ */
+class Syslog extends Base
+{
+ /**
+ * Setup Syslog configuration
+ *
+ * @param string $ident Application name
+ * @param int $facility See http://php.net/manual/en/function.openlog.php
+ */
+ public function __construct($ident = 'PHP', $facility = LOG_USER)
+ {
+ if (! openlog($ident, LOG_ODELAY | LOG_PID, $facility)) {
+ throw new RuntimeException('Unable to connect to syslog.');
+ }
+ }
+
+ /**
+ * Get syslog priority according to Psr\LogLevel
+ *
+ * @param mixed $level
+ * @return integer
+ */
+ public function getSyslogPriority($level)
+ {
+ switch ($level) {
+ case LogLevel::EMERGENCY:
+ return LOG_EMERG;
+ case LogLevel::ALERT:
+ return LOG_ALERT;
+ case LogLevel::CRITICAL:
+ return LOG_CRIT;
+ case LogLevel::ERROR:
+ return LOG_ERR;
+ case LogLevel::WARNING:
+ return LOG_WARNING;
+ case LogLevel::NOTICE:
+ return LOG_NOTICE;
+ case LogLevel::INFO:
+ return LOG_INFO;
+ }
+
+ return LOG_DEBUG;
+ }
+
+ /**
+ * Logs with an arbitrary level.
+ *
+ * @param mixed $level
+ * @param string $message
+ * @param array $context
+ * @return null
+ */
+ public function log($level, $message, array $context = array())
+ {
+ $syslogPriority = $this->getSyslogPriority($level);
+ $syslogMessage = $this->interpolate($message, $context);
+
+ syslog($syslogPriority, $syslogMessage);
+ }
+}