Skip to content

Commit 872bef4

Browse files
committed
Config Logger
1 parent 7093ff9 commit 872bef4

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

docker/update-config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @param IniModifier $iniFileModifier
9+
* @param mixed $varname
910
*/
1011
function load_include_config($varname, $iniFileModifier)
1112
{
@@ -172,6 +173,12 @@ function checkAndWaitPostgresql($profilesConfig, $profileName, $nbRetries = 10,
172173
// DropIn capabilities: Merge all ini file in LIZMAP_LIZMAPCONFIG_INCLUDE
173174
load_include_config('LIZMAP_LIZMAPCONFIG_INCLUDE', $lizmapConfig);
174175

176+
// Enable metrics
177+
$log_level = getenv('LIZMAP_LOGLEVEL');
178+
if (is_numeric($log_level) || $log_level !== '') {
179+
$lizmapConfig->setValue('logLevel', $log_level, 'services');
180+
}
181+
175182
// Enable metrics
176183
$logger_metric = getenv('LIZMAP_LOGMETRICS');
177184
if ($logger_metric !== false) {

lizmap/modules/admin/forms/config_services.form.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@
114114
<item value="1" locale="admin~admin.form.admin_services.debugMode.1.label"/>
115115
</menulist>
116116

117+
<menulist ref="logLevel" required="true">
118+
<label locale="admin~admin.form.admin_services.logLevel.label"/>
119+
<help locale="admin~admin.form.admin_services.logLevel.help"/>
120+
<item value="emergency" locale="admin~admin.form.admin_services.logLevel.emergency.label"/>
121+
<item value="alert" locale="admin~admin.form.admin_services.logLevel.alert.label"/>
122+
<item value="critical" locale="admin~admin.form.admin_services.logLevel.critical.label"/>
123+
<item value="error" selected="true" locale="admin~admin.form.admin_services.logLevel.error.label"/>
124+
<item value="warning" locale="admin~admin.form.admin_services.logLevel.warning.label"/>
125+
<item value="notice" locale="admin~admin.form.admin_services.logLevel.notice.label"/>
126+
<item value="info" locale="admin~admin.form.admin_services.logLevel.info.label"/>
127+
<item value="debug" locale="admin~admin.form.admin_services.logLevel.debug.label"/>
128+
</menulist>
129+
117130
<group ref="requestProxyEnabled" withcheckbox="true">
118131
<oncheckvalue locale="admin~admin.form.admin_services.requestProxy.enabled" value="1"/>
119132
<onuncheckvalue locale="admin~admin.form.admin_services.requestProxy.disabled" value="0" />

lizmap/modules/admin/locales/en_US/admin.UTF-8.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ form.admin_services.requestProxyNotForDomain.label=Domains for which the proxy w
108108
form.admin_services.debugMode.label=Debug mode
109109
form.admin_services.debugMode.0.label=Off
110110
form.admin_services.debugMode.1.label=Log
111+
form.admin_services.logLevel.label=Log level
112+
form.admin_services.logLevel.help=Select the level of severity of the messages to log.
113+
form.admin_services.logLevel.emergency.label=Emergency: indicates that the system is unusable and requires immediate attention.
114+
form.admin_services.logLevel.alert.label=Alert: indicates that immediate action is necessary to resolve a critical issue.
115+
form.admin_services.logLevel.critical.label=Critical: signifies critical conditions in the program that demand intervention to prevent system failure.
116+
form.admin_services.logLevel.error.label=Error: indicates error conditions that impair some operation but are less severe than critical situations.
117+
form.admin_services.logLevel.warning.label=Warning: signifies potential issues that may lead to errors or unexpected behavior in the future if not addressed.
118+
form.admin_services.logLevel.notice.label=Notice: applies to normal but significant conditions that may require monitoring.
119+
form.admin_services.logLevel.info.label=Info: includes messages that provide a record of the normal operation of the system.
120+
form.admin_services.logLevel.debug.label=Debug: intended for logging detailed information about the system for debugging purposes.
111121
form.admin_services.onlyMaps.label=Disable landing page
112122
form.admin_services.onlyMaps.help=If set to "On", no landing page will be available.
113123
form.admin_services.projectSwitcher.label=Show projects switcher

lizmap/modules/lizmap/classes/lizmap.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static function getAppContext()
9999
public static function getLogger()
100100
{
101101
if (!self::$logger) {
102-
self::$logger = new Logger();
102+
self::$logger = new Logger(self::getServices()->logLevel);
103103
}
104104

105105
return self::$logger;

lizmap/modules/lizmap/classes/lizmapServices.class.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3+
use Lizmap\Logger\Logger;
34
use Lizmap\Server\Server;
5+
use Psr\Log\LogLevel;
46

57
/**
68
* Manage and give access to lizmap configuration.
@@ -52,6 +54,7 @@ class lizmapServices
5254
'requestProxyType',
5355
'requestProxyNotForDomain',
5456
'debugMode',
57+
'logLevel',
5558
'cacheRootDirectory',
5659
'cacheRedisHost',
5760
'cacheRedisPort',
@@ -315,6 +318,13 @@ class lizmapServices
315318
*/
316319
public $debugMode = '';
317320

321+
/**
322+
* @var string Log level
323+
*
324+
* @see LogLevel
325+
*/
326+
public $logLevel = Logger::DefaultLevel;
327+
318328
/**
319329
* Cache root directory.
320330
*
@@ -452,6 +462,9 @@ public function __construct($readConfigPath, $globalConfig, $ldapEnabled, $varPa
452462
}
453463
}
454464

465+
// Check log level property
466+
$this->checkLogLevel();
467+
455468
if (!is_array($this->wmsServerHeaders)) {
456469
$this->wmsServerHeaders = array();
457470
}
@@ -613,9 +626,42 @@ public function modify($data)
613626
}
614627
}
615628

629+
$this->checkLogLevel();
630+
616631
return $modified;
617632
}
618633

634+
/**
635+
* Checking the log level property and update it if necessary.
636+
*/
637+
protected function checkLogLevel(): string
638+
{
639+
// check log level
640+
if (is_numeric($this->logLevel)) {
641+
$logLevel = (int) $this->logLevel;
642+
if ($logLevel < 0) {
643+
$this->logLevel = Logger::LogLevels[0];
644+
} elseif ($logLevel > 7) {
645+
$this->logLevel = Logger::LogLevels[7];
646+
} else {
647+
$this->logLevel = Logger::LogLevels[$logLevel];
648+
}
649+
} elseif (!in_array($this->logLevel, Logger::LogLevels)) {
650+
$this->logLevel = Logger::DefaultLevel;
651+
}
652+
653+
// Force log level to debug if debug mode is on
654+
if ($this->debugMode === '1' && $this->logLevel !== Logger::LogLevels[0]) {
655+
$this->logLevel = Logger::LogLevels[0];
656+
}
657+
// Force debugMode to 1 if log level is debug
658+
if ($this->debugMode !== '1' && $this->logLevel === Logger::LogLevels[0]) {
659+
$this->debugMode = '1';
660+
}
661+
662+
return $this->logLevel;
663+
}
664+
619665
/**
620666
* Host URL to the Lizmap QGIS Server API, taking care of the QGIS Server context : FCGI, QJazz, etc.
621667
*

tests/units/classes/lizmapServicesTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,23 @@ public static function getModifyLocalData()
235235
'adminContactEmail' => 'test.test@test.com',
236236
'debugMode' => 'off',
237237
);
238+
$testModify4_1 = array(
239+
'appName' => 'Lizmap',
240+
'adminContactEmail' => 'test.test@test.com',
241+
'logLevel' => 'debug',
242+
);
243+
$testModify5_1 = array(
244+
'appName' => 'Lizmap',
245+
'adminContactEmail' => 'test.test@test.com',
246+
'logLevel' => '1',
247+
);
238248

239249
return array(
240250
array($testModify1, $testModify1_1, null, null, true),
241251
array($testModify1, $testModify2_1, 'adminContactEmail', 'test.test@3liz.org', true),
242252
array($testModify1, $testModify3_1, 'debugMode', 'off', true),
253+
array($testModify1, $testModify4_1, 'logLevel', 'debug', true),
254+
array($testModify1, $testModify5_1, 'logLevel', 'alert', true),
243255
array($testModify1, null, null, null, false),
244256
array($testModify2, $testModify1_1, 'adminContactEmail', 'test.test@test.com', true),
245257
array(null, $testModify1_1, 'adminContactEmail', 'test.test@test.com', true),
@@ -325,6 +337,7 @@ public function testSaveIntoIni($dataModification, $expectedIniValues, $expected
325337
'requestProxyNotForDomain',
326338
'cacheRedisHost',
327339
'cacheRedisPort',
340+
'logLevel',
328341
);
329342

330343
$testLizmapServices = new LizmapServices(

0 commit comments

Comments
 (0)