├── .gitignore ├── LICENSE ├── README.md ├── application ├── config │ └── monolog.php └── libraries │ └── Log.php └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Andreas Pfotenhauer 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Codeigniter-Monolog 2 | =================== 3 | 4 | Simple integration of the Monolog Package (https://github.com/Seldaek/monolog) into CodeIgniter by overwriting the CI_Log class. 5 | 6 | Based on https://github.com/pfote/Codeigniter-Monolog, but updating monolog to 1.7.* and supporting file logging more akin to native CodeIgniter logging. 7 | 8 | This library registers Monolog as the PHP error handler to catch all errors and adds support for IntrospectionProcessor for additional meta data. 9 | 10 | Supports File (RotatingFileHandler), New Relic (NewRelicHandler) and HipChat (HipChatHandler). 11 | 12 | Installation CodeIgniter 2 13 | -------------------------- 14 | * Install monolog with ```composer require monolog/monolog``` 15 | * Make sure your index.php contains ```include_once './vendor/autoload.php';``` 16 | * Copy application/libraries/Log.php and application/config/monolog.php into your CodeIgniter application 17 | 18 | Installation CodeIgniter 3 19 | -------------------------- 20 | * Install monolog with ```composer require monolog/monolog``` 21 | * Make sure your index.php contains ```include_once './vendor/autoload.php';``` 22 | * Copy application/core/Log.php and application/config/monolog.php into your CodeIgniter application 23 | 24 | Usage 25 | ----- 26 | Use log_message() as per normal in CodeIgniter to log error, debug and info messages. Log files are stored in the application/logs folder in format YYYY-MM-DD-ci.log 27 | 28 | License 29 | ------- 30 | 31 | codeigniter-monolog is licensed under the MIT License - see the LICENSE file for details 32 | -------------------------------------------------------------------------------- /application/config/monolog.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * For the full copyright and license information, please view the LICENSE 10 | * file that was distributed with this source code. 11 | */ 12 | 13 | 14 | /* GENERAL OPTIONS */ 15 | $config['handlers'] = array('file', 'new_relic', 'hipchat'); // valid handlers are file | new_relic | hipchat 16 | $config['channel'] = ENVIRONMENT; // channel name which appears on each line of log 17 | $config['threshold'] = '4'; // 'ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4' 18 | $config['introspection_processor'] = TRUE; // add some meta data such as controller and line number to log messages 19 | 20 | /* FILE HANDLER OPTIONS 21 | * Log to default CI log directory (must be writable ie. chmod 757). 22 | * Filename will be encoded to current system date, ie. YYYY-MM-DD-ci.log 23 | */ 24 | $config['file_logfile'] = APPPATH . 'logs/ci.log'; 25 | 26 | /* NEW RELIC OPTIONS */ 27 | $config['new_relic_app_name'] = 'App Name - ' . ENVIRONMENT; 28 | 29 | /* HIPCHAT OPTIONS */ 30 | $config['hipchat_app_token'] = ''; //HipChat API Token 31 | $config['hipchat_app_room_id'] = ''; //The room that should be alerted of the message (Id or Name) 32 | $config['hipchat_app_notification_name'] = 'Monolog'; //Name used in the "from" field 33 | $config['hipchat_app_notify'] = false; //Trigger a notification in clients or not 34 | $config['hipchat_app_loglevel'] = Logger::WARNING; //The minimum logging level at which this handler will be triggered 35 | 36 | 37 | // exclusion list for pesky messages which you may wish to temporarily suppress with strpos() match 38 | $config['exclusion_list'] = array(); 39 | -------------------------------------------------------------------------------- /application/libraries/Log.php: -------------------------------------------------------------------------------- 1 | 10 | * 11 | * For the full copyright and license information, please view the LICENSE 12 | * file that was distributed with this source code. 13 | */ 14 | 15 | use Monolog\Logger; 16 | use Monolog\ErrorHandler; 17 | use Monolog\Handler\RotatingFileHandler; 18 | use Monolog\Handler\NewRelicHandler; 19 | use Monolog\Handler\HipChatHandler; 20 | use Monolog\Processor\IntrospectionProcessor; 21 | 22 | /** 23 | * replaces CI's Logger class, use Monolog instead 24 | * 25 | * see https://github.com/stevethomas/codeigniter-monolog & https://github.com/Seldaek/monolog 26 | * 27 | */ 28 | class CI_Log 29 | { 30 | // CI log levels 31 | protected $_levels = array( 32 | 'OFF' => '0', 33 | 'ERROR' => '1', 34 | 'DEBUG' => '2', 35 | 'INFO' => '3', 36 | 'ALL' => '4' 37 | ); 38 | 39 | // config placeholder 40 | protected $config = array(); 41 | 42 | 43 | /** 44 | * prepare logging environment with configuration variables 45 | */ 46 | public function __construct() 47 | { 48 | // copied functionality from system/core/Common.php, as the whole CI infrastructure is not available yet 49 | if (!defined('ENVIRONMENT') OR !file_exists($file_path = APPPATH . 'config/' . ENVIRONMENT . '/monolog.php')) { 50 | $file_path = APPPATH . 'config/monolog.php'; 51 | } 52 | 53 | // Fetch the config file 54 | if (file_exists($file_path)) { 55 | require($file_path); 56 | } else { 57 | exit('monolog.php config does not exist'); 58 | } 59 | 60 | // make $config from config/monolog.php accessible to $this->write_log() 61 | $this->config = $config; 62 | 63 | $this->log = new Logger($this->config['channel']); 64 | // detect and register all PHP errors in this log hence forth 65 | ErrorHandler::register($this->log); 66 | 67 | if ($this->config['introspection_processor']) { 68 | // add controller and line number info to each log message 69 | $this->log->pushProcessor(new IntrospectionProcessor()); 70 | } 71 | 72 | // decide which handler(s) to use 73 | foreach ($this->config['handlers'] as $value) { 74 | switch ($value) { 75 | case 'file': 76 | $handler = new RotatingFileHandler($this->config['file_logfile']); 77 | break; 78 | 79 | case 'new_relic': 80 | $handler = new NewRelicHandler(Logger::ERROR, true, $this->config['new_relic_app_name']); 81 | break; 82 | 83 | case 'hipchat': 84 | $handler = new HipChatHandler( 85 | $config['hipchat_app_token'], 86 | $config['hipchat_app_room_id'], 87 | $config['hipchat_app_notification_name'], 88 | $config['hipchat_app_notify'], 89 | $config['hipchat_app_loglevel']); 90 | break; 91 | 92 | default: 93 | exit('log handler not supported: ' . $this->config['handler']); 94 | } 95 | 96 | $this->log->pushHandler($handler); 97 | } 98 | 99 | $this->write_log('DEBUG', 'Monolog replacement logger initialized'); 100 | } 101 | 102 | 103 | /** 104 | * Write to defined logger. Is called from CodeIgniters native log_message() 105 | * 106 | * @param string $level 107 | * @param $msg 108 | * @return bool 109 | */ 110 | public function write_log( 111 | $level = 'error', 112 | $msg 113 | ) { 114 | $level = strtoupper($level); 115 | 116 | // verify error level 117 | if (!isset($this->_levels[$level])) { 118 | $this->log->addError('unknown error level: ' . $level); 119 | $level = 'ALL'; 120 | } 121 | 122 | // filter out anything in $this->config['exclusion_list'] 123 | if (!empty($this->config['exclusion_list'])) { 124 | foreach ($this->config['exclusion_list'] as $findme) { 125 | $pos = strpos($msg, $findme); 126 | 127 | if ($pos !== false) { 128 | // just exit now - we don't want to log this error 129 | return true; 130 | } 131 | } 132 | } 133 | 134 | if ($this->_levels[$level] <= $this->config['threshold']) { 135 | switch ($level) { 136 | case 'ERROR': 137 | $this->log->addError($msg); 138 | break; 139 | case 'DEBUG': 140 | $this->log->addDebug($msg); 141 | break; 142 | case 'ALL': 143 | case 'INFO': 144 | $this->log->addInfo($msg); 145 | break; 146 | } 147 | } 148 | return true; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "monolog/monolog": "~1.17" 4 | } 5 | } 6 | --------------------------------------------------------------------------------