Re: Log4j2 - setting programatically the env variable

2017-08-25 Thread Ralph Goers
Is there a reason you didn’t use one of the Configurator.initialize() methods?

Ralph

> On Aug 25, 2017, at 2:17 AM, Darlea, Radu <radu.dar...@finastra.com> wrote:
> 
> Answering myself:
> The only solution I found by successive tries was to give up using the web 
> app style of integrating log4j2 and do it as in a standalone app.
> 1.   I removed log4j-web jar
> 2.   I removed any listener and servlet and parameter from web.xml 
> regarding log4j2
> 3.   I placed the log4j2.xml in classes (otherwise I found no way to 
> avoid the error message No log4j2 configuration file found)
> 4.   I removed any static initialization of logger in the listeners 
> listed in web.xml
> 5.   I change the code somehow similar to app initialization
>  
> System.setProperty("FTMIDDLE_LOGDIR", fullPathLogDir.toString());
> // start and configure the logger
> LoggerContext context = 
> ((LoggerContext)org.apache.logging.log4j.LogManager.getContext(false));
> // just for debugging
> Configuration log4jConfiguration = context.getConfiguration();
> getLogger().info("Finished configuring log4j. The configuration file is: "
> + context.getConfigLocation()
> + "\nThe configuration details are: \n[appender : "
> + log4jConfiguration.getAppenders().toString() + "]"
> + "\n[loggers : " + log4jConfiguration.getLoggers().toString() + "]");
>  
> This is the only line that matters LoggerContext context = 
> ((LoggerContext)org.apache.logging.log4j.LogManager.getContext(false));
> 
> After it, the loggers get correctly initialized, including in the servlet 
> listeners
> private static Logger logger = null;
> private static Logger getLogger() {
> if (logger == null) {
> logger = LogManager.getLogger(ContextListener.class);
> }
> return logger;
> }
>  
> I am not happy with this approach, but at least appears to be fine 
> functionally.
>  
> Thx, Radu
> Ext.  22912
> Mobile + (40)7405 71909
> Advance notice of absence July 6th, 7th; July 24th to August 4th
>  
> From: Darlea, Radu 
> Sent: Monday, August 21, 2017 10:32 AM
> To: log4j-user@logging.apache.org
> Subject: RE: Log4j2 - setting programatically the env variable
>  
> Hi
>  
> Anyone having a clue, please?
>  
> Thx, Radu
>  
> From: Darlea, Radu 
> Sent: Thursday, August 17, 2017 6:10 PM
> To: log4j-user@logging.apache.org <mailto:log4j-user@logging.apache.org>
> Subject: Log4j2 - setting programatically the env variable
>  
> Hi
>  
> Please find the snippet of web.xml
>  
> 
>  
> http://java.sun.com/xml/ns/javaee 
> <http://java.sun.com/xml/ns/javaee>" 
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance 
> <http://www.w3.org/2001/XMLSchema-instance>"
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
>   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd 
> <http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd>"
> version="3.0">
>  
> ft-middle
> 
> isLog4jAutoInitializationDisabled
> true
> 
>  
> 
> summit.feserver.ContextListener
> 
>  
> 
> 
> MessageHandler
> summit.feserver.MessageHandlerServlet
> 
> packet_size
> 1024
> Specify response packet size, in unit of 
> KB
> 
> 
>  
> 
>  
> Please find the deployed libraries
> juli-6.0.16.jar
> juli-LICENSE.txt
> log4j-1.2-api-2.8.2.jar
> log4j-api-2.8.2.jar
> log4j-core-2.8.2.jar
> log4j-web-2.8.2.jar
> in Tomcat 8.5.20
>  
> Please find the log4j2 configuration saved as log4j2.xml in 
> tomcat\webapps\ft_middle_ws\WEB-INF
> 
> 
>  
> 
>  fileName="${sys:FTMIDDLE_LOGDIR}/FTMiddle.log" 
> filePattern="${sys:FTMIDDLE_LOGDIR}/FTMiddle-%d{-MM-dd}.log">
> 
> 
> [%-5level] %d{-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
> 
> 
> 
> 
> 
> 
>  
> 
>  pattern="[%-5level] %d{-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
> 
> 
>  
>

Re: Log4j2 - setting programatically the env variable

2017-08-21 Thread Remko Popma
Q1. Why is the log4j2 framework starting before the servlet context is
called ?
Not sure. There could be some logging happening in a static initialization
block. This static block is called when its class is loaded, which would
cause Log4j2 to initialize itself.

You can see that Log4j2 tries to find a log4j2.xml (and
.json/.yaml/.properties) file in the classpath and is unable to do so.

This is because the log4j2.xml configuration file is located directly under
the WEB-INF folder, which is not in the classpath. It should be in
WEB-INF/classes instead.


Q2. How come the log4j2 get eventually configured?
In your code you tell Log4j2 the path to the configuration file. Not sure
why this works because I don't see Log4j2 re-configuring from the new
config location...

Q3. What is changing once I add the log4j2 listener before servlet context
listener?
Notice this error:

2017-08-17 17:16:08,359 localhost-startStop-1 ERROR Unable to create file
${sys:FTMIDDLE_LOGDIR}/FTMiddle.log java.io.IOException: The filename,
directory
name, or volume label syntax is incorrect

Could it be that the system property `FTMIDDLE_LOGDIR` does not have a
value? The system properties lookup failed and Log4j2 tries to create a
file named "${sys:FTMIDDLE_LOGDIR..."  This is not a valid file name in
windows, hence the error.










On Mon, Aug 21, 2017 at 4:32 PM, Darlea, Radu <radu.dar...@finastra.com>
wrote:

> Hi
>
>
>
> Anyone having a clue, please?
>
>
>
> Thx, Radu
>
>
>
> *From:* Darlea, Radu
> *Sent:* Thursday, August 17, 2017 6:10 PM
> *To:* log4j-user@logging.apache.org
> *Subject:* Log4j2 - setting programatically the env variable
>
>
>
> Hi
>
>
>
> Please find the snippet of web.xml
>
>
>
> 
>
>
>
> http://java.sun.com/xml/ns/javaee; xmlns:xsi="
> http://www.w3.org/2001/XMLSchema-instance;
>
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>
>   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd;
>
> version="3.0">
>
>
>
> ft-middle
>
> 
>
> isLog4jAutoInitializationDisabled
>
> true
>
> 
>
>
>
> 
>
> summit.feserver.ContextListener
>
> 
>
>
>
> 
>
> 
>
> MessageHandler
>
> summit.feserver.MessageHandlerServlet servlet-class>
>
> 
>
> packet_size
>
> 1024
>
> Specify response packet size, in unit of
> KB
>
> 
>
> 
>
>
>
> 
>
>
>
> Please find the deployed libraries
>
> juli-6.0.16.jar
>
> juli-LICENSE.txt
>
> log4j-1.2-api-2.8.2.jar
>
> log4j-api-2.8.2.jar
>
> log4j-core-2.8.2.jar
>
> log4j-web-2.8.2.jar
>
> in Tomcat 8.5.20
>
>
>
> Please find the log4j2 configuration saved as log4j2.xml in
> tomcat\webapps\ft_middle_ws\WEB-INF
>
> 
>
> 
>
>
>
> 
>
>  fileName="${sys:FTMIDDLE_LOGDIR}/FTMiddle.log"
> filePattern="${sys:FTMIDDLE_LOGDIR}/FTMiddle-%d{-MM-dd}.log">
>
> 
>
>
> [%-5level] %d{-MM-dd HH:mm:ss.SSS} [%t] %c{1} -
> %msg%n
>
> 
>
> 
>
>
> 
>
> 
>
> 
>
>
>
>  target="SYSTEM_OUT">
>
>  pattern="[%-5level] %d{-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
>
> 
>
> 
>
>
>
> 
>
>  level="info" additivity="false">
>
>  ref="FTDailyFileAppender" level="info" />
>
> 
>
>
>
> 
>
>  ref="console" />
>
> 
>
> 
>
>
>
> 
>
>
>
>
>
> Please find the logs
>
>
>
> 16-Aug-2017 18:08:51.272 INFO [localhost-startStop-1]
> org.apache.catalina.startup.HostConfig.deployWAR Deploying web
> application archive [D:\soft\apache-tomcat-8.5.20\
> webapps\ft_middle_ws.war]
>
> 16-Aug-2017 18:08:53.202 INFO [localhost-startStop-1]
> org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was
> scanned for TLDs yet contained no