You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Thad Humphries <th...@gmail.com> on 2015/09/22 19:26:34 UTC

Changing log4jConfiguration context-param with Tomcat 8

I am adding Log4j 2 to an existing webapp. I am required to allow users to
put the logger's configuration file wherever they wish, and to write their
log files wherever and however they wish.

I have used Log4j 1 in other webapps. There I alter the logging properties
in a Listener by calling PropertyConfigurator.configure() on whatever file
was named in the webapp's context as "log4j-init-file" (in Tomcat, the
webapp's META-INF/context.xml file). I am using Log4j 2.3 and Tomcat 8.0.22.

Log4j 2 does not allow configuring with PropertyConfigurator. From the
documentation, I had thought that if I set
org.apache.logging.log4j.web.Log4jServletContextListener as the first
<listener> in the web.xml, and added the <filter>, <filter-mapping>, and
<context-params> described in
https://logging.apache.org/log4j/2.0/manual/webapp.html for a Servlet 2.5
app, I could then override the log4jConfiguration
in META-INF/context.xml with

<Parameter
    description="Log4j 2.x configuration file."
    name="log4jConfiguration"
    value="file:///Users/thad/conf/myweblog.xml"/>


This is not working for me. The app is still using the values set by the
log4jConfiguration in web.xml. I have tried using a file path and a file://
URI. I am starting Tomcat and own myweblog.xml so permissions should not be
the problem (and the Log4j 1.x properties files can be read by their apps).

What am I missing or misunderstanding? How can I accomplish what I could do
in Log4j 1.x?

-- 
"Hell hath no limits, nor is circumscrib'd In one self-place; but where we
are is hell, And where hell is, there must we ever be" --Christopher
Marlowe, *Doctor Faustus* (v. 121-24)

Re: Changing log4jConfiguration context-param with Tomcat 8

Posted by Gary Gregory <ga...@gmail.com>.
Wow, you solution deserves a write up of it's own!

Gary

On Wed, Sep 23, 2015 at 2:29 PM, Thad Humphries <th...@gmail.com>
wrote:

> I figured it out, thanks in large part to this blog post by Steffen
> Schäfer:
> https://blog.oio.de/2015/04/27/log4j-2-configuration-depending-environment/
>
> Basically I
>
> (1) Create a fallback logger configuration file in WEB-INF.
> (2) Create a listener that checks the log4jConfiguration context parameter,
> and--borrowing from Steffen's example--sets the
> Log4jWebSupport.LOG4J_CONFIG_LOCATION. I declare this listener in web.xml
> immediately after the
> org.apache.logging.log4j.web.Log4jServletContextListener. If
> log4jConfiguration is not present, my listener loads the fallback.
> (3) Do *not* declare the log4jConfiguration context parameter in web.xml,
> but only in the context.xml file.
>
> True, if the user does not deploy an application context or edit the
> fallback configuration file, they may not get a log because the log file
> location may not exist. That's fine, so long as these things can be done,
> and the logger configuration file and the logs can be written outside of
> the servlet container.
>
> On Tue, Sep 22, 2015 at 1:53 PM, Thad Humphries <th...@gmail.com>
> wrote:
>
> > This is a Mac. There is no drive letter. One of the reasons users
> > (actually, administrators) need to modify the logger configuration is
> that
> > this and other webapps are deployed at many sites on a wide range of
> > operating systems and configurations. What is a valid path on a Windows
> box
> > is not a valid path on Mac, Solaris, Linux, etc.
> >
> > On Tue, Sep 22, 2015 at 1:33 PM, Gary Gregory <ga...@gmail.com>
> > wrote:
> >
> >> Does adding the driver letter to the URI make a difference?
> >>
> >> Gary
> >>
> >> On Tue, Sep 22, 2015 at 10:26 AM, Thad Humphries <
> >> thad.humphries@gmail.com>
> >> wrote:
> >>
> >> > I am adding Log4j 2 to an existing webapp. I am required to allow
> users
> >> to
> >> > put the logger's configuration file wherever they wish, and to write
> >> their
> >> > log files wherever and however they wish.
> >> >
> >> > I have used Log4j 1 in other webapps. There I alter the logging
> >> properties
> >> > in a Listener by calling PropertyConfigurator.configure() on whatever
> >> file
> >> > was named in the webapp's context as "log4j-init-file" (in Tomcat, the
> >> > webapp's META-INF/context.xml file). I am using Log4j 2.3 and Tomcat
> >> > 8.0.22.
> >> >
> >> > Log4j 2 does not allow configuring with PropertyConfigurator. From the
> >> > documentation, I had thought that if I set
> >> > org.apache.logging.log4j.web.Log4jServletContextListener as the first
> >> > <listener> in the web.xml, and added the <filter>, <filter-mapping>,
> and
> >> > <context-params> described in
> >> > https://logging.apache.org/log4j/2.0/manual/webapp.html for a Servlet
> >> 2.5
> >> > app, I could then override the log4jConfiguration
> >> > in META-INF/context.xml with
> >> >
> >> > <Parameter
> >> >     description="Log4j 2.x configuration file."
> >> >     name="log4jConfiguration"
> >> >     value="file:///Users/thad/conf/myweblog.xml"/>
> >> >
> >> >
> >> > This is not working for me. The app is still using the values set by
> the
> >> > log4jConfiguration in web.xml. I have tried using a file path and a
> >> file://
> >> > URI. I am starting Tomcat and own myweblog.xml so permissions should
> >> not be
> >> > the problem (and the Log4j 1.x properties files can be read by their
> >> apps).
> >> >
> >> > What am I missing or misunderstanding? How can I accomplish what I
> >> could do
> >> > in Log4j 1.x?
> >> >
> >> > --
> >> > "Hell hath no limits, nor is circumscrib'd In one self-place; but
> where
> >> we
> >> > are is hell, And where hell is, there must we ever be" --Christopher
> >> > Marlowe, *Doctor Faustus* (v. 121-24)
> >> >
> >>
> >>
> >>
> >> --
> >> E-Mail: garydgregory@gmail.com | ggregory@apache.org
> >> Java Persistence with Hibernate, Second Edition
> >> <http://www.manning.com/bauer3/>
> >> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> >> Spring Batch in Action <http://www.manning.com/templier/>
> >> Blog: http://garygregory.wordpress.com
> >> Home: http://garygregory.com/
> >> Tweet! http://twitter.com/GaryGregory
> >>
> >
> >
> >
> > --
> > "Hell hath no limits, nor is circumscrib'd In one self-place; but where
> we
> > are is hell, And where hell is, there must we ever be" --Christopher
> > Marlowe, *Doctor Faustus* (v. 121-24)
> >
>
>
>
> --
> "Hell hath no limits, nor is circumscrib'd In one self-place; but where we
> are is hell, And where hell is, there must we ever be" --Christopher
> Marlowe, *Doctor Faustus* (v. 121-24)
>



-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Re: Changing log4jConfiguration context-param with Tomcat 8

Posted by Thad Humphries <th...@gmail.com>.
I figured it out, thanks in large part to this blog post by Steffen
Schäfer:
https://blog.oio.de/2015/04/27/log4j-2-configuration-depending-environment/

Basically I

(1) Create a fallback logger configuration file in WEB-INF.
(2) Create a listener that checks the log4jConfiguration context parameter,
and--borrowing from Steffen's example--sets the
Log4jWebSupport.LOG4J_CONFIG_LOCATION. I declare this listener in web.xml
immediately after the
org.apache.logging.log4j.web.Log4jServletContextListener. If
log4jConfiguration is not present, my listener loads the fallback.
(3) Do *not* declare the log4jConfiguration context parameter in web.xml,
but only in the context.xml file.

True, if the user does not deploy an application context or edit the
fallback configuration file, they may not get a log because the log file
location may not exist. That's fine, so long as these things can be done,
and the logger configuration file and the logs can be written outside of
the servlet container.

On Tue, Sep 22, 2015 at 1:53 PM, Thad Humphries <th...@gmail.com>
wrote:

> This is a Mac. There is no drive letter. One of the reasons users
> (actually, administrators) need to modify the logger configuration is that
> this and other webapps are deployed at many sites on a wide range of
> operating systems and configurations. What is a valid path on a Windows box
> is not a valid path on Mac, Solaris, Linux, etc.
>
> On Tue, Sep 22, 2015 at 1:33 PM, Gary Gregory <ga...@gmail.com>
> wrote:
>
>> Does adding the driver letter to the URI make a difference?
>>
>> Gary
>>
>> On Tue, Sep 22, 2015 at 10:26 AM, Thad Humphries <
>> thad.humphries@gmail.com>
>> wrote:
>>
>> > I am adding Log4j 2 to an existing webapp. I am required to allow users
>> to
>> > put the logger's configuration file wherever they wish, and to write
>> their
>> > log files wherever and however they wish.
>> >
>> > I have used Log4j 1 in other webapps. There I alter the logging
>> properties
>> > in a Listener by calling PropertyConfigurator.configure() on whatever
>> file
>> > was named in the webapp's context as "log4j-init-file" (in Tomcat, the
>> > webapp's META-INF/context.xml file). I am using Log4j 2.3 and Tomcat
>> > 8.0.22.
>> >
>> > Log4j 2 does not allow configuring with PropertyConfigurator. From the
>> > documentation, I had thought that if I set
>> > org.apache.logging.log4j.web.Log4jServletContextListener as the first
>> > <listener> in the web.xml, and added the <filter>, <filter-mapping>, and
>> > <context-params> described in
>> > https://logging.apache.org/log4j/2.0/manual/webapp.html for a Servlet
>> 2.5
>> > app, I could then override the log4jConfiguration
>> > in META-INF/context.xml with
>> >
>> > <Parameter
>> >     description="Log4j 2.x configuration file."
>> >     name="log4jConfiguration"
>> >     value="file:///Users/thad/conf/myweblog.xml"/>
>> >
>> >
>> > This is not working for me. The app is still using the values set by the
>> > log4jConfiguration in web.xml. I have tried using a file path and a
>> file://
>> > URI. I am starting Tomcat and own myweblog.xml so permissions should
>> not be
>> > the problem (and the Log4j 1.x properties files can be read by their
>> apps).
>> >
>> > What am I missing or misunderstanding? How can I accomplish what I
>> could do
>> > in Log4j 1.x?
>> >
>> > --
>> > "Hell hath no limits, nor is circumscrib'd In one self-place; but where
>> we
>> > are is hell, And where hell is, there must we ever be" --Christopher
>> > Marlowe, *Doctor Faustus* (v. 121-24)
>> >
>>
>>
>>
>> --
>> E-Mail: garydgregory@gmail.com | ggregory@apache.org
>> Java Persistence with Hibernate, Second Edition
>> <http://www.manning.com/bauer3/>
>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>> Spring Batch in Action <http://www.manning.com/templier/>
>> Blog: http://garygregory.wordpress.com
>> Home: http://garygregory.com/
>> Tweet! http://twitter.com/GaryGregory
>>
>
>
>
> --
> "Hell hath no limits, nor is circumscrib'd In one self-place; but where we
> are is hell, And where hell is, there must we ever be" --Christopher
> Marlowe, *Doctor Faustus* (v. 121-24)
>



-- 
"Hell hath no limits, nor is circumscrib'd In one self-place; but where we
are is hell, And where hell is, there must we ever be" --Christopher
Marlowe, *Doctor Faustus* (v. 121-24)

Re: Changing log4jConfiguration context-param with Tomcat 8

Posted by Thad Humphries <th...@gmail.com>.
This is a Mac. There is no drive letter. One of the reasons users
(actually, administrators) need to modify the logger configuration is that
this and other webapps are deployed at many sites on a wide range of
operating systems and configurations. What is a valid path on a Windows box
is not a valid path on Mac, Solaris, Linux, etc.

On Tue, Sep 22, 2015 at 1:33 PM, Gary Gregory <ga...@gmail.com>
wrote:

> Does adding the driver letter to the URI make a difference?
>
> Gary
>
> On Tue, Sep 22, 2015 at 10:26 AM, Thad Humphries <thad.humphries@gmail.com
> >
> wrote:
>
> > I am adding Log4j 2 to an existing webapp. I am required to allow users
> to
> > put the logger's configuration file wherever they wish, and to write
> their
> > log files wherever and however they wish.
> >
> > I have used Log4j 1 in other webapps. There I alter the logging
> properties
> > in a Listener by calling PropertyConfigurator.configure() on whatever
> file
> > was named in the webapp's context as "log4j-init-file" (in Tomcat, the
> > webapp's META-INF/context.xml file). I am using Log4j 2.3 and Tomcat
> > 8.0.22.
> >
> > Log4j 2 does not allow configuring with PropertyConfigurator. From the
> > documentation, I had thought that if I set
> > org.apache.logging.log4j.web.Log4jServletContextListener as the first
> > <listener> in the web.xml, and added the <filter>, <filter-mapping>, and
> > <context-params> described in
> > https://logging.apache.org/log4j/2.0/manual/webapp.html for a Servlet
> 2.5
> > app, I could then override the log4jConfiguration
> > in META-INF/context.xml with
> >
> > <Parameter
> >     description="Log4j 2.x configuration file."
> >     name="log4jConfiguration"
> >     value="file:///Users/thad/conf/myweblog.xml"/>
> >
> >
> > This is not working for me. The app is still using the values set by the
> > log4jConfiguration in web.xml. I have tried using a file path and a
> file://
> > URI. I am starting Tomcat and own myweblog.xml so permissions should not
> be
> > the problem (and the Log4j 1.x properties files can be read by their
> apps).
> >
> > What am I missing or misunderstanding? How can I accomplish what I could
> do
> > in Log4j 1.x?
> >
> > --
> > "Hell hath no limits, nor is circumscrib'd In one self-place; but where
> we
> > are is hell, And where hell is, there must we ever be" --Christopher
> > Marlowe, *Doctor Faustus* (v. 121-24)
> >
>
>
>
> --
> E-Mail: garydgregory@gmail.com | ggregory@apache.org
> Java Persistence with Hibernate, Second Edition
> <http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
>



-- 
"Hell hath no limits, nor is circumscrib'd In one self-place; but where we
are is hell, And where hell is, there must we ever be" --Christopher
Marlowe, *Doctor Faustus* (v. 121-24)

Re: Changing log4jConfiguration context-param with Tomcat 8

Posted by Gary Gregory <ga...@gmail.com>.
Does adding the driver letter to the URI make a difference?

Gary

On Tue, Sep 22, 2015 at 10:26 AM, Thad Humphries <th...@gmail.com>
wrote:

> I am adding Log4j 2 to an existing webapp. I am required to allow users to
> put the logger's configuration file wherever they wish, and to write their
> log files wherever and however they wish.
>
> I have used Log4j 1 in other webapps. There I alter the logging properties
> in a Listener by calling PropertyConfigurator.configure() on whatever file
> was named in the webapp's context as "log4j-init-file" (in Tomcat, the
> webapp's META-INF/context.xml file). I am using Log4j 2.3 and Tomcat
> 8.0.22.
>
> Log4j 2 does not allow configuring with PropertyConfigurator. From the
> documentation, I had thought that if I set
> org.apache.logging.log4j.web.Log4jServletContextListener as the first
> <listener> in the web.xml, and added the <filter>, <filter-mapping>, and
> <context-params> described in
> https://logging.apache.org/log4j/2.0/manual/webapp.html for a Servlet 2.5
> app, I could then override the log4jConfiguration
> in META-INF/context.xml with
>
> <Parameter
>     description="Log4j 2.x configuration file."
>     name="log4jConfiguration"
>     value="file:///Users/thad/conf/myweblog.xml"/>
>
>
> This is not working for me. The app is still using the values set by the
> log4jConfiguration in web.xml. I have tried using a file path and a file://
> URI. I am starting Tomcat and own myweblog.xml so permissions should not be
> the problem (and the Log4j 1.x properties files can be read by their apps).
>
> What am I missing or misunderstanding? How can I accomplish what I could do
> in Log4j 1.x?
>
> --
> "Hell hath no limits, nor is circumscrib'd In one self-place; but where we
> are is hell, And where hell is, there must we ever be" --Christopher
> Marlowe, *Doctor Faustus* (v. 121-24)
>



-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory