Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logger #832

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ 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
*/
abstract class BDTopoUtils extends AbstractScript {

BDTopoUtils() {
super(LoggerFactory.getLogger(BDTopoUtils.class))
LoggerUtils.setLoggerLevel("INFO")
super(BDTopoUtils.class)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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("-", "_") }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,9 +85,9 @@ class Geoclimate implements Callable<Integer> {
@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...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = [
Expand Down
4 changes: 4 additions & 0 deletions osm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.orbisgis.geoclimate</groupId>
<artifactId>common-utils</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
6 changes: 2 additions & 4 deletions osm/src/main/groovy/org/orbisgis/geoclimate/osm/OSM.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
*/
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
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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
*/
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
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.*

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.*

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand All @@ -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
Expand Down
Loading