Skip to content

Instantly share code, notes, and snippets.

@miziomon
Created July 31, 2012 15:26
Show Gist options
  • Save miziomon/3217799 to your computer and use it in GitHub Desktop.
Save miziomon/3217799 to your computer and use it in GitHub Desktop.
WordPress MonoLog integration
{
"require": {
"monolog/monolog": "1.1.*"
}
}
<?php
/*
Plugin Name: mlog
Plugin URI:
Description: Monolog WordPress Plugin
Version: 1.0
Author: Mavida
Author URI:
License: GPLv2 or later
*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
require_once dirname(__FILE__) . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$Mlog = new Logger("WordPressLog");
add_action( 'init', 'Monolog_setup' );
/**
* Init function and definition
*
*
* @global Logger $Mlog
*/
function Monolog_setup() {
global $Mlog;
$default = array(
'FilePath' => dirname(__FILE__) . '/Mlog.log',
'QueryLog' => true,
);
$Mlog_Options = get_option( "Mlog_Options", $default );
// create a log channel
$Mlog->pushHandler(new StreamHandler( $Mlog_Options["FilePath"] ));
if ( $Mlog_Options["QueryLog"] ) {
add_filter("query", "Monolog_QueryLog" );
}
}
/**
*
* @param type $query
*/
function Monolog_QueryLog($query) {
global $Mlog;
$default = array(
'QueryLog_filter' => null,
);
$Mlog_Options = get_option( "Mlog_Options", $default );
//if ( preg_match( '/^\s*(insert|update|delete) /i', $query ) ) {
//if ( !preg_match( '/\b(wp_options|wp_terms|wp_usermeta|revision)\b/i', $query ) ) {
if ( is_null( $Mlog_Options["QueryLog_filter"]) ){
$Mlog->addInfo( $query );
} else {
if ( preg_match( '/^\s*(' . $Mlog_Options["QueryLog_filter"] . ') /i', $query ) ) {
$Mlog->addInfo( $query );
}
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment