diff --git a/README.md b/README.md index 287a9602..4cb5b10b 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,24 @@ include(""[, ...]) [Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+include+{") +##### `log` + +Sends a message to the logs. + +Parameters: + +- `logMessage` (required): Message to log. + +Options: + +- `level`: Log level to log at (one of `DEBUG`, `INFO`, `WARN` or `ERROR`). (Default: `INFO`) + +```perl +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. 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 4ae2ac32..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; @@ -62,6 +65,34 @@ 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) { + // 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 { @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { @@ -691,4 +722,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); + } 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 -> { });