Skip to content

Commit

Permalink
Merge pull request #366 from metafacture/319-log
Browse files Browse the repository at this point in the history
Add log function
  • Loading branch information
TobiasNx authored Oct 9, 2024
2 parents 019ad48 + ad73aa2 commit 81a88bc
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ include("<path>"[, <dynamicLocalVariables>...])

[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("<logMessage>"[, level: "<logLevel>"])
```

[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.
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
Expand Down
1 change: 1 addition & 0 deletions metafix/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
33 changes: 33 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +65,34 @@ public void apply(final Metafix metafix, final Record record, final List<String>
metafix.getRecordTransformer(includePath).transform(record, options);
}
},
log {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
// does not support Catmandu log level option FATAL

final String level = options.getOrDefault("level", "INFO");
final Consumer<String> 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<String> params, final Map<String, String> options) {
Expand Down Expand Up @@ -691,4 +722,6 @@ public void apply(final Metafix metafix, final Record record, final List<String>

private static final Random RANDOM = new Random();

private static final Logger LOG = LoggerFactory.getLogger(FixMethod.class);

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

Expand Down Expand Up @@ -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 -> { });
Expand Down

0 comments on commit 81a88bc

Please sign in to comment.