Skip to content

Commit

Permalink
Merge pull request #561 from MekDrop/improvement/logging-with-monolog
Browse files Browse the repository at this point in the history
Rewritten logger to use Monolog
  • Loading branch information
MekDrop committed Jan 27, 2020
2 parents 7760aff + 4c4edb7 commit 399f951
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 562 deletions.
15 changes: 13 additions & 2 deletions .env.example
@@ -1,4 +1,4 @@
# Copy this file as .env or set these enviroment variables in server configuration (more secure option)
# Copy this file as .env or set these environment variables in server configuration (more secure option)

# What database driver could be used for database?
# Right now only pdo.mysql driver is supported
Expand Down Expand Up @@ -32,4 +32,15 @@ DB_COLLATION=utf8_general_ci
DB_PREFIX=icms

# String that will be used for protecting sensitive data on database
DB_SALT=icms
DB_SALT=icms

# If need to use some debugging, we write here what kind debuging we need:
# FirePHP - if you use FirePHP (http://www.firephp.org/)
# ChromePHP - if you use Chrome PHP (http://www.chromephp.com/)
# BrowserConsole - any browser console compatible
# PHPConsole - if you use PHP console (https://chrome.google.com/webstore/detail/php-console/nfhmhhlpfleoednkpnnnkolmclajemef)
# Multiple console loggers can be separated by ',' characters
DEBUG_TOOL=

# Do you need logging?
LOGGING_ENABLED=false
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -61,7 +61,9 @@
"league/flysystem": "^1.0.51",
"lulco/phoenix": "^1.1",
"mibe/feedwriter": "^1.1",
"monolog/monolog": "^2.0",
"old-xoops-libraries/php-downloader": "1.*",
"php-console/php-console": "^3.1",
"phpexperts/laravel-env-polyfill": "^1.0",
"phpmailer/phpmailer": "^6.0.7",
"simplepie/simplepie": "^1.5",
Expand Down
134 changes: 133 additions & 1 deletion composer.lock

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

27 changes: 25 additions & 2 deletions core/Providers/DatabaseServiceProvider.php
Expand Up @@ -96,16 +96,17 @@ function () use ($container) {
* @param int $port Port
*
* @return ExtendedPdoInterface
* @throws \Exception
*/
protected function createDatabaseConnection(string $type, string $host, string $user, ?string $pass, $persistentConnection, ?string $name, ?string $charset, ?string $prefix, int $port): ExtendedPdoInterface
{
if (substr($type, 0, 4) == 'pdo.') {
$type = substr($type, 4);
}

$dsn = $type . ":host=" . $host;
$dsn = $type . ':host=' . $host;
if ($name && !(defined('DB_NO_AUTO_SELECT') && DB_NO_AUTO_SELECT)) {
$dsn .= ";dbname=" . $name;
$dsn .= ';dbname=' . $name;
}
$dsn .= ';port=' . $port;
if ($charset) {
Expand Down Expand Up @@ -134,6 +135,28 @@ protected function createDatabaseConnection(string $type, string $host, string $
trigger_error(_CORE_DB_NOTRACEDB, E_USER_ERROR);
}

$enabled = (bool)env('LOGGING_ENABLED', false);
$logger = new \icms_core_Logger(
'DB',
[
new \Monolog\Handler\RotatingFileHandler(
ICMS_LOGGING_PATH . '/db.log',
0,
$enabled ? \Monolog\Logger::DEBUG : \Monolog\Logger::ERROR
)
]
);
if ($enabled) {
$logger->enableRendering();
}

$connection->setProfiler(
new \Aura\Sql\Profiler\Profiler($logger)
);

$connection->getProfiler()->setLogFormat('{function} ({duration} seconds): {statement}');
$connection->getProfiler()->setActive($enabled);

return $connection;
}
}
23 changes: 6 additions & 17 deletions include/common.php
Expand Up @@ -114,23 +114,12 @@
}

global $xoopsOption, $icmsConfig;
if (!isset ($xoopsOption ['nodebug']) || !$xoopsOption ['nodebug']) {
if ($icmsConfig ['debug_mode'] == 1 || $icmsConfig ['debug_mode'] == 2) {
error_reporting(E_ALL);
icms::getInstance()->get('logger')->enableRendering();
icms::getInstance()->get('logger')->usePopup = ($icmsConfig ['debug_mode'] == 2);
if (icms::getInstance()->has('db')) {
icms_Event::attach('icms_db_IConnection', 'prepare', function($params) {
icms::getInstance()->get('logger')->addQuery('prepare: ' . $params ['sql']);
});
icms_Event::attach('icms_db_IConnection', 'execute', function($params) {
icms::getInstance()->get('logger')->addQuery('execute: ' . $params ['sql']);
});
}
} else {
error_reporting(0);
icms::getInstance()->get('logger')->activated = false;
}
if (isset($xoopsOption['nodebug']) && $xoopsOption['nodebug']) {
/**
* @var icms_core_Logger $logger
*/
$logger = icms::getInstance()->get('logger');
$logger->disableLogger();
}
//TODO: change this, because it is using deprecated notations
icms::$module = icms::getInstance()->get('module');
Expand Down
1 change: 1 addition & 0 deletions include/constants.php
Expand Up @@ -75,6 +75,7 @@
define('ICMS_THEME_URL', ICMS_URL . '/themes');
define('ICMS_STORAGE_PATH', ICMS_ROOT_PATH . DIRECTORY_SEPARATOR . 'storage');
define('ICMS_CACHE_PATH', ICMS_STORAGE_PATH . DIRECTORY_SEPARATOR . 'cache');
define('ICMS_LOGGING_PATH', ICMS_STORAGE_PATH . DIRECTORY_SEPARATOR . 'log');
define('ICMS_PURIFIER_CACHE', ICMS_STORAGE_PATH . DIRECTORY_SEPARATOR . 'htmlpurifier');
define('ICMS_COMPILE_PATH', ICMS_STORAGE_PATH . DIRECTORY_SEPARATOR . 'templates_c');
define('ICMS_IMAGES_URL', ICMS_URL . '/images');
Expand Down
15 changes: 0 additions & 15 deletions language/english/global.php
Expand Up @@ -222,21 +222,6 @@
define('_CT_RELATEDS','Related pages');
//Security image (captcha)
define("_SECURITYIMAGE_GETCODE","Enter the security code");
define("_QUERIES", "Queries");
define("_BLOCKS", "Blocks");
define("_EXTRA", "Extra");
define("_TIMERS", "Timers");
define("_CACHED", "Cached");
define("_REGENERATES", "Regenerates every %s seconds");
define("_TOTAL", "Total :");
define("_ERR_NR", "Error number:");
define("_ERR_MSG", "Error message:");
define("_NOTICE", "Notice");
define("_WARNING", "Warning");
define("_STRICT", "Strict");
define("_ERROR", "Error(s)");
define("_TOOKXLONG", " took %s seconds to load.");
define("_BLOCK", "Block(s)");
define("_WARNINGUPDATESYSTEM","Congratulations, you have just successfully upgraded your site to the latest version of ImpressCMS!<br />Therefor to finish the upgrade process you'll need to click here and update your system module.<br />Click here to process the upgrade.");

// This shows local support site in ImpressCMS menu, (if selected language is not English)
Expand Down

0 comments on commit 399f951

Please sign in to comment.