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

Add the possibility to custom the logger #793

Merged
merged 4 commits into from
Jan 5, 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
4 changes: 2 additions & 2 deletions bdtopo_v2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package org.orbisgis.geoclimate.bdtopo_v2

import ch.qos.logback.classic.Logger
import org.locationtech.jts.geom.Envelope
import org.locationtech.jts.geom.Geometry
import org.orbisgis.geoclimate.geoindicators.WorkflowUtilities
import org.orbisgis.process.GroovyProcessFactory
import org.slf4j.LoggerFactory


/**
* Class to manage and access to the BDTOPO processes
*
*/
abstract class BDTopo_V2 extends GroovyProcessFactory {

public static def logger = LoggerFactory.getLogger(BDTopo_V2.class)
public static Logger logger

BDTopo_V2(){
logger = LoggerFactory.getLogger(BDTopo_V2.class)
WorkflowUtilities.setLoggerLevel("INFO")
}

public static WorkflowBDTopo_V2 = new WorkflowBDTopo_V2()
public static InputDataLoading = new InputDataLoading()
Expand Down
6 changes: 4 additions & 2 deletions geoclimate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@
<groupId>org.orbisgis.geoclimate</groupId>
<artifactId>osmtools</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.orbisgis.geoclimate

import ch.qos.logback.classic.Logger
import org.slf4j.LoggerFactory
import picocli.CommandLine

import java.util.concurrent.Callable
Expand Down Expand Up @@ -41,18 +43,18 @@ class Geoclimate implements Callable<Integer> {
description = "The configuration file used to set up the workflow")
String configFile

@CommandLine.Option(names = ["verbose"],
@CommandLine.Option(names = ["-l"],
required = false,
description = "Use it to activate the verbose")
boolean verbose
description = "Use it to manage the log level. Allowed values are : INFO, DEBUG, TRACE, OFF\"\n ")
String verbose


@Override
Integer call() {
if (verbose) {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "INFO")
Geoindicators.WorkflowUtilities.setLoggerLevel(verbose.trim())
} else {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "OFF")
Geoindicators.WorkflowUtilities.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 @@ -35,7 +35,9 @@ class GeoclimateTest {
assert 2 == exitCode
exitCode = cmd.execute("-w osm", "-f /tmp/conf.json")
assert 1 == exitCode
exitCode = cmd.execute("-w osm", "-f /tmp/conf.json", "verbose")
exitCode = cmd.execute("-w osm", "-f /tmp/conf.json", "-l")
assert 2 == exitCode
exitCode = cmd.execute("-w osm", "-f /tmp/conf.json", "-l debug")
assert 1 == exitCode
}

Expand Down Expand Up @@ -78,7 +80,7 @@ class GeoclimateTest {
def cmd = new CommandLine(app)
def sw = new StringWriter()
cmd.setOut(new PrintWriter(sw))
def exitCode = cmd.execute("-w osm", "-f $configFile")
def exitCode = cmd.execute("-w osm", "-f $configFile", "-l info")
assert 0 == exitCode
}

Expand Down
4 changes: 2 additions & 2 deletions geoindicators/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.orbisgis.geoclimate

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.LoggerContext
import org.orbisgis.geoclimate.geoindicators.BlockIndicators
import org.orbisgis.geoclimate.geoindicators.BuildingIndicators
import org.orbisgis.geoclimate.geoindicators.DataUtils
Expand All @@ -17,7 +19,13 @@ import org.orbisgis.process.GroovyProcessFactory
import org.slf4j.LoggerFactory

abstract class Geoindicators extends GroovyProcessFactory {
public static def logger = LoggerFactory.getLogger(Geoindicators.class)
public static def logger

Geoindicators(){
logger = LoggerFactory.getLogger(Geoindicators.class)
var context = (LoggerContext) LoggerFactory.getILoggerFactory()
context.getLogger(Geoindicators.class).setLevel(Level.INFO)
}

//Processes
public static BuildingIndicators = new BuildingIndicators()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package org.orbisgis.geoclimate.geoindicators

import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.Level
import groovy.json.JsonSlurper
import groovy.transform.BaseScript
import org.h2gis.functions.io.utility.PRJUtil
import org.orbisgis.data.H2GIS
import org.orbisgis.data.POSTGIS
import org.orbisgis.geoclimate.Geoindicators
import org.slf4j.LoggerFactory

import java.util.logging.FileHandler
import java.util.logging.Handler
import java.util.logging.LogManager

@BaseScript Geoindicators geoindicators

Expand All @@ -14,9 +22,9 @@ import org.orbisgis.geoclimate.Geoindicators
* - create a connection to a specified output DataBase
* - check if specified output table names to save are allowed
*
* @param outputDBProperties properties to define the db connection
* @param outputTables list of tables to save
* @parm allowedOutputTables list of supported tables
* @param outputDBProperties properties to define the db connection
* @param outputTables list of tables to save
* @parm allowedOutputTables list of supported tables
*
* @return a map with the allowed tables (tables) and a connection to the database (datasource)
*
Expand Down Expand Up @@ -77,8 +85,8 @@ def createDatasource(def database_properties) {
/**
* Method to check the output tables that can be saved
*
* @param outputFolder properties to store in a folder (path and table names)
* @parm allowedOutputTables list of supported tables
* @param outputFolder properties to store in a folder (path and table names)
* @parm allowedOutputTables list of supported tables
*
* @return a map with the allowed tables (tables) and a connection to the database (datasource)
*
Expand All @@ -104,7 +112,7 @@ def buildOutputFolderParameters(def outputFolder, def allowedOutputTables) {
* @param jsonFile
* @return
*/
Map readJSON(def jsonFile) {
Map readJSON(def jsonFile) {
def jsonSlurper = new JsonSlurper()
if (jsonFile) {
return jsonSlurper.parse(jsonFile)
Expand Down Expand Up @@ -157,7 +165,7 @@ def saveToAscGrid(def outputTable, def subFolder, def filePrefix, def h2gis_data

//Save each grid
columnNames.each { it ->
def outputFile = new File(subFolder + File.separator + filePrefix + "_"+it.toLowerCase()+".asc")
def outputFile = new File(subFolder + File.separator + filePrefix + "_" + it.toLowerCase() + ".asc")
if (deleteOutputData) {
outputFile.delete()
}
Expand All @@ -176,7 +184,7 @@ def saveToAscGrid(def outputTable, def subFolder, def filePrefix, def h2gis_data
}
//Save the PRJ file
if (outputSRID >= 0) {
File outPrjFile = new File(subFolder + File.separator + filePrefix + "_"+it.toLowerCase()+".prj")
File outPrjFile = new File(subFolder + File.separator + filePrefix + "_" + it.toLowerCase() + ".prj")
PRJUtil.writePRJ(h2gis_datasource.getConnection(), outputSRID, outPrjFile)
}
info "$outputTable has been saved in $outputFile"
Expand Down Expand Up @@ -223,22 +231,24 @@ def saveToCSV(def outputTable, def filePath, def h2gis_datasource, def deleteOut
}

/**
* Utility class to change log level
* Utility class to change log level for all loggers
*
*/
def applyVerbose(String verboseOption) {
if(verboseOption){
if (verboseOption.equalsIgnoreCase("INFO")) {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "INFO")
}
else if (verboseOption.equalsIgnoreCase("DEBUG")) {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG")
}
else if (verboseOption.equalsIgnoreCase("TRACE")) {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG")
}
else if (verboseOption.equalsIgnoreCase("OFF")) {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "OFF")
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 @@ -83,16 +83,16 @@ class BuildingIndicatorsTests {
"raw_compactness", "perimeter_convexity"],
prefixName : "test",datasource:h2GIS])
def concat = ["", "", "", ""]
h2GIS.eachRow("SELECT * FROM test_building_form_properties WHERE id_build = 1 OR id_build = 7 ORDER BY id_build ASC"){
h2GIS.eachRow("SELECT * FROM test_building_form_properties WHERE id_build = 1 OR id_build = 7 ORDER BY id_build ASC".toString()){
row ->
concat[0]+= "${row.area_concavity}\n"
concat[1]+= "${row.form_factor.round(5)}\n"
}
h2GIS.eachRow("SELECT * FROM test_building_form_properties WHERE id_build = 2 ORDER BY id_build ASC"){
h2GIS.eachRow("SELECT * FROM test_building_form_properties WHERE id_build = 2 ORDER BY id_build ASC".toString()){
row -> concat[2]+= "${row.raw_compactness.round(3)}\n"
}
h2GIS.eachRow("SELECT * FROM test_building_form_properties WHERE id_build = 1 OR id_build = 7 OR " +
"id_build = 30 ORDER BY id_build ASC"){
"id_build = 30 ORDER BY id_build ASC".toString()){
row -> concat[3]+= "${row.perimeter_convexity.round(5)}\n"
}
assertEquals("1.0\n0.94\n".toString(),concat[0].toString())
Expand All @@ -112,7 +112,7 @@ class BuildingIndicatorsTests {
datasource:h2GIS])
def concat = ""
h2GIS.eachRow("SELECT * FROM test_building_minimum_building_spacing WHERE id_build = 2 OR id_build = 4 " +
"OR id_build = 6 ORDER BY id_build ASC"){
"OR id_build = 6 ORDER BY id_build ASC".toString()){
row -> concat+= "${row.minimum_building_spacing}\n"
}
assertEquals("2.0\n0.0\n7.0\n", concat)
Expand Down Expand Up @@ -200,7 +200,7 @@ class BuildingIndicatorsTests {
IProcess process = Geoindicators.BuildingIndicators.buildingPopulation()
assertTrue process.execute([inputBuilding: "building", inputPopulation: "population_grid",
inputPopulationColumns :["pop"],datasource: h2GIS])
def rows = h2GIS.rows("select pop from ${process.results.buildingTableName} order by id_build")
def rows = h2GIS.rows("select pop from ${process.results.buildingTableName} order by id_build".toString())
assertEquals(5f, (float)rows[0].pop)
assertEquals(5f, (float)rows[1].pop)
}
Expand All @@ -216,7 +216,7 @@ class BuildingIndicatorsTests {
IProcess process = Geoindicators.BuildingIndicators.buildingPopulation()
assertTrue process.execute([inputBuilding: "building", inputPopulation: "population_grid",
inputPopulationColumns :["pop"],datasource: h2GIS])
def rows = h2GIS.rows("select pop from ${process.results.buildingTableName} order by id_build")
def rows = h2GIS.rows("select pop from ${process.results.buildingTableName} order by id_build".toString())
assertEquals(3.33f, (float)rows[0].pop, 0.01)
assertEquals(6.666f, (float)rows[1].pop, 0.01)
}
Expand All @@ -234,7 +234,7 @@ class BuildingIndicatorsTests {
IProcess process = Geoindicators.BuildingIndicators.buildingPopulation()
assertTrue process.execute([inputBuilding: "building", inputPopulation: "population_grid",
inputPopulationColumns :["pop"], datasource: h2GIS])
def rows = h2GIS.rows("select pop, id_build from ${process.results.buildingTableName} order by id_build")
def rows = h2GIS.rows("select pop, id_build from ${process.results.buildingTableName} order by id_build".toString())
assertEquals(13.33f, (float)rows[0].pop, 0.01)
assertEquals(6.666f, (float)rows[1].pop, 0.01)
}
Expand All @@ -252,7 +252,7 @@ class BuildingIndicatorsTests {
IProcess process = Geoindicators.BuildingIndicators.buildingPopulation()
assertTrue process.execute([inputBuilding: "building", inputPopulation: "population_grid",
inputPopulationColumns :["pop"],datasource: h2GIS])
def rows = h2GIS.rows("select pop, id_build from ${process.results.buildingTableName} order by id_build")
def rows = h2GIS.rows("select pop, id_build from ${process.results.buildingTableName} order by id_build".toString())
assertEquals(15f, (float)rows[0].pop, 0.01)
assertEquals(5f, (float)rows[1].pop, 0.01)
}
Expand All @@ -270,7 +270,7 @@ class BuildingIndicatorsTests {
IProcess process = Geoindicators.BuildingIndicators.buildingPopulation()
assertTrue process.execute([inputBuilding: "building", inputPopulation: "population_grid",
inputPopulationColumns :["pop"],datasource: h2GIS])
def rows = h2GIS.rows("select pop, id_build from ${process.results.buildingTableName} order by id_build")
def rows = h2GIS.rows("select pop, id_build from ${process.results.buildingTableName} order by id_build".toString())
assertEquals(12f, (float)rows[0].pop, 0.01)
assertEquals(8f, (float)rows[1].pop, 0.01)
}
Expand Down
4 changes: 2 additions & 2 deletions osm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
Expand Down
16 changes: 10 additions & 6 deletions osm/src/main/groovy/org/orbisgis/geoclimate/osm/OSM.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package org.orbisgis.geoclimate.osm

import org.locationtech.jts.geom.Envelope
import org.locationtech.jts.geom.Geometry
import org.orbisgis.geoclimate.Geoindicators
import org.orbisgis.geoclimate.geoindicators.WorkflowUtilities
import org.orbisgis.geoclimate.osmtools.utils.Utilities
import org.orbisgis.process.GroovyProcessFactory
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
Expand All @@ -14,7 +13,12 @@ import org.slf4j.LoggerFactory
*/
abstract class OSM extends GroovyProcessFactory {

public static Logger logger = LoggerFactory.getLogger(OSM.class)
public static def logger

OSM() {
logger = LoggerFactory.getLogger(OSM.class)
WorkflowUtilities.setLoggerLevel("INFO")
}

public static WorkflowOSM = new WorkflowOSM()
public static InputDataLoading = new InputDataLoading()
Expand Down Expand Up @@ -64,7 +68,7 @@ abstract class OSM extends GroovyProcessFactory {
* @return
*/
static List bbox(Geometry geometry) {
if(geometry){
if (geometry) {
Envelope env = geometry.getEnvelopeInternal()
return [env.getMinY(), env.getMinX(), env.getMaxY(), env.getMaxX()]
}
Expand All @@ -74,11 +78,11 @@ abstract class OSM extends GroovyProcessFactory {
* Utility method to create bbox represented by a list of values :
* [minY, minX, maxY, maxX]
* from an envelope
* @param env JTS envelope
* @param env JTS envelope
* @return
*/
static List bbox(Envelope env) {
if(env){
if (env) {
return [env.getMinY(), env.getMinX(), env.getMaxY(), env.getMaxX()]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class InputDataLoadingTest {
@TempDir
static File folder

private static final Logger logger = LoggerFactory.getLogger(InputDataLoadingTest.class)

static H2GIS h2GIS

@BeforeAll
Expand Down
Loading