From f1405eaa2240dac6b4459055a15eed69571adbbb Mon Sep 17 00:00:00 2001 From: TobiasNx Date: Wed, 18 Sep 2024 11:00:03 +0200 Subject: [PATCH 1/4] Add log function #319 --- .../org/metafacture/metafix/FixMethod.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java index 4ae2ac32..8eb9f4f0 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java @@ -43,6 +43,9 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public enum FixMethod implements FixFunction { // checkstyle-disable-line ClassDataAbstractionCoupling|ClassFanOutComplexity // SCRIPT-LEVEL METHODS: @@ -62,6 +65,23 @@ public void apply(final Metafix metafix, final Record record, final List metafix.getRecordTransformer(includePath).transform(record, options); } }, + log { + @Override + public void apply(final Metafix metafix, final Record record, final List params, final Map options) { + final String logMessage = params.get(0); + final String logLevel = options.get("level"); // does not support Catmandu lop level option FATAL + + if (logLevel.equals("WARN")) { + LOG.warn(logMessage); + } + else if (logLevel.equals("ERROR")) { + LOG.error(logMessage); + } + else { + LOG.info(logMessage); + } + } + }, nothing { @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { @@ -691,4 +711,6 @@ public void apply(final Metafix metafix, final Record record, final List private static final Random RANDOM = new Random(); + private static final Logger LOG = LoggerFactory.getLogger(FixMethod.class); + } From 5d15e0ecbb5efa4676388e494b219841fc702c8d Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Tue, 24 Sep 2024 17:38:45 +0200 Subject: [PATCH 2/4] Allow more log levels in `log()` Fix function. (#366) * Disallow unsupported levels. * Add tests. --- build.gradle | 1 + metafix/build.gradle | 1 + .../org/metafacture/metafix/FixMethod.java | 39 ++++++++++++------- .../metafix/MetafixScriptTest.java | 27 +++++++++++++ 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index be1d02c5..566eb558 100644 --- a/build.gradle +++ b/build.gradle @@ -49,6 +49,7 @@ subprojects { 'mockito': '2.27.0', 'requirejs': '2.3.6', 'slf4j': '1.7.21', + 'slf4j_mock': '2.3.0', 'wiremock': '2.33.2', 'xtext': '2.26.0' ] diff --git a/metafix/build.gradle b/metafix/build.gradle index e2e403e2..7aed709c 100644 --- a/metafix/build.gradle +++ b/metafix/build.gradle @@ -25,6 +25,7 @@ dependencies { testImplementation "org.eclipse.xtext:org.eclipse.xtext.xbase.testing:${versions.xtext}" testImplementation "org.junit.jupiter:junit-jupiter-api:${versions.junit_jupiter}" testImplementation "org.junit.platform:junit-platform-launcher:${versions.junit_platform}" + testImplementation "org.simplify4u:slf4j2-mock:${versions.slf4j_mock}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${versions.junit_jupiter}" diff --git a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java index 8eb9f4f0..aef080a0 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java @@ -24,6 +24,9 @@ import org.metafacture.metamorph.functions.URLEncode; import org.metafacture.metamorph.maps.FileMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.IOException; import java.util.Arrays; @@ -43,9 +46,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - public enum FixMethod implements FixFunction { // checkstyle-disable-line ClassDataAbstractionCoupling|ClassFanOutComplexity // SCRIPT-LEVEL METHODS: @@ -68,18 +68,29 @@ public void apply(final Metafix metafix, final Record record, final List log { @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { - final String logMessage = params.get(0); - final String logLevel = options.get("level"); // does not support Catmandu lop level option FATAL - - if (logLevel.equals("WARN")) { - LOG.warn(logMessage); - } - else if (logLevel.equals("ERROR")) { - LOG.error(logMessage); - } - else { - LOG.info(logMessage); + // does not support Catmandu log level option FATAL + + final String level = options.getOrDefault("level", "INFO"); + final Consumer consumer; + + switch (level) { + case "DEBUG": + consumer = LOG::debug; + break; + case "ERROR": + consumer = LOG::error; + break; + case "INFO": + consumer = LOG::info; + break; + case "WARN": + consumer = LOG::warn; + break; + default: + throw new IllegalArgumentException("Unsupported log level: " + level); } + + consumer.accept(params.get(0)); } }, nothing { diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java index 67f3d448..2141ed05 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java @@ -26,6 +26,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; import java.io.IOException; import java.util.Arrays; @@ -45,6 +46,9 @@ public class MetafixScriptTest { private static final String CSV_MAP = "src/test/resources/org/metafacture/metafix/maps/test.csv"; private static final String TSV_MAP = "src/test/resources/org/metafacture/metafix/maps/test.tsv"; + @Mock(name = "org.metafacture.metafix.FixMethod") + private Logger fixMethodLogger; + @Mock private StreamReceiver streamReceiver; @@ -193,6 +197,29 @@ public void shouldPutExternalFileMapWithNameAndOptions() { assertMap("put_filemap('" + TSV_MAP + "', '" + MAP_NAME + "', sep_char: '\t')", MAP_NAME); } + @Test + public void shouldLog() { + assertFix("log('test')", f -> { + Mockito.verify(fixMethodLogger).info("test"); + Mockito.verifyNoMoreInteractions(fixMethodLogger); + }); + } + + @Test + public void shouldLogWithLevel() { + assertFix("log('test', level: 'DEBUG')", f -> { + Mockito.verify(fixMethodLogger).debug("test"); + Mockito.verifyNoMoreInteractions(fixMethodLogger); + }); + } + + @Test + public void shouldNotLogWithUnsupportedLevel() { + MetafixTestHelpers.assertProcessException(IllegalArgumentException.class, "Unsupported log level: FATAL", () -> + assertFix("log('test', level: 'FATAL')", f -> { }) + ); + } + @Test public void shouldDoNothing() { assertFix("nothing()", f -> { }); From d1ed262bb2bc63f6911e372e98db38e4b039e774 Mon Sep 17 00:00:00 2001 From: TobiasNx Date: Wed, 25 Sep 2024 10:58:25 +0200 Subject: [PATCH 3/4] Add documentation for log #319 --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 287a9602..2d3e3424 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,21 @@ include(""[, ...]) [Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+include+{") +##### `log` +Send a message to the logs. + +Parameters: + +- `logMessage` specifies the message that should be logged. + +Options: + +- `level` sets one of the following log levels: INFO, DEBUG, ERROR, WARN (Default: INFO). + +```perl +log(“”,level:DEBUG) +``` + ##### `nothing` Does nothing. It is used for benchmarking in Catmandu. From ad73aa20f5c1599b7e6e2eab92eaf12f630ab3f5 Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Wed, 2 Oct 2024 15:24:11 +0200 Subject: [PATCH 4/4] Update documentation for `log()` Fix function. (#366) --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2d3e3424..4cb5b10b 100644 --- a/README.md +++ b/README.md @@ -169,20 +169,23 @@ include(""[, ...]) [Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+include+{") ##### `log` -Send a message to the logs. + +Sends a message to the logs. Parameters: -- `logMessage` specifies the message that should be logged. +- `logMessage` (required): Message to log. Options: -- `level` sets one of the following log levels: INFO, DEBUG, ERROR, WARN (Default: INFO). +- `level`: Log level to log at (one of `DEBUG`, `INFO`, `WARN` or `ERROR`). (Default: `INFO`) ```perl -log(“”,level:DEBUG) +log(""[, level: ""]) ``` +[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+log+{") + ##### `nothing` Does nothing. It is used for benchmarking in Catmandu.