From 337e0e3534fd64a3c00f330f831ec89a1e138b50 Mon Sep 17 00:00:00 2001 From: ebocher Date: Wed, 13 Sep 2023 15:55:14 +0200 Subject: [PATCH] Fix logger --- .../geoclimate/bdtopo/BDTopoUtils.groovy | 4 +--- .../geoclimate/utils/AbstractScript.groovy | 7 ++++-- .../geoclimate/utils/LoggerUtils.groovy | 13 ++++++++++ .../org/orbisgis/geoclimate/Geoclimate.groovy | 5 ++-- .../orbisgis/geoclimate/Geoindicators.groovy | 8 ++----- .../geoindicators/WorkflowUtilities.groovy | 24 ------------------- .../WorkflowGeoIndicatorsTest.groovy | 6 ++--- osm/pom.xml | 4 ++++ .../org/orbisgis/geoclimate/osm/OSM.groovy | 6 ++--- .../osm/WorkflowAbstractTest.groovy | 6 ++--- .../geoclimate/osmtools/OSMTools.groovy | 4 +--- .../geoclimate/osmtools/LoaderTest.groovy | 6 ++--- .../geoclimate/osmtools/TransformTest.groovy | 6 ++--- .../osmtools/utils/TransformUtilsTest.groovy | 6 ++--- .../osmtools/utils/UtilitiesTest.groovy | 6 ++--- .../worldpoptools/WorldPopTools.groovy | 4 +--- .../worldpoptools/WorldPopExtractTest.groovy | 6 ++--- 17 files changed, 53 insertions(+), 68 deletions(-) diff --git a/bdtopo/src/main/groovy/org/orbisgis/geoclimate/bdtopo/BDTopoUtils.groovy b/bdtopo/src/main/groovy/org/orbisgis/geoclimate/bdtopo/BDTopoUtils.groovy index a4c9773495..d01b6d6302 100644 --- a/bdtopo/src/main/groovy/org/orbisgis/geoclimate/bdtopo/BDTopoUtils.groovy +++ b/bdtopo/src/main/groovy/org/orbisgis/geoclimate/bdtopo/BDTopoUtils.groovy @@ -24,7 +24,6 @@ import org.locationtech.jts.geom.Envelope import org.locationtech.jts.geom.Geometry import org.orbisgis.geoclimate.utils.AbstractScript import org.orbisgis.geoclimate.utils.LoggerUtils -import org.slf4j.LoggerFactory /** * BDTopo utils @@ -32,8 +31,7 @@ import org.slf4j.LoggerFactory abstract class BDTopoUtils extends AbstractScript { BDTopoUtils() { - super(LoggerFactory.getLogger(BDTopoUtils.class)) - LoggerUtils.setLoggerLevel("INFO") + super(BDTopoUtils.class) } /** diff --git a/common-utils/src/main/groovy/org/orbisgis/geoclimate/utils/AbstractScript.groovy b/common-utils/src/main/groovy/org/orbisgis/geoclimate/utils/AbstractScript.groovy index 8032b249ab..209003332a 100644 --- a/common-utils/src/main/groovy/org/orbisgis/geoclimate/utils/AbstractScript.groovy +++ b/common-utils/src/main/groovy/org/orbisgis/geoclimate/utils/AbstractScript.groovy @@ -1,13 +1,16 @@ package org.orbisgis.geoclimate.utils import ch.qos.logback.classic.Logger +import ch.qos.logback.classic.LoggerContext abstract class AbstractScript extends Script { public Logger logger - AbstractScript(Logger logger) { - this.logger = logger + AbstractScript(Class aClass) { + LoggerContext context = new LoggerContext() + this.logger = context.getLogger(aClass) + LoggerUtils.setLoggerLevel("INFO") } static String uuid() { UUID.randomUUID().toString().replaceAll("-", "_") } diff --git a/common-utils/src/main/groovy/org/orbisgis/geoclimate/utils/LoggerUtils.groovy b/common-utils/src/main/groovy/org/orbisgis/geoclimate/utils/LoggerUtils.groovy index f18376e6fe..289bd467c2 100644 --- a/common-utils/src/main/groovy/org/orbisgis/geoclimate/utils/LoggerUtils.groovy +++ b/common-utils/src/main/groovy/org/orbisgis/geoclimate/utils/LoggerUtils.groovy @@ -1,12 +1,14 @@ package org.orbisgis.geoclimate.utils import ch.qos.logback.classic.Level +import ch.qos.logback.classic.Logger import ch.qos.logback.classic.LoggerContext import org.slf4j.LoggerFactory class LoggerUtils { + /** * Utility class to change log level for all loggers * @@ -29,4 +31,15 @@ class LoggerUtils { context.getLoggerList().each { it -> it.setLevel(level) } } } + + + /** + * Create a logback logger + * @param aClass + * @return + */ + static Logger createLogger(Class aClass){ + LoggerContext context = new LoggerContext() + return context.getLogger(aClass) + } } diff --git a/geoclimate/src/main/groovy/org/orbisgis/geoclimate/Geoclimate.groovy b/geoclimate/src/main/groovy/org/orbisgis/geoclimate/Geoclimate.groovy index c7be4333a7..ec834fc3d0 100644 --- a/geoclimate/src/main/groovy/org/orbisgis/geoclimate/Geoclimate.groovy +++ b/geoclimate/src/main/groovy/org/orbisgis/geoclimate/Geoclimate.groovy @@ -20,6 +20,7 @@ package org.orbisgis.geoclimate import org.orbisgis.geoclimate.bdtopo.BDTopo +import org.orbisgis.geoclimate.utils.LoggerUtils import picocli.CommandLine import java.util.concurrent.Callable @@ -84,9 +85,9 @@ class Geoclimate implements Callable { @Override Integer call() { if (verbose) { - Geoindicators.WorkflowUtilities.setLoggerLevel(verbose.trim()) + LoggerUtils.setLoggerLevel(verbose.trim()) } else { - Geoindicators.WorkflowUtilities.setLoggerLevel("INFO") + LoggerUtils.setLoggerLevel("INFO") } if (workflow.trim().equalsIgnoreCase("OSM")) { println("The OSM workflow has been started.\nPlease wait...") diff --git a/geoindicators/src/main/groovy/org/orbisgis/geoclimate/Geoindicators.groovy b/geoindicators/src/main/groovy/org/orbisgis/geoclimate/Geoindicators.groovy index 4e80ed0643..5b78998c7f 100644 --- a/geoindicators/src/main/groovy/org/orbisgis/geoclimate/Geoindicators.groovy +++ b/geoindicators/src/main/groovy/org/orbisgis/geoclimate/Geoindicators.groovy @@ -20,18 +20,14 @@ package org.orbisgis.geoclimate import ch.qos.logback.classic.Level -import ch.qos.logback.classic.LoggerContext import org.orbisgis.geoclimate.geoindicators.* import org.orbisgis.geoclimate.utils.AbstractScript -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils abstract class Geoindicators extends AbstractScript { - public static def logger Geoindicators() { - super(LoggerFactory.getLogger(Geoindicators.class)) - var context = (LoggerContext) LoggerFactory.getILoggerFactory() - context.getLogger(Geoindicators.class).setLevel(Level.INFO) + super(Geoindicators.class) } //Processes diff --git a/geoindicators/src/main/groovy/org/orbisgis/geoclimate/geoindicators/WorkflowUtilities.groovy b/geoindicators/src/main/groovy/org/orbisgis/geoclimate/geoindicators/WorkflowUtilities.groovy index 2d76dba989..7c0c8481f0 100644 --- a/geoindicators/src/main/groovy/org/orbisgis/geoclimate/geoindicators/WorkflowUtilities.groovy +++ b/geoindicators/src/main/groovy/org/orbisgis/geoclimate/geoindicators/WorkflowUtilities.groovy @@ -28,7 +28,6 @@ import org.orbisgis.data.H2GIS import org.orbisgis.data.POSTGIS import org.orbisgis.data.jdbc.JdbcDataSource import org.orbisgis.geoclimate.Geoindicators -import org.slf4j.LoggerFactory @BaseScript Geoindicators geoindicators @@ -249,27 +248,4 @@ def saveToCSV(def outputTable, def filePath, def h2gis_datasource, def deleteOut h2gis_datasource.save("(SELECT ID_BUILD, ID_SOURCE FROM $outputTable WHERE estimated=true)", filePath, deleteOutputData) info "${outputTable} has been saved in ${filePath}." } -} - -/** - * Utility class to change log level for all loggers - * - */ -static def setLoggerLevel(String loggerLevel) { - if (loggerLevel) { - Level level - if (loggerLevel.equalsIgnoreCase("INFO")) { - level = Level.INFO - } else if (loggerLevel.equalsIgnoreCase("DEBUG")) { - level = Level.DEBUG - } else if (loggerLevel.equalsIgnoreCase("TRACE")) { - level = Level.TRACE - } else if (loggerLevel.equalsIgnoreCase("OFF")) { - level = Level.OFF - } else { - throw new RuntimeException("Invalid log level. Allowed values are : INFO, DEBUG, TRACE, OFF") - } - var context = (LoggerContext) LoggerFactory.getILoggerFactory() - context.getLoggerList().each { it -> it.setLevel(level) } - } } \ No newline at end of file diff --git a/geoindicators/src/test/groovy/org/orbisgis/geoclimate/geoindicators/WorkflowGeoIndicatorsTest.groovy b/geoindicators/src/test/groovy/org/orbisgis/geoclimate/geoindicators/WorkflowGeoIndicatorsTest.groovy index 17f5b014b3..aef02a435d 100644 --- a/geoindicators/src/test/groovy/org/orbisgis/geoclimate/geoindicators/WorkflowGeoIndicatorsTest.groovy +++ b/geoindicators/src/test/groovy/org/orbisgis/geoclimate/geoindicators/WorkflowGeoIndicatorsTest.groovy @@ -19,14 +19,14 @@ */ package org.orbisgis.geoclimate.geoindicators +import ch.qos.logback.classic.Logger import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir import org.orbisgis.data.H2GIS import org.orbisgis.data.dataframe.DataFrame import org.orbisgis.geoclimate.Geoindicators -import org.slf4j.Logger -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils import static org.junit.jupiter.api.Assertions.* import static org.orbisgis.data.H2GIS.open @@ -36,7 +36,7 @@ class WorkflowGeoIndicatorsTest { @TempDir static File folder - public static Logger logger = LoggerFactory.getLogger(WorkflowGeoIndicatorsTest.class) + public static Logger logger = LoggerUtils.createLogger(WorkflowGeoIndicatorsTest.class) // Indicator list (at RSU scale) for each type of use public static listNames = [ diff --git a/osm/pom.xml b/osm/pom.xml index a00fd4303e..600de6639d 100644 --- a/osm/pom.xml +++ b/osm/pom.xml @@ -63,6 +63,10 @@ ch.qos.logback logback-classic + + org.orbisgis.geoclimate + common-utils + org.junit.jupiter diff --git a/osm/src/main/groovy/org/orbisgis/geoclimate/osm/OSM.groovy b/osm/src/main/groovy/org/orbisgis/geoclimate/osm/OSM.groovy index 6fcd15dab7..2651dcb2aa 100644 --- a/osm/src/main/groovy/org/orbisgis/geoclimate/osm/OSM.groovy +++ b/osm/src/main/groovy/org/orbisgis/geoclimate/osm/OSM.groovy @@ -21,10 +21,9 @@ package org.orbisgis.geoclimate.osm import org.locationtech.jts.geom.Envelope import org.locationtech.jts.geom.Geometry -import org.orbisgis.geoclimate.geoindicators.WorkflowUtilities import org.orbisgis.geoclimate.osmtools.utils.Utilities import org.orbisgis.geoclimate.utils.AbstractScript -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils /** * Main class to access to the OSM processes @@ -37,8 +36,7 @@ abstract class OSM extends AbstractScript { public static InputDataFormatting = new InputDataFormatting() OSM() { - super(LoggerFactory.getLogger(OSM.class)) - WorkflowUtilities.setLoggerLevel("INFO") + super(OSM.class) } diff --git a/osm/src/test/groovy/org/orbisgis/geoclimate/osm/WorkflowAbstractTest.groovy b/osm/src/test/groovy/org/orbisgis/geoclimate/osm/WorkflowAbstractTest.groovy index 1756343d8e..b989990453 100644 --- a/osm/src/test/groovy/org/orbisgis/geoclimate/osm/WorkflowAbstractTest.groovy +++ b/osm/src/test/groovy/org/orbisgis/geoclimate/osm/WorkflowAbstractTest.groovy @@ -19,15 +19,15 @@ */ package org.orbisgis.geoclimate.osm +import ch.qos.logback.classic.Logger import org.orbisgis.geoclimate.Geoindicators -import org.slf4j.Logger -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils import static org.junit.jupiter.api.Assertions.* class WorkflowAbstractTest { - public static Logger logger = LoggerFactory.getLogger(WorkflowAbstractTest.class) + public static Logger logger = LoggerUtils.createLogger(WorkflowAbstractTest.class) /** * A method to compute geomorphological indicators diff --git a/osmtools/src/main/groovy/org/orbisgis/geoclimate/osmtools/OSMTools.groovy b/osmtools/src/main/groovy/org/orbisgis/geoclimate/osmtools/OSMTools.groovy index 35d57c6b13..1fe86943ad 100644 --- a/osmtools/src/main/groovy/org/orbisgis/geoclimate/osmtools/OSMTools.groovy +++ b/osmtools/src/main/groovy/org/orbisgis/geoclimate/osmtools/OSMTools.groovy @@ -25,7 +25,6 @@ import org.orbisgis.geoclimate.osmtools.utils.TransformUtils as TRANSFORM_UTILS import org.orbisgis.geoclimate.osmtools.utils.Utilities as UTILITIES import org.orbisgis.geoclimate.utils.AbstractScript import org.orbisgis.geoclimate.utils.LoggerUtils -import org.slf4j.LoggerFactory /** * Main script to access to all processes used to extract, transform and save OSM data as GIS layers. @@ -42,7 +41,6 @@ abstract class OSMTools extends AbstractScript { def static TransformUtils = new TRANSFORM_UTILS() OSMTools() { - super(LoggerFactory.getLogger(OSMTools.class)) - LoggerUtils.setLoggerLevel("INFO") + super(OSMTools.class) } } \ No newline at end of file diff --git a/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/LoaderTest.groovy b/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/LoaderTest.groovy index 2bdd405e61..70bf52ccc8 100644 --- a/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/LoaderTest.groovy +++ b/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/LoaderTest.groovy @@ -19,6 +19,7 @@ */ package org.orbisgis.geoclimate.osmtools +import ch.qos.logback.classic.Logger import org.junit.jupiter.api.* import org.junit.jupiter.api.io.TempDir import org.locationtech.jts.geom.Coordinate @@ -26,8 +27,7 @@ import org.locationtech.jts.geom.GeometryFactory import org.orbisgis.data.H2GIS import org.orbisgis.geoclimate.osmtools.utils.OSMElement import org.orbisgis.geoclimate.osmtools.utils.Utilities -import org.slf4j.Logger -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils import java.util.regex.Pattern @@ -44,7 +44,7 @@ class LoaderTest extends AbstractOSMToolsTest { @TempDir static File folder - private static final Logger LOGGER = LoggerFactory.getLogger(LoaderTest) + private static final Logger LOGGER = LoggerUtils.createLogger(LoaderTest) static H2GIS ds diff --git a/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/TransformTest.groovy b/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/TransformTest.groovy index 6b36ce5463..cc9286eb35 100644 --- a/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/TransformTest.groovy +++ b/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/TransformTest.groovy @@ -19,6 +19,7 @@ */ package org.orbisgis.geoclimate.osmtools +import ch.qos.logback.classic.Logger import org.h2gis.utilities.GeographyUtilities import org.junit.jupiter.api.* import org.junit.jupiter.api.io.TempDir @@ -26,8 +27,7 @@ import org.locationtech.jts.geom.* import org.orbisgis.data.H2GIS import org.orbisgis.geoclimate.osmtools.utils.OSMElement import org.orbisgis.geoclimate.osmtools.utils.Utilities -import org.slf4j.Logger -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils import static org.junit.jupiter.api.Assertions.* @@ -42,7 +42,7 @@ class TransformTest extends AbstractOSMToolsTest { @TempDir static File folder - private static final Logger LOGGER = LoggerFactory.getLogger(TransformTest) + private static final Logger LOGGER = LoggerUtils.createLogger(TransformTest) static H2GIS ds diff --git a/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/utils/TransformUtilsTest.groovy b/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/utils/TransformUtilsTest.groovy index bc08c7b3d5..6d428af073 100644 --- a/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/utils/TransformUtilsTest.groovy +++ b/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/utils/TransformUtilsTest.groovy @@ -19,6 +19,7 @@ */ package org.orbisgis.geoclimate.osmtools.utils +import ch.qos.logback.classic.Logger import org.junit.jupiter.api.* import org.junit.jupiter.api.io.CleanupMode import org.junit.jupiter.api.io.TempDir @@ -27,8 +28,7 @@ import org.locationtech.jts.geom.MultiLineString import org.orbisgis.data.H2GIS import org.orbisgis.geoclimate.osmtools.AbstractOSMToolsTest import org.orbisgis.geoclimate.osmtools.OSMTools -import org.slf4j.Logger -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils import static org.junit.jupiter.api.Assertions.* @@ -43,7 +43,7 @@ class TransformUtilsTest extends AbstractOSMToolsTest { @TempDir(cleanup = CleanupMode.ON_SUCCESS) static File folder - private static final Logger LOGGER = LoggerFactory.getLogger(TransformUtilsTest) + private static final Logger LOGGER = LoggerUtils.createLogger(TransformUtilsTest) static H2GIS h2gis diff --git a/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/utils/UtilitiesTest.groovy b/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/utils/UtilitiesTest.groovy index 74c557521a..66c0643050 100644 --- a/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/utils/UtilitiesTest.groovy +++ b/osmtools/src/test/groovy/org/orbisgis/geoclimate/osmtools/utils/UtilitiesTest.groovy @@ -19,6 +19,7 @@ */ package org.orbisgis.geoclimate.osmtools.utils +import ch.qos.logback.classic.Logger import org.junit.jupiter.api.* import org.junit.jupiter.api.io.CleanupMode import org.junit.jupiter.api.io.TempDir @@ -29,8 +30,7 @@ import org.locationtech.jts.geom.Polygon import org.orbisgis.data.H2GIS import org.orbisgis.geoclimate.osmtools.AbstractOSMToolsTest import org.orbisgis.geoclimate.osmtools.OSMTools -import org.slf4j.Logger -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils import java.util.regex.Pattern @@ -47,7 +47,7 @@ class UtilitiesTest extends AbstractOSMToolsTest { @TempDir(cleanup = CleanupMode.ON_SUCCESS) static File folder - private static final Logger LOGGER = LoggerFactory.getLogger(UtilitiesTest) + private static final Logger LOGGER = LoggerUtils.createLogger(UtilitiesTest) /** Used to store method pointer in order to replace it for the tests to avoid call to Overpass servers. */ private static def executeOverPassQuery diff --git a/worldpoptools/src/main/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopTools.groovy b/worldpoptools/src/main/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopTools.groovy index a859da4cea..ed0568abe4 100644 --- a/worldpoptools/src/main/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopTools.groovy +++ b/worldpoptools/src/main/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopTools.groovy @@ -32,7 +32,6 @@ package org.orbisgis.geoclimate.worldpoptools import org.orbisgis.geoclimate.utils.AbstractScript import org.orbisgis.geoclimate.utils.LoggerUtils -import org.slf4j.LoggerFactory /** * Main script to access to all WorldPop utilities @@ -48,7 +47,6 @@ abstract class WorldPopTools extends AbstractScript { def static Extract = new WorldPopExtract() WorldPopTools() { - super(LoggerFactory.getLogger(WorldPopTools.class)) - LoggerUtils.setLoggerLevel("INFO") + super(WorldPopTools.class) } } \ No newline at end of file diff --git a/worldpoptools/src/test/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtractTest.groovy b/worldpoptools/src/test/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtractTest.groovy index ce2567403e..2f6ae87683 100644 --- a/worldpoptools/src/test/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtractTest.groovy +++ b/worldpoptools/src/test/groovy/org/orbisgis/geoclimate/worldpoptools/WorldPopExtractTest.groovy @@ -19,13 +19,13 @@ */ package org.orbisgis.geoclimate.worldpoptools +import ch.qos.logback.classic.Logger import org.h2gis.api.EmptyProgressVisitor import org.h2gis.functions.io.asc.AscReaderDriver import org.junit.jupiter.api.* import org.junit.jupiter.api.io.TempDir import org.orbisgis.data.H2GIS -import org.slf4j.Logger -import org.slf4j.LoggerFactory +import org.orbisgis.geoclimate.utils.LoggerUtils import static org.junit.jupiter.api.Assertions.* @@ -34,7 +34,7 @@ class WorldPopExtractTest { @TempDir static File folder - private static final Logger LOGGER = LoggerFactory.getLogger(WorldPopExtractTest) + private static final Logger LOGGER = LoggerUtils.createLogger(WorldPopExtractTest) private static H2GIS h2GIS @BeforeAll