Skip to content

Commit f1aa840

Browse files
committed
Using the new Logger
1 parent b1bb0d5 commit f1aa840

File tree

10 files changed

+72
-26
lines changed

10 files changed

+72
-26
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Lizmap\Logger as Log;
1717
use Lizmap\Logger\Config as LogConfig;
1818
use Lizmap\Logger\Item as LogItem;
19+
use Lizmap\Logger\Logger;
1920
use Lizmap\Project\Project;
2021

2122
/**
@@ -54,6 +55,11 @@ class lizmap
5455
*/
5556
protected static $appContext;
5657

58+
/**
59+
* @var null|Logger The logger instance
60+
*/
61+
protected static $logger;
62+
5763
/**
5864
* this is a static class, so private constructor.
5965
*/
@@ -87,6 +93,18 @@ public static function getAppContext()
8793
return self::$appContext;
8894
}
8995

96+
/**
97+
* @return Logger The logger instance
98+
*/
99+
public static function getLogger()
100+
{
101+
if (!self::$logger) {
102+
self::$logger = new Logger();
103+
}
104+
105+
return self::$logger;
106+
}
107+
90108
public static function saveServices()
91109
{
92110
$ini = new IniModifier(jApp::varConfigPath('lizmapConfig.ini.php'));
@@ -495,6 +513,6 @@ public static function logMetric($label, $service, $payload = array())
495513

496514
$logMessage = new Log\MetricsLogMessage($log, 'metric');
497515

498-
jLog::log($logMessage);
516+
self::getLogger()->info($logMessage);
499517
}
500518
}

lizmap/modules/lizmap/lib/App/JelixContext.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Lizmap\App;
1515

1616
use Jelix\IniFile\IniModifier;
17+
use Lizmap\Logger\Logger;
1718

1819
class JelixContext implements AppContextInterface
1920
{
@@ -210,7 +211,14 @@ public function flushCache($profile = '')
210211
*/
211212
public function logMessage($message, $cat = 'default')
212213
{
213-
\jLog::log($message, $cat);
214+
if ($cat == 'lizmapadmin') {
215+
\jLog::log($message, $cat);
216+
\lizmap::getLogger()->error($message);
217+
} elseif (in_array($cat, Logger::LogLevels)) {
218+
\lizmap::getLogger()->log($cat, $message);
219+
} else {
220+
\jLog::log($message, $cat);
221+
}
214222
}
215223

216224
/**
@@ -221,7 +229,18 @@ public function logMessage($message, $cat = 'default')
221229
*/
222230
public function logException($exception, $cat = 'default')
223231
{
224-
\jLog::logEx($exception, $cat);
232+
if ($cat == 'lizmapadmin') {
233+
\jLog::logEx($exception, $cat);
234+
if (\lizmap::getLogger()->isLevelHighEnough('error')) {
235+
\jLog::logEx($exception, 'error');
236+
}
237+
} elseif (in_array($cat, Logger::LogLevels)) {
238+
if (\lizmap::getLogger()->isLevelHighEnough($cat)) {
239+
\jLog::logEx($exception, $cat);
240+
}
241+
} else {
242+
\jLog::logEx($exception, $cat);
243+
}
225244
}
226245

227246
/**

lizmap/modules/lizmap/lib/Form/QgisFormValueRelationDynamicDatasource.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public function getData($form)
6666
if ($wkt && \lizmapWkt::check($wkt)) {
6767
$geom = \lizmapWkt::parse($wkt);
6868
if ($geom === null) {
69-
\jLog::log('Parsing WKT failed! '.$wkt, 'error');
69+
\lizmap::getLogger()->error(
70+
'Parsing WKT failed! {wkt}',
71+
array('wkt' => $wkt)
72+
);
7073
}
7174
}
7275
} else {

lizmap/modules/lizmap/lib/Project/ProjectConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ private function parseDatavizTreeNode($node, $level, $debug)
653653
$active = ($n == 0) ? 'active' : '';
654654

655655
if ($debug) {
656-
\jLog::log("Node {$subNode->name} - n = {$n} ET active = {$active}");
656+
\lizmap::getLogger()->debug("Node {$subNode->name} - n = {$n} ET active = {$active}");
657657
}
658658
$item = $prefix.' <li class="nav-item" role="presentation">';
659659
$item .= $prefix.' <button class="nav-link '.$active.'" data-bs-target="#dataviz-dnd-'.$level.'-'.md5($subNode->name);

lizmap/modules/lizmap/lib/Project/QgisProject.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ protected function getXml()
787787
if (!is_object($xml)) {
788788
$errormsg = '\n'.basename($qgs_path).'\n'.$xml;
789789
$errormsg = 'An error has been raised when loading QGIS Project:'.$errormsg;
790-
\jLog::log($errormsg, 'lizmapadmin');
790+
\lizmap::getAppContext()->logMessage($errormsg, 'lizmapadmin');
791791

792792
throw new \Exception('The QGIS project '.basename($qgs_path).' has invalid content!');
793793
}
@@ -1404,7 +1404,10 @@ protected function getFieldConfiguration($layerXml)
14041404

14051405
// Option + Attributes
14061406
if (count((array) $options) > 2) {
1407-
\jLog::log('Project '.basename($this->path).': More than one Option found in the Qgis File for field '.$fieldName.', only the first will be read.', 'lizmapadmin');
1407+
$errormsg = 'Project '.basename($this->path);
1408+
$errormsg .= ': More than one Option found in the Qgis File for field ';
1409+
$errormsg .= $fieldName.', only the first will be read.';
1410+
\lizmap::getAppContext()->logMessage($errormsg, 'lizmapadmin');
14081411
}
14091412
$fieldEditOptions = $this->getFieldConfigurationOptions($options);
14101413

lizmap/modules/lizmap/lib/Request/OGCRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ protected function logRequestIfError($code, $headers)
255255
// The master error with MAP parameter
256256
// This user must have an access to QGIS Server logs
257257
$params = $this->parameters();
258-
\jLog::log($message.' Check logs on QGIS Server. '.$this->formatHttpErrorString($params, $code), 'error');
258+
\lizmap::getLogger()->error($message.' Check logs on QGIS Server. '.$this->formatHttpErrorString($params, $code));
259259

260260
// The admin error without the MAP parameter
261261
// but replaced by REPOSITORY and PROJECT parameters
@@ -388,7 +388,7 @@ protected function process_getcapabilities()
388388
} catch (\Exception $e) {
389389
// if qgisprojects profile does not exist, or if there is an
390390
// other error about the cache, let's log it
391-
\jLog::logEx($e, 'error');
391+
\lizmap::getAppContext()->logException($e, 'error');
392392
}
393393
// return cached data
394394
if ($cached !== false) {
@@ -435,7 +435,7 @@ protected function loadXmlString($xmldata, $name)
435435
$errormsg = '\n'.$xmldata.'\n'.$xml;
436436
$errormsg = '\n'.http_build_query($this->params).$errormsg;
437437
$errormsg = 'An error has been raised when loading '.$name.':'.$errormsg;
438-
\jLog::log($errormsg, 'lizmapadmin');
438+
\lizmap::getAppContext()->logMessage($errormsg, 'lizmapadmin');
439439

440440
return null;
441441
}

lizmap/modules/lizmap/lib/Request/Proxy.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static function build($project, $params, $requestXml = null)
128128
if (!is_object($xml)) {
129129
$errormsg = '\n'.$requestXml.'\n'.$xml;
130130
$errormsg = 'An error has been raised when loading requestXml:'.$errormsg;
131-
\jLog::log($errormsg, 'lizmapadmin');
131+
self::getAppContext()->logMessage($errormsg, 'lizmapadmin');
132132
$requestXml = null;
133133
} else {
134134
$request = $xml->getName();
@@ -373,7 +373,7 @@ protected static function logRequestIfError($httpCode, $url, $headers = array())
373373
$error .= ''.$url;
374374
}
375375
\jLog::log($lizmapAdmin, 'lizmapadmin');
376-
\jLog::log($error, 'error');
376+
\lizmap::getLogger()->error($error);
377377
}
378378

379379
/**
@@ -483,12 +483,12 @@ protected static function sendRequest($url, $options)
483483
$response = $client->send($request, $requestOptions);
484484
} catch (ConnectException $e) {
485485
// Handle connection timeout
486-
\jLog::log('Connection failed: '.$e->getMessage(), 'error');
486+
\lizmap::getLogger()->error('Connection failed: '.$e->getMessage());
487487
$response = new Response(504, array(), null, '1.1', 'HTTP/1.1 504 Gateway Timeout');
488488
} catch (RequestException $e) {
489489
// Handle transfer timeout
490490
// throwing exceptions on an HTTP protocol errors (i.e., 4xx and 5xx responses) has been disabled
491-
\jLog::log('Request failed: '.$e->getMessage(), 'error');
491+
\lizmap::getLogger()->error('Request failed: '.$e->getMessage());
492492
if ($e->hasResponse()) {
493493
$response = $e->getResponse();
494494
}
@@ -702,7 +702,10 @@ public static function createVirtualProfile($repository, $project, $layers, $crs
702702
$cacheRootDirectory = $ser->cacheRootDirectory;
703703
if ($cacheStorageType != 'redis') {
704704
if (!is_dir($cacheRootDirectory) or !is_writable($cacheRootDirectory)) {
705-
\jLog::log('cacheRootDirectory "'.$cacheRootDirectory.'" is not a directory or is not writable!', 'lizmapadmin');
705+
self::getAppContext()->logMessage(
706+
'cacheRootDirectory "'.$cacheRootDirectory.'" is not a directory or is not writable!',
707+
'lizmapadmin'
708+
);
706709
$cacheRootDirectory = sys_get_temp_dir();
707710
}
708711
}

lizmap/modules/lizmap/lib/Request/RequestCacheStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function fetch($key)
4444
return $cache;
4545
}
4646
} catch (\Exception $ignored) {
47-
\jLog::logEx($ignored, 'error');
47+
\lizmap::getAppContext()->logException($ignored, 'error');
4848

4949
return null;
5050
}
@@ -80,7 +80,7 @@ public function save($key, $data)
8080
}
8181
} catch (\Exception $ignored) {
8282
// No fail if we can't save it the storage
83-
\jLog::logEx($ignored, 'error');
83+
\lizmap::getAppContext()->logException($ignored, 'error');
8484
}
8585

8686
return false;

lizmap/modules/lizmap/lib/Request/WMSRequest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ protected function process_getcapabilities()
122122
$data = $result->data;
123123
if (empty($data) or floor($result->code / 100) >= 4) {
124124
if (empty($data)) {
125-
\jLog::log(
125+
\lizmap::getAppContext()->logMessage(
126126
'Error in project '.$this->repository->getKey().'/'.$this->project->getKey().': GetCapabilities empty data',
127127
'lizmapadmin'
128128
);
129129
} else {
130-
\jLog::log(
130+
\lizmap::getAppContext()->logMessage(
131131
'Error in project '.$this->repository->getKey().'/'.$this->project->getKey().': GetCapabilities result code: '.$result->code,
132132
'lizmapadmin'
133133
);
@@ -327,7 +327,7 @@ protected function process_getlegendgraphic()
327327
} catch (\Exception $e) {
328328
// if qgisprojects profile does not exist, or if there is an
329329
// other error about the cache, let's log it
330-
\jLog::logEx($e, 'error');
330+
\lizmap::getAppContext()->logException($e, 'error');
331331
}
332332
// return cached data
333333
if ($cached !== false) {
@@ -1035,7 +1035,7 @@ protected function getVProfileInfos($configLayer, $repository, $project)
10351035
return array('error', 'text/plain');
10361036
}
10371037
} catch (UnknownLizmapProjectException $e) {
1038-
\jLog::logEx($e, 'error');
1038+
\lizmap::getAppContext()->logException($e, 'error');
10391039
\jMessage::add('The lizmap project '.strtoupper($project).' does not exist !', 'ProjectNotDefined');
10401040

10411041
return array('error', 'text/plain');
@@ -1071,7 +1071,7 @@ protected function useCache($configLayer, $params, $profile)
10711071
$useCache = false;
10721072
}
10731073
} catch (\Exception $e) {
1074-
\jLog::logEx($e, 'error');
1074+
\lizmap::getAppContext()->logException($e, 'error');
10751075
$useCache = false;
10761076
}
10771077
}
@@ -1113,7 +1113,7 @@ public function getTileCache($params, $profile, $useCache, $forced, $debug = fal
11131113
try {
11141114
$tile = $this->appContext->getCache($key, $profile);
11151115
} catch (\Exception $e) {
1116-
\jLog::logEx($e, 'error');
1116+
\lizmap::getAppContext()->logException($e, 'error');
11171117
$tile = false;
11181118
}
11191119
if ($tile) {
@@ -1350,7 +1350,7 @@ protected function getMapData($project, $params, $forced = false)
13501350
// Store into cache if needed
13511351
$cached = false;
13521352
if ($useCache) {
1353-
// ~ \jLog::log( ' Store into cache');
1353+
\lizmap::getLogger()->debug(' Store into cache');
13541354
$cacheExpiration = (int) $this->services->cacheExpiration;
13551355
if (property_exists($configLayer, 'cacheExpiration')) {
13561356
$cacheExpiration = (int) $configLayer->cacheExpiration;
@@ -1364,7 +1364,7 @@ protected function getMapData($project, $params, $forced = false)
13641364
'qgisParams' => $params,
13651365
));
13661366
} catch (\Exception $e) {
1367-
\jLog::logEx($e, 'error');
1367+
\lizmap::getAppContext()->logException($e, 'error');
13681368
$cached = false;
13691369
}
13701370
}

lizmap/modules/lizmap/lib/Request/WMTSRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected function process_getcapabilities()
9393
*/
9494
protected function process_gettile()
9595
{
96-
// \jLog::log('GetTile '.http_build_query($this->params));
96+
\lizmap::getLogger()->debug('GetTile '.http_build_query($this->params));
9797
// Get the parameters values
9898
$params = array(
9999
'LayerName' => 'Layer',

0 commit comments

Comments
 (0)