Skip to content

Commit 29284be

Browse files
committed
fix logging
- Now even with default log4j2.yml that comes with engine dist console appender with info threshold now attached. - appears to now properly create nuix.log file for main process
1 parent f91e2a6 commit 29284be

2 files changed

Lines changed: 49 additions & 17 deletions

File tree

IntelliJ/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = "com.nuix.innovation"
7-
version = "Nuix9.10-v1.1.2"
7+
version = "Nuix9.10-v1.1.3"
88

99
val sourceCompatibility = 11
1010
val targetCompatibility = 11

IntelliJ/src/main/java/com/nuix/innovation/enginewrapper/NuixEngine.java

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
import nuix.engine.Engine;
88
import nuix.engine.GlobalContainer;
99
import nuix.engine.GlobalContainerFactory;
10-
import org.apache.logging.log4j.core.LifeCycle;
11-
import org.jetbrains.annotations.Nullable;
12-
import org.joda.time.DateTime;
13-
import org.apache.logging.log4j.Level;
10+
import org.apache.log4j.Level;
1411
import org.apache.logging.log4j.LogManager;
1512
import org.apache.logging.log4j.Logger;
13+
import org.apache.logging.log4j.core.LifeCycle;
14+
import org.apache.logging.log4j.core.LogEvent;
1615
import org.apache.logging.log4j.core.LoggerContext;
16+
import org.apache.logging.log4j.core.appender.ConsoleAppender;
1717
import org.apache.logging.log4j.core.config.Configuration;
18-
import org.apache.logging.log4j.core.config.LoggerConfig;
18+
import org.apache.logging.log4j.core.filter.AbstractFilter;
19+
import org.apache.logging.log4j.core.layout.PatternLayout;
20+
import org.jetbrains.annotations.Nullable;
21+
import org.joda.time.DateTime;
1922
import org.jruby.embed.internal.BiVariableMap;
2023

2124
import java.io.File;
@@ -490,19 +493,47 @@ private void checkPreConditions() throws Exception {
490493
*/
491494
protected void initializeLogging() {
492495
if (log == null) {
496+
// Default log4j2.yaml included with engine distribution references these system properties
497+
// so we want them in place before we ask log4j2 to reload configuration
498+
System.setProperty("nuix.loglevel", "info");
493499
System.setProperty("nuix.logdir", logDirectorySupplier.get().getAbsolutePath());
494-
// Use Log4j2 config YAML from engine base directory
500+
501+
// Use Log4j2 config YAML from engine distribution directory
495502
File log4jConfigFile = new File(engineDistributionDirectorySupplier.get(), "config/log4j2.yml");
496-
System.setProperty("log4j.configurationFile", log4jConfigFile.getAbsolutePath());
503+
System.setProperty("log4j.configurationFile", log4jConfigFile.toURI().toString());
504+
505+
// Report settings we're using
506+
System.out.println("log4j.configurationFile => " + System.getProperty("log4j.configurationFile"));
507+
System.out.println("nuix.loglevel => " + System.getProperty("nuix.loglevel"));
508+
System.out.println("nuix.logdir => " + System.getProperty("nuix.logdir"));
509+
510+
// Log4j2 has likely already attempted to configure itself at this point but configuration
511+
// above likely was not already in place at that moment. Now that we have configured those values
512+
// we ask log4j2 to reconfigure itself. It should then find the "log4j.configurationFile" property
513+
// and configure itself from that.
514+
System.out.println("Asking log4j2 to reload configuration...");
515+
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
516+
context.reconfigure();
517+
518+
// Default log4j2.yml file only appends to console when logged event is fatal, also it logs that to
519+
// SYSTEM_ERR rather than SYSTEM_OUT. For testing it can be helpful to have INFO events written to the
520+
// console, so we will add our own appender with these traits.
521+
ConsoleAppender consoleAppender = ConsoleAppender.newBuilder()
522+
.setName("Nuix_Engine_Console_Appender")
523+
.setFilter(new AbstractFilter() {
524+
@Override
525+
public Result filter(LogEvent event) {
526+
return Result.NEUTRAL;
527+
}
528+
})
529+
.setLayout(PatternLayout.newBuilder().withPattern("%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %r %-5p %c - %m%n").build())
530+
.setConfiguration(context.getConfiguration()).build();
531+
consoleAppender.start();
532+
context.getConfiguration().addAppender(consoleAppender);
533+
context.getRootLogger().addAppender(context.getConfiguration().getAppender(consoleAppender.getName()));
534+
context.updateLoggers();
535+
497536
log = LogManager.getLogger(this.getClass());
498-
log.info("log4j.configurationFile => " + log4jConfigFile.getAbsolutePath());
499-
500-
// Set default level to INFO
501-
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
502-
Configuration config = ctx.getConfiguration();
503-
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
504-
loggerConfig.setLevel(Level.INFO);
505-
ctx.updateLoggers();
506537
}
507538
}
508539

@@ -522,9 +553,10 @@ protected void ensureGlobalContainer() {
522553
* period has elapsed for the license.
523554
*/
524555
protected void buildEngine() {
556+
System.setProperty("nuix.userDataBase", userDataDirectorySupplier.get().getAbsolutePath());
525557
Map<Object, Object> engineConfiguration = Map.of(
526558
"user", System.getProperty("user.name"),
527-
"userDataDirs", userDataDirectorySupplier.get()
559+
"userDataDirs", userDataDirectorySupplier.get().getAbsolutePath()
528560
);
529561

530562
engine = globalContainer.newEngine(engineConfiguration);

0 commit comments

Comments
 (0)