Created
April 17, 2016 19:13
-
-
Save Nyholm/905be92b8578566da1618af05029562f to your computer and use it in GitHub Desktop.
Example of logging plugin to Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('aal_insert_log', 'storeActivityToFile'); | |
function storeActivityToFile($args) { | |
// Example | |
$args = array( | |
'action' => 'activated', | |
'object_type' => 'Plugin', | |
'object_subtype' => '', | |
'object_name' => 'Activity Log', | |
'object_id' => 0, | |
'user_id' => 1, | |
'user_caps' => 'administrator', | |
'hist_ip' => '127.0.0.1', | |
'hist_time' => 1460476892, | |
); | |
addLogEntry('info', 'activity_log', $args); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Logging | |
Plugin URI: http://wordpress.org/tnyholm | |
Description: Logging stuff with Monolog | |
Author: Tobias Nyholm | |
Version: 0.1 | |
Author URI: http://tnyholm.se | |
*/ | |
use Monolog\Logger; | |
use Monolog\Handler\StreamHandler; | |
use Monolog\Handler\SlackHandler; | |
// make sure to load composer autoload | |
require_once __DIR__."/../../../vendor/autoload.php"; | |
class MyLogger | |
{ | |
/** | |
* @var Logger | |
*/ | |
private $monolog; | |
public function __construct() | |
{ | |
// Create the logger | |
$this->monolog = new Logger('my_logger'); | |
// Now add some handlers | |
$this->monolog->pushHandler(new StreamHandler(__DIR__.'/my_app.log', Logger::DEBUG)); | |
$this->monolog->pushHandler(new SlackHandler('t0ken', 'happyr')); | |
} | |
public function log($level, $message, array $context = []) | |
{ | |
$this->monolog->log($level, $message, $context); | |
} | |
} | |
function addLogEntry($level, $message, array $context = []) | |
{ | |
global $logger; | |
if (!$logger) { | |
$logger = new MyLogger(); | |
} | |
$logger->log($level, $message, $context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sweet, då håller jag utkik där istället 😄