Skip to content

Commit b9c428b

Browse files
maarztctrueden
authored andcommitted
LoggerPreprocessor: name Logger by label attribute
If label is specified in the @parameter annotation, the logger is given a name matching the provided label. Signed-off-by: Curtis Rueden <ctrueden@wisc.edu>
1 parent aedc3ca commit b9c428b

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/main/java/org/scijava/module/process/LoggerPreprocessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ public void process(final Module module) {
6565
Logger.class);
6666
if (loggerInput == null || !loggerInput.isAutoFill()) return;
6767

68+
String loggerName = loggerInput.getLabel();
69+
if(loggerName.isEmpty())
70+
loggerName = module.getDelegateObject().getClass().getSimpleName();
71+
Logger logger = logService.subLogger(loggerName);
72+
6873
final String name = loggerInput.getName();
69-
module.setInput(name, logService.subLogger(module.getDelegateObject()
70-
.getClass().getSimpleName()));
74+
module.setInput(name, logger);
7175
module.resolveInput(name);
7276
}
7377

src/test/java/org/scijava/module/process/LoggerPreprocessorTest.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
import org.junit.Test;
3939
import org.scijava.Context;
4040
import org.scijava.command.Command;
41-
import org.scijava.command.CommandModule;
4241
import org.scijava.command.CommandService;
4342
import org.scijava.log.LogService;
4443
import org.scijava.log.Logger;
4544
import org.scijava.log.TestLogListener;
4645
import org.scijava.plugin.Parameter;
47-
import org.scijava.plugin.Plugin;
46+
47+
import static org.junit.Assert.assertEquals;
4848

4949
/**
5050
* Tests {@link LoggerPreprocessor}.
@@ -57,16 +57,13 @@ public class LoggerPreprocessorTest {
5757
public void testInjection() throws InterruptedException, ExecutionException {
5858
final Context context = new Context(CommandService.class);
5959
final CommandService commandService = context.service(CommandService.class);
60-
final LogService logService = context.service(LogService.class);
6160
final TestLogListener listener = new TestLogListener();
62-
logService.addListener(listener);
61+
context.service(LogService.class).addListener(listener);
6362

64-
final CommandModule module = //
65-
commandService.run(CommandWithLogger.class, true).get();
63+
commandService.run(CommandWithLogger.class, true).get();
6664
assertTrue(listener.hasLogged(m -> m.source().path().contains(CommandWithLogger.class.getSimpleName())));
6765
}
6866

69-
@Plugin(type = Command.class)
7067
public static class CommandWithLogger implements Command {
7168

7269
@Parameter
@@ -78,4 +75,21 @@ public void run() {
7875
}
7976
}
8077

78+
@Test
79+
public void testLoggerNameByAnnotation() throws ExecutionException, InterruptedException {
80+
final Context context = new Context(CommandService.class);
81+
final CommandService commandService = context.service(CommandService.class);
82+
commandService.run(CommandWithNamedLogger.class, true).get();
83+
}
84+
85+
public static class CommandWithNamedLogger implements Command {
86+
87+
@Parameter(label = "MyLoggerName")
88+
public Logger log;
89+
90+
@Override
91+
public void run() {
92+
assertEquals("MyLoggerName", log.getName());
93+
}
94+
}
8195
}

0 commit comments

Comments
 (0)