diff options
author | Olivier Paroz <github@oparoz.com> | 2015-01-07 22:55:27 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-01-07 22:55:27 +0300 |
commit | f722a45d5983d257ccd0605b8bd98cb959dc8048 (patch) | |
tree | b9db8a8c6671135feb08cc6cf1789fdcb2114766 | |
parent | cc1f908d222b82747821c8b9ebff5b1bb9b6372b (diff) |
Integrate the smarter logger
* No need to patch ownCloud any more
* Can get rid of adding the appName every time we need to log a simple message
* Converts Objects, Arrays and Exceptions to String so that those can be read from the log file
-rw-r--r-- | README.md | 12 | ||||
-rw-r--r-- | appinfo/application.php | 31 | ||||
-rw-r--r-- | controller/pagecontroller.php | 15 | ||||
-rw-r--r-- | controller/publicservicecontroller.php | 7 | ||||
-rw-r--r-- | controller/servicecontroller.php | 10 | ||||
-rw-r--r-- | middleware/checkmiddleware.php | 29 | ||||
-rw-r--r-- | middleware/sessionmiddleware.php | 10 | ||||
-rw-r--r-- | middleware/sharingcheckmiddleware.php | 7 | ||||
-rw-r--r-- | middleware/tokencheckmiddleware.php | 6 | ||||
-rw-r--r-- | patches/smarter-logger.patch | 184 | ||||
-rw-r--r-- | service/environmentservice.php | 8 | ||||
-rw-r--r-- | service/infoservice.php | 9 | ||||
-rw-r--r-- | service/previewservice.php | 22 | ||||
-rw-r--r-- | service/service.php | 18 | ||||
-rw-r--r-- | service/thumbnailservice.php | 10 | ||||
-rw-r--r-- | utility/smarterlogger.php | 261 |
16 files changed, 324 insertions, 315 deletions
@@ -29,13 +29,6 @@ The AppFramework has a problem with sessions, but it can be fixed via this patch $ patch -p1 < apps/galleryplus/patches/session-template-fix.patch ``` -### Smarter logger (mandatory) -While in beta, you'll need to be able to dump objects to the log in order to be able to quickly find the information you need. - -``` -$ patch -p1 < apps/galleryplus/patches/smarter-logger.patch -``` - ### Supporting more media types First, make sure you have installed ImageMagick and its PECL extension. Then, we can patch ownCloud @@ -76,7 +69,6 @@ $ git clone -b stable7 https://github.com/interfasys/galleryplus.git` Now you can activate it in the apps menu. It's called Gallery+ ## List of patches -1. smarter-logger.patch - Logger patch to be able to easily have access to objects, arrays, etc. -2. bitmap_preview.patch - Adds support for Photoshop, Illustrator, TIFF, Postscript -3. session-template-fix.patch - Fixes AppFramework sessions for public shares +1. bitmap_preview.patch - Adds support for Photoshop, Illustrator, TIFF, Postscript +2. session-template-fix.patch - Fixes AppFramework sessions for public shares diff --git a/appinfo/application.php b/appinfo/application.php index dc59024a..ec5dc64a 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -27,6 +27,7 @@ use OCA\GalleryPlus\Service\PreviewService; use OCA\GalleryPlus\Middleware\SharingCheckMiddleware; use OCA\GalleryPlus\Middleware\TokenCheckMiddleware; use OCA\GalleryPlus\Middleware\SessionMiddleware; +use OCA\GalleryPlus\Utility\SmarterLogger; /** @@ -56,7 +57,6 @@ class Application extends App { $c->query('Request'), $c->query('Environment'), $c->query('URLGenerator'), - $c->query('Logger'), $c->query('API') ); } @@ -70,8 +70,7 @@ class Application extends App { $c->query('InfoService'), $c->query('ThumbnailService'), $c->query('PreviewService'), - $c->query('URLGenerator'), - $c->query('Logger') + $c->query('URLGenerator') ); } ); @@ -84,8 +83,7 @@ class Application extends App { $c->query('InfoService'), $c->query('ThumbnailService'), $c->query('PreviewService'), - $c->query('URLGenerator'), - $c->query('Logger') + $c->query('URLGenerator') ); } ); @@ -144,6 +142,14 @@ class Application extends App { } ); $container->registerService( + 'SmarterLogger', function (IAppContainer $c) { + return new SmarterLogger( + $c->query('AppName'), + $c->query('Logger') + ); + } + ); + $container->registerService( 'RootFolder', function (IAppContainer $c) { return $c ->getServer() @@ -191,7 +197,7 @@ class Application extends App { $c->query('UserFolder'), $c->query('UserManager'), $c->getServer(), - $c->query('Logger') + $c->query('SmarterLogger') ); } ); @@ -211,7 +217,7 @@ class Application extends App { $c->query('AppName'), $c->query('UserFolder'), $c->query('Environment'), - $c->query('Logger'), + $c->query('SmarterLogger'), $c->query('PreviewManager') ); } @@ -220,7 +226,7 @@ class Application extends App { 'ThumbnailService', function (IContainer $c) { return new ThumbnailService( $c->query('AppName'), - $c->query('Logger'), + $c->query('SmarterLogger'), $c->query('PreviewService') ); } @@ -230,7 +236,7 @@ class Application extends App { return new PreviewService( $c->query('AppName'), $c->query('Environment'), - $c->query('Logger'), + $c->query('SmarterLogger'), $c->query('PreviewManager') ); } @@ -251,7 +257,7 @@ class Application extends App { //$c->query('ControllerMethodReflector'), // Available in OC8. https://github.com/owncloud/core/pull/12839 $c->query('Reflector'), $c->query('URLGenerator'), - $c->query('Logger') + $c->query('SmarterLogger') ); } ); @@ -272,7 +278,7 @@ class Application extends App { //$c->query('ControllerMethodReflector'), // Available in OC8. https://github.com/owncloud/core/pull/12839 $c->query('Reflector'), $c->query('URLGenerator'), - $c->query('Logger') + $c->query('SmarterLogger') ); } ); @@ -283,8 +289,7 @@ class Application extends App { $c->query('Request'), //$c->query('ControllerMethodReflector'), // Available in OC8. https://github.com/owncloud/core/pull/12839 $c->query('Reflector'), - $c->query('UserSession'), - $c->query('Logger') + $c->query('UserSession') ); } ); diff --git a/controller/pagecontroller.php b/controller/pagecontroller.php index 1895f143..388d6301 100644 --- a/controller/pagecontroller.php +++ b/controller/pagecontroller.php @@ -14,7 +14,6 @@ namespace OCA\GalleryPlus\Controller; -use OCP\ILogger; use OCP\IURLGenerator; use OCP\IRequest; @@ -41,10 +40,6 @@ class PageController extends Controller { */ private $urlGenerator; /** - * @type ILogger - */ - private $logger; - /** * @type IApi */ private $api; @@ -56,7 +51,6 @@ class PageController extends Controller { * @param IRequest $request * @param EnvironmentService $environmentService * @param IURLGenerator $urlGenerator - * @param ILogger $logger * @param IApi $api */ public function __construct( @@ -64,14 +58,12 @@ class PageController extends Controller { IRequest $request, EnvironmentService $environmentService, IURLGenerator $urlGenerator, - ILogger $logger, IApi $api ) { parent::__construct($appName, $request); $this->environmentService = $environmentService; $this->urlGenerator = $urlGenerator; - $this->logger = $logger; $this->api = $api; } @@ -115,13 +107,6 @@ class PageController extends Controller { $token = $this->request->getParam('token'); $appName = $this->appName; $env = $this->environmentService->getEnv(); - /*$this->logger->debug( - 'This is the env : {env}', - array( - 'app' => $appName, - 'env' => $env - ) - );*/ $displayName = $env['originalOwnerDisplayName']; $albumName = $env['albumName']; diff --git a/controller/publicservicecontroller.php b/controller/publicservicecontroller.php index 582dcfa1..d3a151d0 100644 --- a/controller/publicservicecontroller.php +++ b/controller/publicservicecontroller.php @@ -12,7 +12,6 @@ namespace OCA\GalleryPlus\Controller; -use OCP\ILogger; use OCP\IURLGenerator; use OCP\IRequest; @@ -38,8 +37,7 @@ class PublicServiceController extends ServiceController { InfoService $infoService, ThumbnailService $thumbnailService, PreviewService $previewService, - IURLGenerator $urlGenerator, - ILogger $logger + IURLGenerator $urlGenerator ) { parent::__construct( $appName, @@ -47,8 +45,7 @@ class PublicServiceController extends ServiceController { $infoService, $thumbnailService, $previewService, - $urlGenerator, - $logger + $urlGenerator ); } diff --git a/controller/servicecontroller.php b/controller/servicecontroller.php index 852c0e3d..dcce7c11 100644 --- a/controller/servicecontroller.php +++ b/controller/servicecontroller.php @@ -14,7 +14,6 @@ namespace OCA\GalleryPlus\Controller; -use OCP\ILogger; use OCP\IURLGenerator; use OCP\IRequest; @@ -51,10 +50,6 @@ class ServiceController extends Controller { * @type IURLGenerator */ private $urlGenerator; - /** - * @type ILogger - */ - private $logger; /** * Constructor @@ -65,7 +60,6 @@ class ServiceController extends Controller { * @param ThumbnailService $thumbnailService * @param PreviewService $previewService * @param IURLGenerator $urlGenerator - * @param ILogger $logger */ public function __construct( $appName, @@ -73,8 +67,7 @@ class ServiceController extends Controller { InfoService $infoService, ThumbnailService $thumbnailService, PreviewService $previewService, - IURLGenerator $urlGenerator, - ILogger $logger + IURLGenerator $urlGenerator ) { parent::__construct($appName, $request); @@ -82,7 +75,6 @@ class ServiceController extends Controller { $this->thumbnailService = $thumbnailService; $this->previewService = $previewService; $this->urlGenerator = $urlGenerator; - $this->logger = $logger; } /** diff --git a/middleware/checkmiddleware.php b/middleware/checkmiddleware.php index 79dd2e10..7d2e2b0c 100644 --- a/middleware/checkmiddleware.php +++ b/middleware/checkmiddleware.php @@ -15,7 +15,6 @@ namespace OCA\GalleryPlus\Middleware; use OCP\IURLGenerator; -use OCP\ILogger; use OCP\IRequest; use OCP\AppFramework\Http\JSONResponse; @@ -23,6 +22,8 @@ use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Middleware; +use OCA\GalleryPlus\Utility\SmarterLogger; + /** * Checks that we have a valid token linked to a valid resource and that the * user is authorised to access it @@ -44,7 +45,7 @@ abstract class CheckMiddleware extends Middleware { */ private $urlGenerator; /** - * @type ILogger + * @type SmarterLogger */ private $logger; @@ -54,13 +55,13 @@ abstract class CheckMiddleware extends Middleware { * @param string $appName * @param IRequest $request * @param IURLGenerator $urlGenerator - * @param ILogger $logger + * @param SmarterLogger $logger */ public function __construct( $appName, IRequest $request, IURLGenerator $urlGenerator, - ILogger $logger + SmarterLogger $logger ) { $this->appName = $appName; $this->request = $request; @@ -79,14 +80,12 @@ abstract class CheckMiddleware extends Middleware { $controller, $methodName, \Exception $exception ) { if ($exception instanceof CheckException) { - $appName = $this->appName; $message = $exception->getMessage(); $code = $exception->getCode(); $this->logger->debug( "[TokenCheckException] {message} ({code})", array( - 'app' => $appName, 'message' => $message, 'code' => $code ) @@ -114,12 +113,7 @@ abstract class CheckMiddleware extends Middleware { * @return RedirectResponse|TemplateResponse */ private function sendHtmlResponse($message, $code) { - $this->logger->debug( - "[CheckException] HTML response", - array( - 'app' => $this->appName - ) - ); + $this->logger->debug("[CheckException] HTML response"); /** * We need to render a template for 401 or we'll have an endless loop as @@ -140,19 +134,17 @@ abstract class CheckMiddleware extends Middleware { * @return TemplateResponse */ private function sendHtml401() { - $appName = $this->appName; $params = $this->request->getParams(); $this->logger->debug( '[CheckException] Unauthorised Request params: {params}', array( - 'app' => $appName, 'params' => $params ) ); return new TemplateResponse( - $appName, 'authenticate', $params, + $this->appName, 'authenticate', $params, 'guest' ); } @@ -186,12 +178,7 @@ abstract class CheckMiddleware extends Middleware { * @return JSONResponse */ private function sendJsonResponse($message, $code) { - $this->logger->debug( - "[TokenCheckException] JSON response", - array( - 'app' => $this->appName - ) - ); + $this->logger->debug("[TokenCheckException] JSON response"); $jsonData = array( 'message' => $message, diff --git a/middleware/sessionmiddleware.php b/middleware/sessionmiddleware.php index 9e351e26..0e6be5f2 100644 --- a/middleware/sessionmiddleware.php +++ b/middleware/sessionmiddleware.php @@ -19,7 +19,6 @@ use \OC\AppFramework\Utility\ControllerMethodReflector; use OCP\IRequest; use OCP\ISession; -use OCP\ILogger; use OCP\AppFramework\Http\Response; use OCP\AppFramework\Middleware; @@ -46,27 +45,20 @@ class SessionMiddleware extends Middleware { * @type ISession */ private $session; - /** - * @type ILogger - */ - private $logger; /** * @param IRequest $request * @param ControllerMethodReflector $reflector * @param ISession $session - * @param ILogger $logger */ public function __construct( IRequest $request, ControllerMethodReflector $reflector, - ISession $session, - ILogger $logger + ISession $session ) { $this->request = $request; $this->reflector = $reflector; $this->session = $session; - $this->logger = $logger; } /** diff --git a/middleware/sharingcheckmiddleware.php b/middleware/sharingcheckmiddleware.php index a39f3940..5e353666 100644 --- a/middleware/sharingcheckmiddleware.php +++ b/middleware/sharingcheckmiddleware.php @@ -20,10 +20,11 @@ use \OC\AppFramework\Utility\ControllerMethodReflector; use OCP\IAppConfig; use OCP\IRequest; use OCP\IURLGenerator; -use OCP\ILogger; use OCP\AppFramework\Http; +use OCA\GalleryPlus\Utility\SmarterLogger; + /** * Checks whether the "sharing check" is enabled * @@ -48,7 +49,7 @@ class SharingCheckMiddleware extends CheckMiddleware { * @param IAppConfig $appConfig * @param ControllerMethodReflector $reflector * @param IURLGenerator $urlGenerator - * @param ILogger $logger + * @param SmarterLogger $logger */ public function __construct( $appName, @@ -56,7 +57,7 @@ class SharingCheckMiddleware extends CheckMiddleware { IAppConfig $appConfig, ControllerMethodReflector $reflector, IURLGenerator $urlGenerator, - ILogger $logger + SmarterLogger $logger ) { parent::__construct( $appName, diff --git a/middleware/tokencheckmiddleware.php b/middleware/tokencheckmiddleware.php index 24a66e71..fcfe8f90 100644 --- a/middleware/tokencheckmiddleware.php +++ b/middleware/tokencheckmiddleware.php @@ -19,12 +19,12 @@ use \OC\AppFramework\Utility\ControllerMethodReflector; use OCP\IRequest; use OCP\IURLGenerator; -use OCP\ILogger; use OCP\AppFramework\Http; use OCA\GalleryPlus\Service\EnvironmentService; use OCA\GalleryPlus\Service\ServiceException; +use OCA\GalleryPlus\Utility\SmarterLogger; /** @@ -52,7 +52,7 @@ class TokenCheckMiddleware extends CheckMiddleware { * @param EnvironmentService $environmentService * @param ControllerMethodReflector $reflector * @param IURLGenerator $urlGenerator - * @param ILogger $logger + * @param SmarterLogger $logger */ public function __construct( $appName, @@ -60,7 +60,7 @@ class TokenCheckMiddleware extends CheckMiddleware { EnvironmentService $environmentService, ControllerMethodReflector $reflector, IURLGenerator $urlGenerator, - ILogger $logger + SmarterLogger $logger ) { parent::__construct( $appName, diff --git a/patches/smarter-logger.patch b/patches/smarter-logger.patch deleted file mode 100644 index 5c8551c6..00000000 --- a/patches/smarter-logger.patch +++ /dev/null @@ -1,184 +0,0 @@ -From 0e29d7a0580bd23a0bb13ae389f6bc5a78aca411 Mon Sep 17 00:00:00 2001 -From: Olivier Paroz <github@oparoz.com> -Date: Mon, 8 Dec 2014 04:13:23 +0100 -Subject: [PATCH 1/2] Dump Arrays, Objects and Exceptions to the log - -This is a first draft. It has only been tested with the ownlcloud -logger. -Some internal fields are JSON encoded several times which makes it -harder to read. - -Most of the normalizer is borrowed from Monolog - -https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/NormalizerFormatter.php ---- - lib/private/log.php | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++-- - 1 file changed, 82 insertions(+), 3 deletions(-) - -diff --git a/lib/private/log.php b/lib/private/log.php -index 021cc15..f441187 100644 ---- a/lib/private/log.php -+++ b/lib/private/log.php -@@ -1,6 +1,8 @@ - <?php - /** - * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> -+ * Copyright (c) 2014 Jordi Boggiano <j.boggiano@seld.be> -+ * Copyright (c) 2014 Olivier Paroz <owncloud@oparoz.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. -@@ -27,10 +29,10 @@ class Log implements ILogger { - /** - * @param string $logger The logger that should be used - */ -- public function __construct($logger=null) { -+ public function __construct($logger = null) { - // FIXME: Add this for backwards compatibility, should be fixed at some point probably -- if($logger === null) { -- $this->logger = 'OC_Log_'.ucfirst(\OC_Config::getValue('log_type', 'owncloud')); -+ if ($logger === null) { -+ $this->logger = 'OC_Log_' . ucfirst(\OC_Config::getValue('log_type', 'owncloud')); - call_user_func(array($this->logger, 'init')); - } else { - $this->logger = $logger; -@@ -147,6 +149,8 @@ public function log($level, $message, array $context = array()) { - // interpolate $message as defined in PSR-3 - $replace = array(); - foreach ($context as $key => $val) { -+ // Allows us to dump arrays, objects and exceptions to the log -+ $val = $this->normalize($val); - $replace['{' . $key . '}'] = $val; - } - -@@ -156,4 +160,79 @@ public function log($level, $message, array $context = array()) { - $logger = $this->logger; - call_user_func(array($logger, 'write'), $app, $message, $level); - } -+ -+ private function normalize($data) { -+ if (null === $data || is_scalar($data)) { -+ return $data; -+ } -+ -+ if (is_array($data) || $data instanceof \Traversable) { -+ $normalized = array(); -+ $count = 1; -+ foreach ($data as $key => $value) { -+ if ($count++ >= 1000) { -+ $normalized['...'] = 'Over 1000 items, aborting normalization'; -+ break; -+ } -+ $normalized[$key] = $this->normalize($value); -+ } -+ -+ //return $normalized; -+ return $this->toJson($normalized, true); -+ } -+ -+ if (is_object($data)) { -+ if ($data instanceof \Exception) { -+ return $this->normalizeException($data); -+ } -+ -+ $arrayObject = new \ArrayObject($data); -+ $serializedObject = $arrayObject->getArrayCopy(); -+ -+ return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($serializedObject, true)); -+ } -+ -+ if (is_resource($data)) { -+ return '[resource]'; -+ } -+ -+ return '[unknown(' . gettype($data) . ')]'; -+ } -+ -+ private function normalizeException(\Exception $e) { -+ $data = array( -+ 'class' => get_class($e), -+ 'message' => $e->getMessage(), -+ 'file' => $e->getFile() . ':' . $e->getLine(), -+ ); -+ $trace = $e->getTrace(); -+ foreach ($trace as $frame) { -+ if (isset($frame['file'])) { -+ $data['trace'][] = $frame['file'] . ':' . $frame['line']; -+ } else { -+ $data['trace'][] = $this->toJson($frame, true); -+ } -+ } -+ if ($previous = $e->getPrevious()) { -+ $data['previous'] = $this->normalizeException($previous); -+ } -+ -+ return $this->toJson($data, true); -+ } -+ -+ private function toJson($data, $ignoreErrors = false) { -+ // suppress json_encode errors since it's twitchy with some inputs -+ if ($ignoreErrors) { -+ if (version_compare(PHP_VERSION, '5.4.0', '>=')) { -+ return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); -+ } -+ -+ return @json_encode($data); -+ } -+ if (version_compare(PHP_VERSION, '5.4.0', '>=')) { -+ return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); -+ } -+ -+ return json_encode($data); -+ } - } - -From 6044bcb149211e8fe8b60998c6b1f9f9e4563490 Mon Sep 17 00:00:00 2001 -From: Olivier Paroz <github@oparoz.com> -Date: Mon, 8 Dec 2014 15:48:39 +0100 -Subject: [PATCH 2/2] Simpler Exception trace and PHP5.4+ only - ---- - lib/private/log.php | 22 +++++----------------- - 1 file changed, 5 insertions(+), 17 deletions(-) - -diff --git a/lib/private/log.php b/lib/private/log.php -index f441187..d7c9301 100644 ---- a/lib/private/log.php -+++ b/lib/private/log.php -@@ -205,14 +205,9 @@ private function normalizeException(\Exception $e) { - 'message' => $e->getMessage(), - 'file' => $e->getFile() . ':' . $e->getLine(), - ); -- $trace = $e->getTrace(); -- foreach ($trace as $frame) { -- if (isset($frame['file'])) { -- $data['trace'][] = $frame['file'] . ':' . $frame['line']; -- } else { -- $data['trace'][] = $this->toJson($frame, true); -- } -- } -+ $trace = $e->getTraceAsString(); -+ $data['trace'][] = $trace; -+ - if ($previous = $e->getPrevious()) { - $data['previous'] = $this->normalizeException($previous); - } -@@ -223,16 +218,9 @@ private function normalizeException(\Exception $e) { - private function toJson($data, $ignoreErrors = false) { - // suppress json_encode errors since it's twitchy with some inputs - if ($ignoreErrors) { -- if (version_compare(PHP_VERSION, '5.4.0', '>=')) { -- return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); -- } -- -- return @json_encode($data); -- } -- if (version_compare(PHP_VERSION, '5.4.0', '>=')) { -- return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); -+ return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); - } - -- return json_encode($data); -+ return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); - } - } diff --git a/service/environmentservice.php b/service/environmentservice.php index 25e1463a..b3513cbb 100644 --- a/service/environmentservice.php +++ b/service/environmentservice.php @@ -18,11 +18,12 @@ use OCP\Files\Folder; use OCP\IServerContainer; use OCP\IUser; use OCP\Share; -use OCP\ILogger; use OCP\IUserManager; use OCP\AppFramework\Http; +use OCA\GalleryPlus\Utility\SmarterLogger; + /** * Builds the environment so that the services have access to the proper user, * folder and files @@ -62,7 +63,7 @@ class EnvironmentService extends Service { * @param Folder|null $userFolder * @param IUserManager $userManager * @param IServerContainer $serverContainer - * @param ILogger $logger + * @param SmarterLogger $logger */ public function __construct( $appName, @@ -70,7 +71,7 @@ class EnvironmentService extends Service { $userFolder, IUserManager $userManager, IServerContainer $serverContainer, - ILogger $logger + SmarterLogger $logger ) { parent::__construct($appName, $logger); @@ -267,7 +268,6 @@ class EnvironmentService extends Service { /*$this->logger->debug( 'Full Path {origSharePath}, relative path {origShareRelPath}', array( - 'app' => $this->$appName, 'origSharePath' => $origSharePath, 'origShareRelPath' => $origShareRelPath ) diff --git a/service/infoservice.php b/service/infoservice.php index bac2b870..2a5fd6a5 100644 --- a/service/infoservice.php +++ b/service/infoservice.php @@ -15,11 +15,11 @@ namespace OCA\GalleryPlus\Service; use OCP\Files\Folder; use OCP\Files\File; use OCP\Files\NotFoundException; -use OCP\ILogger; use OCP\IPreview; use OCP\AppFramework\Http; +use OCA\GalleryPlus\Utility\SmarterLogger; /** * Contains various methods which provide initial information about the * supported media types, the folder permissions and the images contained in @@ -75,14 +75,14 @@ class InfoService extends Service { * @param string $appName * @param Folder|null $userFolder * @param EnvironmentService $environmentService - * @param ILogger $logger + * @param SmarterLogger $logger * @param IPreview $previewManager */ public function __construct( $appName, $userFolder, EnvironmentService $environmentService, - ILogger $logger, + SmarterLogger $logger, IPreview $previewManager ) { parent::__construct($appName, $logger); @@ -150,7 +150,6 @@ class InfoService extends Service { $this->logger->debug( "Supported Mimes: {mimes}", array( - 'app' => $this->appName, 'mimes' => $supportedMimes ) ); @@ -178,7 +177,6 @@ class InfoService extends Service { /*$this->logger->debug( "Images array: {images}", array( - 'app' => $this->appName, 'images' => $result ) );*/ @@ -254,7 +252,6 @@ class InfoService extends Service { /*$this->logger->debug( "folderPath: {folderPath} pathRelativeToFolder: {pathRelativeToFolder} imagePath: {imagePath} mime: {mime}", array( - 'app' => $this->appName, 'folderPath' => $folderPath, 'pathRelativeToFolder' => $pathRelativeToFolder, 'imagePath' => $imagePath, diff --git a/service/previewservice.php b/service/previewservice.php index 59aab200..574003b3 100644 --- a/service/previewservice.php +++ b/service/previewservice.php @@ -17,11 +17,11 @@ namespace OCA\GalleryPlus\Service; use OCP\Files\File; use OCP\Files\Folder; use OCP\IPreview; -use OCP\ILogger; use OCP\AppFramework\Http; use OCA\GalleryPlus\Http\ImageResponse; +use OCA\GalleryPlus\Utility\SmarterLogger; /** * Generates previews @@ -62,13 +62,13 @@ class PreviewService extends Service { * * @param string $appName * @param EnvironmentService $environmentService - * @param ILogger $logger + * @param SmarterLogger $logger * @param IPreview $previewManager */ public function __construct( $appName, EnvironmentService $environmentService, - ILogger $logger, + SmarterLogger $logger, IPreview $previewManager ) { parent::__construct($appName, $logger); @@ -149,7 +149,6 @@ class PreviewService extends Service { * @return array preview data */ private function createPreview($image, $maxX = 0, $maxY = 0) { - $appName = $this->appName; $response = array(); $env = $this->environmentService->getEnv(); @@ -168,12 +167,7 @@ class PreviewService extends Service { $this->previewRequired($file, $preview); if ($previewRequired) { - $this->logger->debug( - "[PreviewService] Generating a new preview", - array( - 'app' => $appName - ) - ); + $this->logger->debug("[PreviewService] Generating a new preview"); $perfectPreview = $this->preparePreview($owner, $file, $preview, $maxX, $maxY); @@ -185,7 +179,6 @@ class PreviewService extends Service { $this->logger->debug( "[PreviewService] Downloading file {file} as-is", array( - 'app' => $appName, 'file' => $image ) ); @@ -208,7 +201,6 @@ class PreviewService extends Service { /*$this->logger->debug( "[PreviewService] PREVIEW Path : {path} / size: {size} / mime: {mimetype} / status: {status}", array( - 'app' => $appName, 'path' => $response['data']['path'], 'mimetype' => $response['data']['mimetype'], 'status' => $response['status'] @@ -260,7 +252,6 @@ class PreviewService extends Service { * @return array */ private function preparePreview($owner, $file, $preview, $maxX, $maxY) { - $appName = $this->appName; $keepAspect = $this->keepAspect; $scalingUp = false; // TODO: Need to read from settings $preview->setMaxX($maxX); @@ -285,10 +276,7 @@ class PreviewService extends Service { $statusCode = Http::STATUS_OK; } else { $this->logger->debug( - "[PreviewService] ERROR! Did not get a preview", - array( - 'app' => $appName - ) + "[PreviewService] ERROR! Did not get a preview" ); /** diff --git a/service/service.php b/service/service.php index 16652ccf..0a52858b 100644 --- a/service/service.php +++ b/service/service.php @@ -12,13 +12,14 @@ namespace OCA\GalleryPlus\Service; -use OCP\ILogger; use OCP\Files\Folder; use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\AppFramework\Http; +use OCA\GalleryPlus\Utility\SmarterLogger; + /** * Contains methods which all services will need * @@ -31,7 +32,7 @@ abstract class Service { */ protected $appName; /** - * @type ILogger + * @type SmarterLogger */ protected $logger; @@ -39,9 +40,9 @@ abstract class Service { * Constructor * * @param string $appName - * @param ILogger $logger + * @param SmarterLogger $logger */ - public function __construct($appName, ILogger $logger) { + public function __construct($appName, SmarterLogger $logger) { $this->appName = $appName; $this->logger = $logger; } @@ -80,13 +81,8 @@ abstract class Service { * @throws ServiceException */ protected function kaBoom($message, $code) { - $appName = $this->appName; - $this->logger->error( - $message . ' (' . $code . ')', - array( - 'app' => $appName - ) - ); + $this->logger->error($message . ' (' . $code . ')'); + throw new ServiceException( $message, $code diff --git a/service/thumbnailservice.php b/service/thumbnailservice.php index 483e6e54..6962bef7 100644 --- a/service/thumbnailservice.php +++ b/service/thumbnailservice.php @@ -15,7 +15,8 @@ namespace OCA\GalleryPlus\Service; use OCP\Util; -use OCP\ILogger; + +use OCA\GalleryPlus\Utility\SmarterLogger; /** * Collects and returns thumbnails for the list of images which is submitted to @@ -32,7 +33,7 @@ class ThumbnailService { */ private $appName; /** - * @type ILogger + * @type SmarterLogger */ private $logger; /** @@ -45,12 +46,12 @@ class ThumbnailService { * Constructor * * @param string $appName - * @param ILogger $logger + * @param SmarterLogger $logger * @param PreviewService $previewService */ public function __construct( $appName, - ILogger $logger, + SmarterLogger $logger, PreviewService $previewService ) { @@ -109,7 +110,6 @@ class ThumbnailService { // $this->logger->debug( // "[Batch] THUMBNAIL NAME : {image} / PATH : {path} / MIME : {mimetype} / DATA : {preview}", // array( -// 'app' => $this->appName, // 'image' => $preview['data']['image'], // 'path' => $preview['data']['path'], // 'mimetype' => $preview['data']['mimetype'], diff --git a/utility/smarterlogger.php b/utility/smarterlogger.php new file mode 100644 index 00000000..30d26911 --- /dev/null +++ b/utility/smarterlogger.php @@ -0,0 +1,261 @@ +<?php +/** + * ownCloud - galleryplus + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Olivier Paroz <owncloud@interfasys.ch> + * @author Bart Visscher <bartv@thisnet.nl> + * @author Jordi Boggiano <j.boggiano@seld.be> + * + * @copyright Olivier Paroz 2015 + * @copyright Bart Visscher 2013-2015 + * @copyright Jordi Boggiano 2014-2015 + */ + +namespace OCA\GalleryPlus\Utility; + +use OCP\ILogger; + +/** + * Lets us call the main logger without having to add the context at every + * request + * + * @package OCA\GalleryPlus\Middleware + */ +class SmarterLogger implements ILogger { + + /** + * @type ILogger + */ + private $logger; + + /*** + * Constructor + * + * @param string $appName + * @param ILogger $logger + */ + public function __construct( + $appName, + ILogger $logger + ) { + $this->appName = $appName; + $this->logger = $logger; + } + + /** + * System is unusable. + * + * @param string $message + * @param array $context + * + * @return null + */ + public function emergency($message, array $context = array()) { + $this->log(\OCP\Util::FATAL, $message, $context); + } + + /** + * Action must be taken immediately. + * + * @param string $message + * @param array $context + * + * @return null + */ + public function alert($message, array $context = array()) { + $this->log(\OCP\Util::ERROR, $message, $context); + } + + /** + * Critical conditions. + * + * @param string $message + * @param array $context + * + * @return null + */ + public function critical($message, array $context = array()) { + $this->log(\OCP\Util::ERROR, $message, $context); + } + + /** + * Runtime errors that do not require immediate action but should typically + * be logged and monitored. + * + * @param string $message + * @param array $context + * + * @return null + */ + public function error($message, array $context = array()) { + $this->log(\OCP\Util::ERROR, $message, $context); + } + + /** + * Exceptional occurrences that are not errors. + * + * @param string $message + * @param array $context + * + * @return null + */ + public function warning($message, array $context = array()) { + $this->log(\OCP\Util::WARN, $message, $context); + } + + /** + * Normal but significant events. + * + * @param string $message + * @param array $context + * + * @return null + */ + public function notice($message, array $context = array()) { + $this->log(\OCP\Util::INFO, $message, $context); + } + + /** + * Interesting events. + * + * @param string $message + * @param array $context + * + * @return null + */ + public function info($message, array $context = array()) { + $this->log(\OCP\Util::INFO, $message, $context); + } + + /** + * Detailed debug information. + * + * @param string $message + * @param array $context + * + * @return null + */ + public function debug($message, array $context = array()) { + $this->log(\OCP\Util::DEBUG, $message, $context); + } + + /** + * Normalises a message and logs it with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * + * @return mixed + */ + public function log($level, $message, array $context = array()) { + // interpolate $message as defined in PSR-3 + $replace = array(); + foreach ($context as $key => $val) { + // Allows us to dump arrays, objects and exceptions to the log + $val = $this->normalize($val); + $replace['{' . $key . '}'] = $val; + } + + // interpolate replacement values into the message and return + $message = strtr($message, $replace); + + $this->logger->log( + $level, $message, + array( + 'app' => $this->appName + ) + ); + + } + + /** + * Converts Objects, Arrays and Exceptions to String + * + * @param $data + * + * @return string + */ + private function normalize($data) { + if (null === $data || is_scalar($data)) { + return $data; + } + + if (is_array($data) || $data instanceof \Traversable) { + $normalized = array(); + $count = 1; + foreach ($data as $key => $value) { + if ($count >= 1000) { + $normalized['...'] = + 'Over 1000 items, aborting normalization'; + break; + } + $normalized[$key] = $this->normalize($value); + } + + //return $normalized; + return $this->toJson($normalized); + } + + if (is_object($data)) { + if ($data instanceof \Exception) { + return $this->normalizeException($data); + } + + $arrayObject = new \ArrayObject($data); + $serializedObject = $arrayObject->getArrayCopy(); + + return sprintf( + "[object] (%s: %s)", get_class($data), + $this->toJson($serializedObject) + ); + } + + if (is_resource($data)) { + return '[resource]'; + } + + return '[unknown(' . gettype($data) . ')]'; + } + + /** + * Normalises exceptions + * + * @param \Exception $exception + * + * @return string + */ + private function normalizeException(\Exception $exception) { + $data = array( + 'class' => get_class($exception), + 'message' => $exception->getMessage(), + 'file' => $exception->getFile() . ':' . $exception->getLine(), + ); + $trace = $exception->getTraceAsString(); + $data['trace'][] = $trace; + + $previous = $exception->getPrevious(); + if ($previous) { + $data['previous'] = $this->normalizeException($previous); + } + + return $this->toJson($data); + } + + /** + * JSON encodes data + * + * @param $data + * + * @return string + */ + private function toJson($data) { + // suppress json_encode errors since it's twitchy with some inputs + return @json_encode( + $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE + ); + } +}
\ No newline at end of file |