From 575b70060479f404656a87c52bf64084ecd8459c Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sat, 23 Feb 2019 20:29:45 +0100 Subject: [PATCH] implemented logging --- lam-packaging/debian/copyright | 1 + lam-packaging/debian/postinst | 2 +- lam/copyright | 1 + lam/lib/3rdParty/Psr/Log/AbstractLogger.php | 128 +++++++++++++++ .../Psr/Log/InvalidArgumentException.php | 7 + lam/lib/3rdParty/Psr/Log/LogLevel.php | 18 +++ .../3rdParty/Psr/Log/LoggerAwareInterface.php | 18 +++ lam/lib/3rdParty/Psr/Log/LoggerAwareTrait.php | 26 ++++ lam/lib/3rdParty/Psr/Log/LoggerInterface.php | 123 +++++++++++++++ lam/lib/3rdParty/Psr/Log/LoggerTrait.php | 140 +++++++++++++++++ lam/lib/3rdParty/Psr/Log/NullLogger.php | 28 ++++ .../Psr/Log/Test/LoggerInterfaceTest.php | 144 +++++++++++++++++ lam/lib/3rdParty/Psr/Log/Test/TestLogger.php | 146 ++++++++++++++++++ lam/lib/security.inc | 14 +- 14 files changed, 793 insertions(+), 3 deletions(-) create mode 100644 lam/lib/3rdParty/Psr/Log/AbstractLogger.php create mode 100644 lam/lib/3rdParty/Psr/Log/InvalidArgumentException.php create mode 100644 lam/lib/3rdParty/Psr/Log/LogLevel.php create mode 100644 lam/lib/3rdParty/Psr/Log/LoggerAwareInterface.php create mode 100644 lam/lib/3rdParty/Psr/Log/LoggerAwareTrait.php create mode 100644 lam/lib/3rdParty/Psr/Log/LoggerInterface.php create mode 100644 lam/lib/3rdParty/Psr/Log/LoggerTrait.php create mode 100644 lam/lib/3rdParty/Psr/Log/NullLogger.php create mode 100644 lam/lib/3rdParty/Psr/Log/Test/LoggerInterfaceTest.php create mode 100644 lam/lib/3rdParty/Psr/Log/Test/TestLogger.php diff --git a/lam-packaging/debian/copyright b/lam-packaging/debian/copyright index 706da6d5..e2d67758 100644 --- a/lam-packaging/debian/copyright +++ b/lam-packaging/debian/copyright @@ -230,6 +230,7 @@ lib/3rdParty/tcpdf/fonts/DejaVu*.ttf A Public Domain, Bitstream, I lib/3rdParty/tcpdf/fonts/DejaVu*.z A Public Domain, Bitstream, Inc., Tavmjong Bah lib/3rdParty/phpseclib B Jim Wigginton lib/3rdParty/Monolog B 2011 Jordi Boggiano +lib/3rdParty/Psr B 2012 PHP Framework Interoperability Group lib/3rdParty/yubico/Yubico.php C 2015 Yubico AB templates/lib/*jquery*.js B 2018 jQuery Foundation and other contributors style/120_jquery-ui*.css B 2016 jQuery Foundation and other contributors diff --git a/lam-packaging/debian/postinst b/lam-packaging/debian/postinst index 66b38133..c3caa797 100755 --- a/lam-packaging/debian/postinst +++ b/lam-packaging/debian/postinst @@ -10,7 +10,7 @@ fi db_version 2.0 || [ $? -lt 30 ] # 3rd party libs -phpThirdPartyLibs='phpseclib tcpdf Monolog' +phpThirdPartyLibs='phpseclib tcpdf Monolog Psr' for phpThirdPartyLib in $phpThirdPartyLibs; do if [ ! -L /usr/share/ldap-account-manager/lib/3rdParty/${phpThirdPartyLib} ] ; then ln -s /usr/share/php/${phpThirdPartyLib} /usr/share/ldap-account-manager/lib/3rdParty/${phpThirdPartyLib} diff --git a/lam/copyright b/lam/copyright index 74b958a4..d97d3f2d 100644 --- a/lam/copyright +++ b/lam/copyright @@ -229,6 +229,7 @@ lib/3rdParty/tcpdf/fonts/DejaVu*.ttf A Public Domain, Bitstream, I lib/3rdParty/tcpdf/fonts/DejaVu*.z A Public Domain, Bitstream, Inc., Tavmjong Bah lib/3rdParty/phpseclib B Jim Wigginton lib/3rdParty/Monolog B 2011 Jordi Boggiano +lib/3rdParty/Psr B 2012 PHP Framework Interoperability Group lib/3rdParty/yubico/Yubico.php C 2015 Yubico AB templates/lib/*jquery*.js B 2018 jQuery Foundation and other contributors style/120_jquery-ui*.css B 2016 jQuery Foundation and other contributors diff --git a/lam/lib/3rdParty/Psr/Log/AbstractLogger.php b/lam/lib/3rdParty/Psr/Log/AbstractLogger.php new file mode 100644 index 00000000..90e721af --- /dev/null +++ b/lam/lib/3rdParty/Psr/Log/AbstractLogger.php @@ -0,0 +1,128 @@ +log(LogLevel::EMERGENCY, $message, $context); + } + + /** + * 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 $context + * + * @return void + */ + public function alert($message, array $context = array()) + { + $this->log(LogLevel::ALERT, $message, $context); + } + + /** + * Critical conditions. + * + * Example: Application component unavailable, unexpected exception. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function critical($message, array $context = array()) + { + $this->log(LogLevel::CRITICAL, $message, $context); + } + + /** + * Runtime errors that do not require immediate action but should typically + * be logged and monitored. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function error($message, array $context = array()) + { + $this->log(LogLevel::ERROR, $message, $context); + } + + /** + * 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 $context + * + * @return void + */ + public function warning($message, array $context = array()) + { + $this->log(LogLevel::WARNING, $message, $context); + } + + /** + * Normal but significant events. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function notice($message, array $context = array()) + { + $this->log(LogLevel::NOTICE, $message, $context); + } + + /** + * Interesting events. + * + * Example: User logs in, SQL logs. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function info($message, array $context = array()) + { + $this->log(LogLevel::INFO, $message, $context); + } + + /** + * Detailed debug information. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function debug($message, array $context = array()) + { + $this->log(LogLevel::DEBUG, $message, $context); + } +} diff --git a/lam/lib/3rdParty/Psr/Log/InvalidArgumentException.php b/lam/lib/3rdParty/Psr/Log/InvalidArgumentException.php new file mode 100644 index 00000000..67f852d1 --- /dev/null +++ b/lam/lib/3rdParty/Psr/Log/InvalidArgumentException.php @@ -0,0 +1,7 @@ +logger = $logger; + } +} diff --git a/lam/lib/3rdParty/Psr/Log/LoggerInterface.php b/lam/lib/3rdParty/Psr/Log/LoggerInterface.php new file mode 100644 index 00000000..5ea72438 --- /dev/null +++ b/lam/lib/3rdParty/Psr/Log/LoggerInterface.php @@ -0,0 +1,123 @@ +log(LogLevel::EMERGENCY, $message, $context); + } + + /** + * 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 $context + * + * @return void + */ + public function alert($message, array $context = array()) + { + $this->log(LogLevel::ALERT, $message, $context); + } + + /** + * Critical conditions. + * + * Example: Application component unavailable, unexpected exception. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function critical($message, array $context = array()) + { + $this->log(LogLevel::CRITICAL, $message, $context); + } + + /** + * Runtime errors that do not require immediate action but should typically + * be logged and monitored. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function error($message, array $context = array()) + { + $this->log(LogLevel::ERROR, $message, $context); + } + + /** + * 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 $context + * + * @return void + */ + public function warning($message, array $context = array()) + { + $this->log(LogLevel::WARNING, $message, $context); + } + + /** + * Normal but significant events. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function notice($message, array $context = array()) + { + $this->log(LogLevel::NOTICE, $message, $context); + } + + /** + * Interesting events. + * + * Example: User logs in, SQL logs. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function info($message, array $context = array()) + { + $this->log(LogLevel::INFO, $message, $context); + } + + /** + * Detailed debug information. + * + * @param string $message + * @param array $context + * + * @return void + */ + public function debug($message, array $context = array()) + { + $this->log(LogLevel::DEBUG, $message, $context); + } + + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * + * @return void + */ + abstract public function log($level, $message, array $context = array()); +} diff --git a/lam/lib/3rdParty/Psr/Log/NullLogger.php b/lam/lib/3rdParty/Psr/Log/NullLogger.php new file mode 100644 index 00000000..d8cd682c --- /dev/null +++ b/lam/lib/3rdParty/Psr/Log/NullLogger.php @@ -0,0 +1,28 @@ +logger) { }` + * blocks. + */ +class NullLogger extends AbstractLogger +{ + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * + * @return void + */ + public function log($level, $message, array $context = array()) + { + // noop + } +} diff --git a/lam/lib/3rdParty/Psr/Log/Test/LoggerInterfaceTest.php b/lam/lib/3rdParty/Psr/Log/Test/LoggerInterfaceTest.php new file mode 100644 index 00000000..4b861c3e --- /dev/null +++ b/lam/lib/3rdParty/Psr/Log/Test/LoggerInterfaceTest.php @@ -0,0 +1,144 @@ + ". + * + * Example ->error('Foo') would yield "error Foo". + * + * @return string[] + */ + abstract public function getLogs(); + + public function testImplements() + { + $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); + } + + /** + * @dataProvider provideLevelsAndMessages + */ + public function testLogsAtAllLevels($level, $message) + { + $logger = $this->getLogger(); + $logger->{$level}($message, array('user' => 'Bob')); + $logger->log($level, $message, array('user' => 'Bob')); + + $expected = array( + $level.' message of level '.$level.' with context: Bob', + $level.' message of level '.$level.' with context: Bob', + ); + $this->assertEquals($expected, $this->getLogs()); + } + + public function provideLevelsAndMessages() + { + return array( + LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'), + LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'), + LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'), + LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'), + LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'), + LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'), + LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'), + LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'), + ); + } + + /** + * @expectedException \Psr\Log\InvalidArgumentException + */ + public function testThrowsOnInvalidLevel() + { + $logger = $this->getLogger(); + $logger->log('invalid level', 'Foo'); + } + + public function testContextReplacement() + { + $logger = $this->getLogger(); + $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar')); + + $expected = array('info {Message {nothing} Bob Bar a}'); + $this->assertEquals($expected, $this->getLogs()); + } + + public function testObjectCastToString() + { + if (method_exists($this, 'createPartialMock')) { + $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString')); + } else { + $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString')); + } + $dummy->expects($this->once()) + ->method('__toString') + ->will($this->returnValue('DUMMY')); + + $this->getLogger()->warning($dummy); + + $expected = array('warning DUMMY'); + $this->assertEquals($expected, $this->getLogs()); + } + + public function testContextCanContainAnything() + { + $closed = fopen('php://memory', 'r'); + fclose($closed); + + $context = array( + 'bool' => true, + 'null' => null, + 'string' => 'Foo', + 'int' => 0, + 'float' => 0.5, + 'nested' => array('with object' => new DummyTest), + 'object' => new \DateTime, + 'resource' => fopen('php://memory', 'r'), + 'closed' => $closed, + ); + + $this->getLogger()->warning('Crazy context data', $context); + + $expected = array('warning Crazy context data'); + $this->assertEquals($expected, $this->getLogs()); + } + + public function testContextExceptionKeyCanBeExceptionOrOtherValues() + { + $logger = $this->getLogger(); + $logger->warning('Random message', array('exception' => 'oops')); + $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail'))); + + $expected = array( + 'warning Random message', + 'critical Uncaught Exception!' + ); + $this->assertEquals($expected, $this->getLogs()); + } +} + +class DummyTest +{ + public function __toString() + { + } +} diff --git a/lam/lib/3rdParty/Psr/Log/Test/TestLogger.php b/lam/lib/3rdParty/Psr/Log/Test/TestLogger.php new file mode 100644 index 00000000..0cdffe4f --- /dev/null +++ b/lam/lib/3rdParty/Psr/Log/Test/TestLogger.php @@ -0,0 +1,146 @@ + $level, + 'message' => $message, + 'context' => $context, + ]; + + $this->recordsByLevel[$record['level']][] = $record; + $this->records[] = $record; + } + + public function hasRecords($level) + { + return isset($this->recordsByLevel[$level]); + } + + public function hasRecord($record, $level) + { + if (is_string($record)) { + $record = ['message' => $record]; + } + return $this->hasRecordThatPasses(function ($rec) use ($record) { + if ($rec['message'] !== $record['message']) { + return false; + } + if (isset($record['context']) && $rec['context'] !== $record['context']) { + return false; + } + return true; + }, $level); + } + + public function hasRecordThatContains($message, $level) + { + return $this->hasRecordThatPasses(function ($rec) use ($message) { + return strpos($rec['message'], $message) !== false; + }, $level); + } + + public function hasRecordThatMatches($regex, $level) + { + return $this->hasRecordThatPasses(function ($rec) use ($regex) { + return preg_match($regex, $rec['message']) > 0; + }, $level); + } + + public function hasRecordThatPasses(callable $predicate, $level) + { + if (!isset($this->recordsByLevel[$level])) { + return false; + } + foreach ($this->recordsByLevel[$level] as $i => $rec) { + if (call_user_func($predicate, $rec, $i)) { + return true; + } + } + return false; + } + + public function __call($method, $args) + { + if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) { + $genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3]; + $level = strtolower($matches[2]); + if (method_exists($this, $genericMethod)) { + $args[] = $level; + return call_user_func_array([$this, $genericMethod], $args); + } + } + throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()'); + } + + public function reset() + { + $this->records = []; + } +} diff --git a/lam/lib/security.inc b/lam/lib/security.inc index c32e382a..af8f45aa 100644 --- a/lam/lib/security.inc +++ b/lam/lib/security.inc @@ -759,12 +759,22 @@ function lamEncryptionAlgo() { * @param LAMCfgMain $cfgMain main configuration */ function lamLogRemoteMessage($level, $message, $cfgMain) { + include_once __DIR__ . '/3rdParty/Psr/Log/InvalidArgumentException.php'; + include_once __DIR__ . '/3rdParty/Psr/Log/LoggerInterface.php'; + include_once __DIR__ . '/3rdParty/Monolog/ResettableInterface.php'; include_once __DIR__ . '/3rdParty/Monolog/Logger.php'; + include_once __DIR__ . '/3rdParty/Monolog/Formatter/FormatterInterface.php'; + include_once __DIR__ . '/3rdParty/Monolog/Formatter/NormalizerFormatter.php'; include_once __DIR__ . '/3rdParty/Monolog/Formatter/LineFormatter.php'; + include_once __DIR__ . '/3rdParty/Monolog/Handler/HandlerInterface.php'; + include_once __DIR__ . '/3rdParty/Monolog/Handler/AbstractHandler.php'; + include_once __DIR__ . '/3rdParty/Monolog/Handler/AbstractProcessingHandler.php'; + include_once __DIR__ . '/3rdParty/Monolog/Handler/AbstractSyslogHandler.php'; + include_once __DIR__ . '/3rdParty/Monolog/Handler/SyslogUdp/UdpSocket.php'; include_once __DIR__ . '/3rdParty/Monolog/Handler/SyslogUdpHandler.php'; $remoteParts = explode(':', $cfgMain->logDestination); - $server = $remoteParts[0]; - $port = $remoteParts[1]; + $server = $remoteParts[1]; + $port = intval($remoteParts[2]); $output = "%channel%.%level_name%: %message%"; $formatter = new Monolog\Formatter\LineFormatter($output); $logger = new Monolog\Logger('lam');