Skip to content
This repository was archived by the owner on Dec 16, 2018. It is now read-only.

Adding support for CI 3 and SyslogUDPHandler for external log systems like papertrail #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
Codeigniter-Monolog
Codeigniter-Monolog - with PaperTrail support
===================

Simple integration of the Monolog Package (https://github.com/Seldaek/monolog) into CodeIgniter by overwriting the CI_Log class.

Based on https://github.com/pfote/Codeigniter-Monolog, but updating monolog to 1.7.* and supporting file logging more akin to native CodeIgniter logging.
Based on https://github.com/pfote/Codeigniter-Monolog, but updating monolog to 1.22.* and supporting file logging more akin to native CodeIgniter logging.

This library registers Monolog as the PHP error handler to catch all errors and adds support for IntrospectionProcessor for additional meta data.

Supports File (RotatingFileHandler), New Relic (NewRelicHandler) and HipChat (HipChatHandler).
Supports File (RotatingFileHandler), New Relic (NewRelicHandler), HipChat (HipChatHandler) and Papertrail via SyslogUdpHandler.

Compatible with Codeigniter 3.1.6 and up

Installation
------------
* Install monolog with ```composer require monolog/monolog```
* Make sure your index.php contains ```include_once './vendor/autoload.php';```
* Copy application/libraries/Log.php and application/config/monolog.php into your CodeIgniter application
* Copy application/core/MY_Log.php and application/config/monolog.php into your CodeIgniter application

Usage
-----
Expand Down
18 changes: 13 additions & 5 deletions application/config/monolog.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}

use Monolog\Logger;
/*
* CodeIgniter Monolog integration
* CodeIgniter Monolog integration - Plus Papertrail support
*
* (c) Steve Thomas <steve@thomasmultimedia.com.au>
* (adjustments) Carlos Umanzor <carlos@nidux.com>
* (original idea and concept) Steve Thomas <steve@thomasmultimedia.com.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/


/* GENERAL OPTIONS */
$config['handlers'] = array('file', 'new_relic', 'hipchat'); // valid handlers are file | new_relic | hipchat
$config['channel'] = ENVIRONMENT; // channel name which appears on each line of log
$config['threshold'] = '4'; // 'ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4'
//$config['handlers'] = array('file', 'new_relic', 'hipchat', 'papertrail'); // valid handlers are file | new_relic | hipchat | papertrail
$config['handlers'] = array('papertrail'); // Send logs to papertrail only
$config['channel'] = 'My log'; // channel name which appears on each line of log, use proper naming for control
$config['threshold'] = '2'; // 'ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4'
$config['introspection_processor'] = TRUE; // add some meta data such as controller and line number to log messages

/* PAPERTRAIL OPTIONS - Make sure you add the right hostname and port*/
$config['papertrail_hostname'] = 'logsX.papertrailapp.com';
$config['papertrail_port'] = '#####';

/* FILE HANDLER OPTIONS
* Log to default CI log directory (must be writable ie. chmod 757).
* Filename will be encoded to current system date, ie. YYYY-MM-DD-ci.log
Expand Down
19 changes: 11 additions & 8 deletions application/libraries/Log.php → application/core/MY_Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@
}

/*
* CodeIgniter Monolog integration
* CodeIgniter Monolog integration - Plus Papertrail support
*
* Version 1.1.1
* (c) Steve Thomas <steve@thomasmultimedia.com.au>
* (adjustments) Carlos Umanzor <carlos@nidux.com>
* (original idea and concept) Steve Thomas <steve@thomasmultimedia.com.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once APPPATH.'vendor/autoload.php';

use Monolog\Logger;
use Monolog\ErrorHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\NewRelicHandler;
use Monolog\Handler\HipChatHandler;
use Monolog\Processor\IntrospectionProcessor;
use Monolog\Handler\SyslogUdpHandler;

/**
* replaces CI's Logger class, use Monolog instead
*
* see https://github.com/stevethomas/codeigniter-monolog & https://github.com/Seldaek/monolog
*
*/
class CI_Log
class MY_Log
{
// CI log levels
protected $_levels = array(
Expand Down Expand Up @@ -72,14 +75,15 @@ public function __construct()
// decide which handler(s) to use
foreach ($this->config['handlers'] as $value) {
switch ($value) {
case 'papertrail':
$handler = new SyslogUdpHandler($this->config['papertrail_hostname'], $this->config['papertrail_port']);
break;
case 'file':
$handler = new RotatingFileHandler($this->config['file_logfile']);
break;

case 'new_relic':
$handler = new NewRelicHandler(Logger::ERROR, true, $this->config['new_relic_app_name']);
break;

case 'hipchat':
$handler = new HipChatHandler(
$config['hipchat_app_token'],
Expand All @@ -88,7 +92,6 @@ public function __construct()
$config['hipchat_app_notify'],
$config['hipchat_app_loglevel']);
break;

default:
exit('log handler not supported: ' . $this->config['handler']);
}
Expand Down Expand Up @@ -147,4 +150,4 @@ public function write_log(
}
return true;
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"monolog/monolog": "~1.17"
"monolog/monolog": "1.22.0"
}
}