Skip to content

Instantly share code, notes, and snippets.

@valpackett
Created August 18, 2012 14:16
Show Gist options
  • Save valpackett/3387016 to your computer and use it in GitHub Desktop.
Save valpackett/3387016 to your computer and use it in GitHub Desktop.
MODX + Monolog

MODX Revolution + Monolog

Replace MODX logging with Monolog to log to Graylog2, syslog, AMQP, etc.

Installation

  • put all the contents of this gist into the document root (where folders core, manager, etc. are located)
  • run composer install to get Monolog
  • run sh patchmodx.sh
  • edit modx-monolog.php - at line 22 there's the default handler - replace it with whatever you want to log to - see monolog readme for more info
{
"require": {
"php": ">=5.3.0",
"monolog/monolog": "1.1.0"
}
}
30c30,31
< $modx= new modX('', array(xPDO::OPT_CONN_INIT => array(xPDO::OPT_CONN_MUTABLE => true)));
---
> require_once dirname(dirname(__FILE__)).'/modx-monolog.php';
> $modx= new modXwithMonolog('', array(xPDO::OPT_CONN_INIT => array(xPDO::OPT_CONN_MUTABLE => true)));
54c54,55
< $modx= new modX();
---
> require_once dirname(__FILE__).'/modx-monolog.php';
> $modx= new modXwithMonolog();
56c56,57
< $modx= new modX('', array(xPDO::OPT_CONN_INIT => array(xPDO::OPT_CONN_MUTABLE => true)));
---
> require_once dirname(dirname(__FILE__)).'/modx-monolog.php';
> $modx= new modXwithMonolog('', array(xPDO::OPT_CONN_INIT => array(xPDO::OPT_CONN_MUTABLE => true)));
70c71
< if (isset($modx) && is_object($modx) && $modx instanceof modX) {
---
> if (isset($modx) && is_object($modx) && $modx instanceof modXwithMonolog) {
22,23c22,24
< $modx= new modX('', array(xPDO::OPT_CONN_INIT => array(xPDO::OPT_CONN_MUTABLE => true)));
< if (!is_object($modx) || !($modx instanceof modX)) {
---
> require_once dirname(dirname(dirname(__FILE__))).'/modx-monolog.php';
> $modx= new modXwithMonolog('', array(xPDO::OPT_CONN_INIT => array(xPDO::OPT_CONN_MUTABLE => true)));
> if (!is_object($modx) || !($modx instanceof modXwithMonolog)) {
<?php
require_once dirname(__FILE__).'/config.core.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
require_once dirname(__FILE__).'/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class modXwithMonolog extends modX {
public $monolog;
protected $levelconv = array(
modX::LOG_LEVEL_FATAL => Logger::CRITICAL,
modX::LOG_LEVEL_ERROR => Logger::ERROR,
modX::LOG_LEVEL_WARN => Logger::WARNING,
modX::LOG_LEVEL_INFO => Logger::INFO,
modX::LOG_LEVEL_DEBUG => Logger::DEBUG,
);
public function log($level, $msg, $target='', $def='', $file='', $line='') {
if (!$this->monolog) {
$this->monolog = new Logger('modx');
$this->monolog->pushHandler(new StreamHandler(MODX_CORE_PATH.'cache/logs/error.log', Logger::ERROR));
}
$this->monolog->addRecord($this->levelconv[$level], $msg, array('target' => $target, 'def' => $def, 'file' => $file, 'line' => $line));
}
}
?>
#!/bin/sh
patch index.php index.diff
patch manager/index.php manager-index.diff
patch manager/min/index.php manager-min-index.diff
patch connectors/index.php connectors-index.diff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment