Add rudimentary logger
[MAILPOET-570]
This commit is contained in:
@ -85,6 +85,7 @@ class Database {
|
|||||||
$statistics_unsubscribes = Env::$db_prefix . 'statistics_unsubscribes';
|
$statistics_unsubscribes = Env::$db_prefix . 'statistics_unsubscribes';
|
||||||
$statistics_forms = Env::$db_prefix . 'statistics_forms';
|
$statistics_forms = Env::$db_prefix . 'statistics_forms';
|
||||||
$mapping_to_external_entities = Env::$db_prefix . 'mapping_to_external_entities';
|
$mapping_to_external_entities = Env::$db_prefix . 'mapping_to_external_entities';
|
||||||
|
$log = Env::$db_prefix . 'log';
|
||||||
|
|
||||||
define('MP_SETTINGS_TABLE', $settings);
|
define('MP_SETTINGS_TABLE', $settings);
|
||||||
define('MP_SEGMENTS_TABLE', $segments);
|
define('MP_SEGMENTS_TABLE', $segments);
|
||||||
@ -110,6 +111,7 @@ class Database {
|
|||||||
define('MP_STATISTICS_UNSUBSCRIBES_TABLE', $statistics_unsubscribes);
|
define('MP_STATISTICS_UNSUBSCRIBES_TABLE', $statistics_unsubscribes);
|
||||||
define('MP_STATISTICS_FORMS_TABLE', $statistics_forms);
|
define('MP_STATISTICS_FORMS_TABLE', $statistics_forms);
|
||||||
define('MP_MAPPING_TO_EXTERNAL_ENTITIES_TABLE', $mapping_to_external_entities);
|
define('MP_MAPPING_TO_EXTERNAL_ENTITIES_TABLE', $mapping_to_external_entities);
|
||||||
|
define('MP_LOG_TABLE', $log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -435,10 +435,12 @@ class Migrator {
|
|||||||
|
|
||||||
function log() {
|
function log() {
|
||||||
$attributes = [
|
$attributes = [
|
||||||
|
'id BIGINT unsigned NOT NULL AUTO_INCREMENT,',
|
||||||
'name VARCHAR(255),',
|
'name VARCHAR(255),',
|
||||||
'level INTEGER,',
|
'level INTEGER,',
|
||||||
'message LONGTEXT,',
|
'message LONGTEXT,',
|
||||||
'time TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
|
'created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
|
||||||
|
'PRIMARY KEY (id),',
|
||||||
];
|
];
|
||||||
return $this->sqlify(__FUNCTION__, $attributes);
|
return $this->sqlify(__FUNCTION__, $attributes);
|
||||||
}
|
}
|
||||||
|
22
lib/Logging/LogHandler.php
Normal file
22
lib/Logging/LogHandler.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Logging;
|
||||||
|
|
||||||
|
use MailPoet\Dependencies\Monolog\Handler\AbstractProcessingHandler;
|
||||||
|
use MailPoet\Models\Log;
|
||||||
|
|
||||||
|
class LogHandler extends AbstractProcessingHandler {
|
||||||
|
|
||||||
|
|
||||||
|
protected function write(array $record) {
|
||||||
|
$model = Log::create();
|
||||||
|
$model->hydrate([
|
||||||
|
'name' => $record['channel'],
|
||||||
|
'level' => $record['level'],
|
||||||
|
'message' => $record['formatted'],
|
||||||
|
'created_at' => $record['datetime']->format('Y-m-d H:i:s'),
|
||||||
|
]);
|
||||||
|
$model->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
38
lib/Logging/Logger.php
Normal file
38
lib/Logging/Logger.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Logging;
|
||||||
|
|
||||||
|
use MailPoet\Dependencies\Monolog\Processor\IntrospectionProcessor;
|
||||||
|
use MailPoet\Dependencies\Monolog\Processor\MemoryUsageProcessor;
|
||||||
|
use MailPoet\Dependencies\Monolog\Processor\WebProcessor;
|
||||||
|
|
||||||
|
class Logger {
|
||||||
|
|
||||||
|
/** @var \MailPoet\Dependencies\Monolog\Logger[] */
|
||||||
|
private static $instance = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @param bool $forceCreate
|
||||||
|
*
|
||||||
|
* @return \MailPoet\Dependencies\Monolog\Logger
|
||||||
|
*/
|
||||||
|
public static function getLogger($name = 'MailPoet', $forceCreate = false) {
|
||||||
|
if(!isset(self::$instance[$name]) || $forceCreate) {
|
||||||
|
self::$instance[$name] = new \MailPoet\Dependencies\Monolog\Logger($name);
|
||||||
|
|
||||||
|
if(WP_DEBUG) {
|
||||||
|
// Adds the line/file/class/method from which the log call originated
|
||||||
|
self::$instance[$name]->pushProcessor(new IntrospectionProcessor());
|
||||||
|
// Adds the current request URI, request method and client IP to a log record
|
||||||
|
self::$instance[$name]->pushProcessor(new WebProcessor());
|
||||||
|
// Adds the current memory usage to a log record
|
||||||
|
self::$instance[$name]->pushProcessor(new MemoryUsageProcessor());
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$instance[$name]->pushHandler(new LogHandler());
|
||||||
|
}
|
||||||
|
return self::$instance[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
8
lib/Models/Log.php
Normal file
8
lib/Models/Log.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Models;
|
||||||
|
|
||||||
|
class Log extends Model {
|
||||||
|
public static $_table = MP_LOG_TABLE;
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user