Details
-
Question
-
Status: Open
-
Major
-
Resolution: Unresolved
-
None
-
None
-
None
-
None
Description
I wanna add other configuration for loggers. For example, we have:
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration shutdownHook="disable" monitorInterval="30"> <Appenders> <File name="MyFile" fileName="all.log" immediateFlush="true" append="false"> <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </File> </Appenders> <Loggers> <Root level="ALL" additivity="false"> <AppenderRef ref="MyFile" /> </Root> </Loggers> </Configuration>
And we have the folder where we can add some XML, for example:
Console.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration shutdownHook="disable"> <Appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="[%highlight{%-5level}] %d{DEFAULT} %c{1}.%M() - %msg%n%throwable{short.lineNumber}" /> </Console> </Appenders> <Loggers> <Root level="ALL" additivity="false"> <AppenderRef ref="console" /> </Root> </Loggers> </Configuration>
I wanna understanding how apply this config to loggers. I have WatchService and the code, which doesn't work:
private void AddConfiguration(String fileName) { logger.info("Adding log4j configuration settings"); uris.add(new File(pathToFolder + fileName).toURI()); ApplyChanges(); } private void ApplyChanges() { logger.info("Applying log4j configuration settings"); LoggerContext ctx = (LoggerContext) LogManager.getContext(false); initialize = Configurator.initialize(null, null, uris, null); ctx.updateLoggers(); }
I can't understand how should I use Configurator.initialize. Please, help me.