77import nuix .engine .Engine ;
88import nuix .engine .GlobalContainer ;
99import 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 ;
1411import org .apache .logging .log4j .LogManager ;
1512import org .apache .logging .log4j .Logger ;
13+ import org .apache .logging .log4j .core .LifeCycle ;
14+ import org .apache .logging .log4j .core .LogEvent ;
1615import org .apache .logging .log4j .core .LoggerContext ;
16+ import org .apache .logging .log4j .core .appender .ConsoleAppender ;
1717import 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 ;
1922import org .jruby .embed .internal .BiVariableMap ;
2023
2124import 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