├── .gitignore ├── CHANGELOG.md ├── package.json ├── composer.json ├── LICENSE ├── README.md └── src └── PHPConsoleWriter.php /.gitignore: -------------------------------------------------------------------------------- 1 | fabfile* 2 | vendor 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## Release 0.0.6 4 | Jul 6th 2015 5 | * **Fixed:** Modified README.md to reflect middleware implementation 6 | * **Changed:** PSR-4 autoloading instead of PSR-0 7 | * **Changed:** Moved main class to an `src` folder 8 | * **Added:** Extends \Slim\Middleware so you can `add` this logwriter as a middleware 9 | * **Added:** Changelog file -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "SlimPHPConsole", 3 | "version": "0.0.6", 4 | "description": "PHP-Console logging support for Slim Framework.", 5 | "keywords": [ 6 | "php-console", 7 | "slim", 8 | "log" 9 | ], 10 | "author": "Felipe Figueroa ", 11 | "license": "MIT", 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/amenadiel/SlimPHPConsole" 15 | }, 16 | "engines": { 17 | "php": ">=5.3.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "amenadiel/slim-phpconsole", 3 | "type": "library", 4 | "version": "0.1.1", 5 | "description": "PHP-Console logging support for Slim Framework", 6 | "keywords": ["logging", "extensions", "middleware"], 7 | "homepage": "http://github.com/amenadiel/SlimPHPConsole", 8 | "license": "MIT", 9 | "authors": [{ 10 | "name": "Felipe Figueroa", 11 | "email": "amenadiel@gmail.com", 12 | "homepage": "http://github.com/amenadiel/" 13 | }], 14 | "require": { 15 | "php-console/php-console": "~3.1", 16 | "slim/slim": "^2.3", 17 | "php": ">=5.3.0" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Amenadiel\\SlimPHPConsole\\": "src/" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Felipe Figueroa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SlimPHPConsole 2 | 3 | PHP-Console log writer for Slim Framework 4 | 5 | [![Packagist](https://img.shields.io/packagist/dm/amenadiel/slim-phpconsole.svg)](https://packagist.org/packages/amenadiel/slim-phpconsole) 6 | 7 | Use this custom log writer to output [Slim Framework](http://www.slimframework.com/)'s log messages 8 | to your browser's console using [PHP-Console](https://github.com/barbushin/php-console). 9 | 10 | ### Installation 11 | 12 | Just add `amenadiel/slim-phpconsole` to your `composer.json` file in the require or require-dev sections: 13 | 14 | { 15 | "require": { 16 | "amenadiel/slim-phpconsole":"~0.0.4" 17 | } 18 | } 19 | 20 | ### Usage 21 | 22 | Instantiate the log writer. If you don't want to have the handler autostarted, pass `false` as a parameter, `true` is implied otherwise. 23 | 24 | When the handler is started it will set itself as error and exception handler too, unless you set it otherwise. 25 | 26 | ```php 27 | $logwriter = new \Amenadiel\SlimPHPConsole\PHPConsoleWriter(true); 28 | 29 | $app = new \Slim\Slim(array( 30 | 'log.enabled' => true, 31 | 'log.level' => \Slim\Log::DEBUG, 32 | 'log.writer' => $logwriter 33 | )); 34 | ``` 35 | 36 | Starting from version `0.0.6` this adapter extends Slim\Middleware. Therefore, you can also use the `add` method of your app 37 | 38 | ```php 39 | $app = new \Slim\Slim(array( 40 | 'log.enabled' => true, 41 | 'log.level' => \Slim\Log::DEBUG 42 | )); 43 | 44 | $app->add(new \Amenadiel\SlimPHPConsole\PHPConsoleWriter); 45 | ``` 46 | 47 | Both ways of setting PHP-Console as your logger are pretty much the same. Afterwards, you can send messages to your browser's console using `$app->log`'s methods. 48 | 49 | 50 | ```php 51 | $app->log->debug('Debug called!'); 52 | $app->log->info('This is just info'); 53 | $app->log->warning('Heads Up! This is a warning'); 54 | ``` 55 | 56 | You can pass custom tags to PHPConsole by using this adapter's `debug` method which forwards its parameters to PHPConsole's `debug` method. 57 | 58 | ```php 59 | $app->log->getWriter()->debug('This has a custom tag', 'custom.tag'); 60 | ``` 61 | 62 | If you are using PHPConsole directly somewhere else in your app, remember not to start it twice, for it will throw an exception. Use its `isStarted` method to check if it's already started. 63 | 64 | ```php 65 | $myHandler = \PhpConsole\Handler::getInstance(); 66 | 67 | if (!$myHandler->isStarted()) { 68 | $myHandler->start(); // Only start it if it hasn't been started yet 69 | } 70 | ``` 71 | 72 | ### Optional Settings 73 | 74 | You can use PHP-Console's configuration methods by getting a reference to the Handler instance or the Connector instance. For example: 75 | 76 | ```php 77 | $logwriter = new \Amenadiel\SlimPHPConsole\PHPConsoleWriter(false); 78 | $handler = $logwriter->getHandler(); 79 | $handler->setHandleErrors(false); // disable errors handling, must be done before 'start' method 80 | $handler->start(); 81 | 82 | $connector = $logwriter->getConnector(); 83 | $connector->setPassword('macoy123'); //sets a very insecure passwd 84 | ``` 85 | 86 | 87 | -------------------------------------------------------------------------------- /src/PHPConsoleWriter.php: -------------------------------------------------------------------------------- 1 | true, 19 | * 'log.level' => \Slim\Log::DEBUG, 20 | * 'log.writer' => $logwriter 21 | * )); 22 | * 23 | * $app->log->debug('Debug called!'); 24 | * $app->log->info('This is just info'); 25 | * $app->log->warning('Heads Up! This is a warning'); 26 | * 27 | * You can pass custom tags to PHPConsole by using this adapter's debug method 28 | * 29 | * $app->log->getWriter()->debug('This has a custom tag', 'custom.tag'); 30 | * 31 | * If you are using PHPConsole directly somewhere else in your app, remember not 32 | * to start it twice, for it will throw an exception: 33 | * 34 | * $myHandler = \PhpConsole\Handler::getInstance(); 35 | * 36 | * if (!$myHandler->isStarted()) { 37 | * $myHandler->start(); 38 | * } 39 | * 40 | * 41 | * 42 | * SETTINGS 43 | * 44 | * You can use PHPConsole's configuration methods by getting a reference 45 | * to the Handler instance or the Connector instance. For example: 46 | * 47 | * $handler = $logwriter->getHandler(); 48 | * $handler->setHandleErrors(false); // disable errors handling 49 | * 50 | * $connector = $logwriter->getConnector(); 51 | * $connector->setPassword('macoy123'); //sets a very insecure passwd 52 | * 53 | * 54 | * 55 | * @author Felipe Figueroa 56 | * @copyright 2015 Amenadiel 57 | * 58 | * MIT LICENSE 59 | * 60 | * Permission is hereby granted, free of charge, to any person obtaining 61 | * a copy of this software and associated documentation files (the 62 | * "Software"), to deal in the Software without restriction, including 63 | * without limitation the rights to use, copy, modify, merge, publish, 64 | * distribute, sublicense, and/or sell copies of the Software, and to 65 | * permit persons to whom the Software is furnished to do so, subject to 66 | * the following conditions: 67 | * 68 | * The above copyright notice and this permission notice shall be 69 | * included in all copies or substantial portions of the Software. 70 | * 71 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 72 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 73 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 74 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 75 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 76 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 77 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 78 | */ 79 | 80 | namespace Amenadiel\SlimPHPConsole; 81 | 82 | class PHPConsoleWriter extends \Slim\Middleware 83 | { 84 | /** 85 | * @var handler 86 | */ 87 | protected $handler; 88 | 89 | /** 90 | * @var connector 91 | */ 92 | protected $connector; 93 | 94 | /** 95 | * Converts Slim log level to its matching PHP-Console tag 96 | * @var array 97 | */ 98 | protected $log_level = [ 99 | \Slim\Log::EMERGENCY => 'EMERGENCY', 100 | \Slim\Log::ALERT => 'ALERT', 101 | \Slim\Log::CRITICAL => 'CRITICAL', 102 | \Slim\Log::ERROR => 'ERROR', 103 | \Slim\Log::WARN => 'WARN', 104 | \Slim\Log::NOTICE => 'NOTICE', 105 | \Slim\Log::INFO => 'INFO', 106 | \Slim\Log::DEBUG => 'DEBUG', 107 | ]; 108 | 109 | /** 110 | * Constructs the writer, optionally starts the handler 111 | * @param boolean $autostart set to true to autostart the handler 112 | * @return void 113 | */ 114 | public function __construct($autostart = true) 115 | { 116 | 117 | $this->handler = \PhpConsole\Handler::getInstance(); 118 | $this->connector = $this->handler->getConnector(); 119 | if ($autostart && !$this->handler->isStarted()) { 120 | $this->handler->start(); 121 | } 122 | 123 | } 124 | 125 | public function call() 126 | { 127 | $app = $this->app; 128 | 129 | $app->config('log.writer', $this); 130 | 131 | $this->next->call(); 132 | } 133 | 134 | public function getHandler() 135 | { 136 | return $this->handler; 137 | } 138 | 139 | public function getConnector() 140 | { 141 | return $this->connector; 142 | } 143 | 144 | public function debug($data, $tags = null, $ignoreTraceCalls = 0) 145 | { 146 | $this->handler->debug($data, $tags, $ignoreTraceCalls); 147 | } 148 | 149 | /** 150 | * Write to log 151 | * 152 | * @param mixed $object 153 | * @param int $level 154 | * @return void 155 | */ 156 | public function write($object, $level = \Slim\Log::DEBUG) 157 | { 158 | $this->handler->debug($object, $this->get_log_level($level)); 159 | } 160 | 161 | /** 162 | * Converts Slim log level to matching PHPConsole's tag 163 | * 164 | * @param int $slim_log_level Slim log level we're converting from 165 | * @return string matching debug level 166 | */ 167 | protected function get_log_level($slim_log_level) 168 | { 169 | 170 | return isset($this->log_level[$slim_log_level]) ? 171 | $this->log_level[$slim_log_level] : 172 | $slim_log_level; 173 | } 174 | } 175 | --------------------------------------------------------------------------------