From edba95a3c0b6cefab7fde5f9c7a16136fd544289 Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Thu, 5 Nov 2015 09:02:15 +0700 Subject: [PATCH] Initial commit Signed-off-by: Fery Wardiyanto --- .coveralls.yml | 5 ++ .editorconfig | 16 ++++ .gitattributes | 12 +++ .gitignore | 6 ++ .travis.yml | 18 +++++ CONTRIBUTING.md | 19 +++++ LICENSE.md | 21 +++++ README.md | 78 ++++++++++++++++++ composer.json | 39 +++++++++ phpunit.xml.dist | 27 +++++++ src/Monolog.php | 175 ++++++++++++++++++++++++++++++++++++++++ src/MonologProvider.php | 23 ++++++ tests/MonologTest.php | 26 ++++++ 13 files changed, 465 insertions(+) create mode 100644 .coveralls.yml create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/Monolog.php create mode 100644 src/MonologProvider.php create mode 100644 tests/MonologTest.php diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..5d6eb27 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,5 @@ + +src_dir: . +service_name: travis-ci +coverage_clover: clover.xml +json_path: coveralls-upload.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..416de97 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +# editorconfig.org +root = true + +[*] +end_of_line = lf +indent_style = space +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = false +indent_style = space + +[{src,tests}/**.php] +indent_size = 4 + +[*.{json,yml}] +indent_size = 4 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f4e1467 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,12 @@ +# Enforce Unix newlines +* text=lf + +# Exclude unused files +# see: https://redd.it/2jzp6k +/tests export-ignore +/.coveralls.yml export-ignore +/.editorconfig export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..37c8113 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.directory +composer.lock +vendor +clover.xml +coveralls-upload.json +*.sublime-* \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8ca4614 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +language: php + +php: + - 5.5 + - 5.6 + - 7.0 + - hhvm + +install: + - if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then composer require satooshi/php-coveralls:dev-master -n ; fi + - composer install -n + +script: + - if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then phpunit --coverage-clover clover.xml ; fi + - phpunit --coverage-clover clover.xml + +after_script: + - if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then php vendor/bin/coveralls -v ; fi \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e06fcbf --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,19 @@ +# How to Contribute + +## Pull Requests + +1. Fork this repository +2. Create a new branch for each feature or improvement +3. Send a pull request from each feature branch + +It is very important to separate new features or improvements into separate feature branches, and to send a pull request for each branch. This allows me to review and pull in new features or improvements individually. + +## Style Guide + +All pull requests must adhere to the [PSR-2 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md). + +## Unit Testing + +All pull requests must be accompanied by passing unit tests and complete code coverage. + +[Learn about PHPUnit](https://github.com/sebastianbergmann/phpunit/) \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..895c64f --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Fery Wardiyanto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6597970 --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +# [Unofficial] Slim Framework 3.x Monolog Logger + +[![Build Status](https://img.shields.io/travis/projek-xyz/slim-monolog/master.svg?style=flat-square)](https://travis-ci.org/projek-xyz/slim-monolog) [![LICENSE](https://img.shields.io/packagist/l/projek-xyz/slim-monolog.svg?style=flat-square)](https://packagist.org/packages/projek-xyz/slim-monolog) [![VERSION](https://img.shields.io/packagist/v/projek-xyz/slim-monolog.svg?style=flat-square)](https://packagist.org/packages/projek-xyz/slim-monolog) [![Coveralls branch](https://img.shields.io/coveralls/projek-xyz/slim-monolog/master.svg?style=flat-square)](https://coveralls.io/github/projek-xyz/slim-monolog) + +This is a Slim Framework 3.x component helper built on top of the Monolog Logger. + +## Install + +Via [Composer](https://getcomposer.org/) + +```bash +$ composer require projek-xyz/slim-monolog --prefer-dist +``` + +Requires Slim Framework 3 and PHP 5.5.0 or newer. + +## Usage + +```php +// Create Slim app +$app = new \Slim\App(); + +// Fetch DI Container +$container = $app->getContainer(); + +// Register Monolog helper: +// Option 1, using MonologProvider +$container->register(new \Projek\Slim\MonologProvider); + +// Option 2, using Closure +$container['view'] = function ($c) { + $view = new \Projek\Slim\Monolog([ + 'directory' => 'path/to/logs', + ]); + + return $view; +}; + +// Define named route +$app->get('/hello/{name}', function ($req, $res, $args) { + return $this->view->render($res, 'profile', [ + 'name' => $args['name'] + ]); +})->setName('profile'); + +// Run app +$app->run(); +``` + +**NOTE**: if you are using _option 1_ please make sure you already have `$container['settings']['monolog']` in your configuration file. + +## Custom template functions + +... + +```php +// +``` + +## Testing + +```bash +phpunit +``` + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +## Credits + +- [Fery Wardiyanto](http://feryardiant.me) +- [Slim Framework](http://www.slimframework.com) +- [Monolog](https://github.com/Seldaek/monolog) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..eaeffe5 --- /dev/null +++ b/composer.json @@ -0,0 +1,39 @@ +{ + "name": "projek-xyz/slim-monolog", + "description": "Slim Framework 3 view helper built on top of the Plates templating component", + "keywords": ["slim","framework","view","template","monolog"], + "homepage": "http://www.projek.xyz/slim-monolog", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Fery Wardiyanto", + "email": "hallo@feryardiant.me", + "homepage": "http://feryardiant.me" + } + ], + "support": { + "issues": "https://github.com/projek-xyz/slim-monolog/issues", + "source": "https://github.com/projek-xyz/slim-monolog" + }, + "autoload": { + "psr-4": { + "Projek\\Slim\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Projek\\Slim\\Tests\\": "tests/" + } + }, + "minimum-stability": "dev", + "prefer-stable": true, + "require": { + "php": ">=5.5.0", + "slim/slim": "3.x-dev", + "monolog/monolog": "^1.17" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..97ee052 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,27 @@ + + + + + + + ./tests/ + + + + + + ./src + + + + \ No newline at end of file diff --git a/src/Monolog.php b/src/Monolog.php new file mode 100644 index 0000000..c977dd7 --- /dev/null +++ b/src/Monolog.php @@ -0,0 +1,175 @@ + null, + 'timezone' => null, + 'handlers' => [], + ]; + + /** + * @var \Monolog\Logger + */ + private $monolog; + + /** + * Register this plates view provider with a Pimple container + * + * @param string $name + * @param array $settings + */ + public function __construct($name = 'slim-app', $settings = []) + { + $this->name = $name; + $this->settings += $settings; + $this->monolog = new Logger($this->name); + + $this->monolog->setHandlers($this->settings['handlers']); + + if (null !== $this->settings['timezone']) { + if (is_string($this->settings['timezone'])) { + $this->settings['timezone'] = new \DateTimeZone($this->settings['timezone']); + } + + Logger::setTimezone($this->settings['timezone']); + } + } + + /** + * Returns Monolog Instance + * + * @return \Monolog\Logger + */ + public function getMonolog() + { + return $this->monolog; + } + + /** + * Pushes a handler on to the stack. + * + * @param HandlerInterface $handler + * @return \Monolog\Logger + */ + public function pushHandler(HandlerInterface $handler) + { + return $this->monolog->pushHandler($handler); + } + + /** + * Pops a handler from the stack + * + * @return Monolog\Handler\HandlerInterface + */ + public function popHandler() + { + return $this->monolog->popHandler(); + } + + /** + * Adds a processor on to the stack. + * + * @param callable $callback + * @return \Monolog\Logger + */ + public function pushProcessor($callback) + { + return $this->monolog->pushProcessor($callback); + } + + /** + * Removes the processor on top of the stack and returns it. + * + * @return callable + */ + public function popProcessor() + { + return $this->monolog->popProcessor(); + } + + /** + * Adds a log record. + * + * @param integer $level The logging level + * @param string $message The log message + * @param array $context The log context + * @return Boolean Whether the record has been processed + */ + public function log($level, $message, array $context = []) + { + return $this->monolog->log($level, $message, $context); + } + + /** + * Register a Syslog handler. + * + * @param string $name + * @param string $level + * @return void + */ + public function useSyslog($name = 'slim-app', $level = 'debug') + { + $name or $name = $this->name; + $this->monolog->pushHandler(new SyslogHandler($name, LOG_USER, $level)); + return $this; + } + + /** + * Register an error_log handler. + * + * @param string $level + * @param int $messageType + * @return void + */ + public function useErrorLog($level = 'debug', $messageType = ErrorLogHandler::OPERATING_SYSTEM) + { + $handler = new ErrorLogHandler($messageType, Logger::toMonologLevel($level)); + $this->monolog->pushHandler($handler); + $handler->setFormatter($this->getDefaultFormatter()); + return $this; + } + + /** + * Register a file log handler. + * + * @param string $path + * @param string $level + * @return void + */ + public function useFiles($path = '', $level = 'debug') + { + $path or $path = $this->settings['directory']; + $handler = new StreamHandler($path, Logger::toMonologLevel($level)); + $this->monolog->pushHandler($handler); + $handler->setFormatter($this->getDefaultFormatter()); + + return $this; + } + + /** + * Get a defaut Monolog formatter instance. + * + * @return \Monolog\Formatter\LineFormatter + */ + protected function getDefaultFormatter() + { + return new LineFormatter(null, null, true, true); + } +} \ No newline at end of file diff --git a/src/MonologProvider.php b/src/MonologProvider.php new file mode 100644 index 0000000..e1af22d --- /dev/null +++ b/src/MonologProvider.php @@ -0,0 +1,23 @@ +has('monolog')) { + throw new InvalidArgumentException('Logger configuration not found'); + } + + $container['view'] = new Monolog('slim-app', $container['settings']->get('monolog')); + } +} \ No newline at end of file diff --git a/tests/MonologTest.php b/tests/MonologTest.php new file mode 100644 index 0000000..f0db927 --- /dev/null +++ b/tests/MonologTest.php @@ -0,0 +1,26 @@ +logger = new Monolog('slim-app-test', [ + 'timezone' => new \DateTimeZone('UTC'), + 'handlers' => [], + ]); + } + + public function testShoudReturnsPlatesEngine() + { + $this->assertInstanceOf('Monolog\Logger', $this->logger->getMonolog()); + } +}