You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by antonypaul <an...@gmail.com> on 2016/02/10 15:52:09 UTC

Using log4j2 with Tomee 7

Hi,

Is there any documentation and log4j2.properties/xml file for using log4j2
with Tomee 7 M1.

Regards,
Antony



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Using-log4j2-with-Tomee-7-tp4677585.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Using log4j2 with Tomee 7

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Yes, IIRC we force tomcat to switch to something else than JUL after 2
lines we cant control but IIRC the info of these lines are repeated after
so no real information lost.

To get these lines you should add *log4j* in the classpath - and no more
the common loader which is more risky in term of infra.
Le 11 févr. 2016 09:12, "antonypaul" <an...@gmail.com> a écrit :

> I wanted to use it for container logging. I followed steps in 1 and got it
> working. In addition to the steps mentioned, I added following jars and
> log4j2.xml - log4j-1.2-api-2.2.jar, log4j-api-2.2.jar, log4j-core-2.2.jar.
> Only thing is Tomcat on startup is creating seperate log file using JDK
> logging. Then it switches over to log4j.
>
> log4j2.xml copied from a stackoverflow.com answer.
>
> <?xml version="1.0" encoding="utf-8"?>
> <Configuration status="info">
>   <Properties>
>     <Property name="logdir">${sys:catalina.base}/logs</Property>
>     <Property name="layout">%d %-5p [%t] %c- %m%n</Property>
>         <Property name="consoleLayout">%-5p- %m%n</Property>
>   </Properties>
>   <Appenders>
>     <Console name="CONSOLE" target="SYSTEM_OUT">
>       <PatternLayout pattern="${consoleLayout}"/>
>     </Console>
>     <RollingFile name="CATALINA"
>         fileName="${logdir}/catalina.log"
>         filePattern="${logdir}/catalina.%d{yyyy-MM-dd}-%i.log">
>       <PatternLayout pattern="${layout}"/>
>       <Policies>
>         <TimeBasedTriggeringPolicy />
>         <SizeBasedTriggeringPolicy size="100 MB"/>
>       </Policies>
>       <DefaultRolloverStrategy max="10"/>
>     </RollingFile>
>     <RollingFile name="LOCALHOST"
>         fileName="${logdir}/localhost.log"
>         filePattern="${logdir}/localhost.%d{yyyy-MM-dd}-%i.log">
>       <PatternLayout pattern="${layout}"/>
>       <Policies>
>         <TimeBasedTriggeringPolicy />
>         <SizeBasedTriggeringPolicy size="1 MB"/>
>       </Policies>
>       <DefaultRolloverStrategy max="10"/>
>     </RollingFile>
>     <RollingFile name="MANAGER"
>         fileName="${logdir}/manager.log"
>         filePattern="${logdir}/manager.%d{yyyy-MM-dd}-%i.log">
>       <PatternLayout pattern="${layout}"/>
>       <Policies>
>         <TimeBasedTriggeringPolicy />
>         <SizeBasedTriggeringPolicy size="1 MB"/>
>       </Policies>
>       <DefaultRolloverStrategy max="10"/>
>     </RollingFile>
>     <RollingFile name="HOST-MANAGER"
>         fileName="${logdir}/host-manager.log"
>         filePattern="${logdir}/host-manager.%d{yyyy-MM-dd}-%i.log">
>       <PatternLayout pattern="${layout}"/>
>       <Policies>
>         <TimeBasedTriggeringPolicy />
>         <SizeBasedTriggeringPolicy size="1 MB"/>
>       </Policies>
>       <DefaultRolloverStrategy max="10"/>
>     </RollingFile>
>   </Appenders>
>   <Loggers>
>     <Root level="info">
>       <AppenderRef ref="CATALINA"/>
>           <AppenderRef ref="CONSOLE"/>
>     </Root>
>     <Logger
> name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]"
>         level="info" additivity="false">
>       <AppenderRef ref="LOCALHOST"/>
>     </Logger>
>     <Logger
>
> name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]"
>         level="info" additivity="false">
>       <AppenderRef ref="MANAGER"/>
>     </Logger>
>     <Logger
>
> name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]"
>         level="info" additivity="false">
>       <AppenderRef ref="HOST-MANAGER"/>
>     </Logger>
>   </Loggers>
> </Configuration>
>
> Regards,
> Antony
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Using-log4j2-with-Tomee-7-tp4677585p4677589.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: Using log4j2 with Tomee 7

Posted by antonypaul <an...@gmail.com>.
I wanted to use it for container logging. I followed steps in 1 and got it
working. In addition to the steps mentioned, I added following jars and
log4j2.xml - log4j-1.2-api-2.2.jar, log4j-api-2.2.jar, log4j-core-2.2.jar.
Only thing is Tomcat on startup is creating seperate log file using JDK
logging. Then it switches over to log4j.

log4j2.xml copied from a stackoverflow.com answer.

<?xml version="1.0" encoding="utf-8"?>
<Configuration status="info">
  <Properties>
    <Property name="logdir">${sys:catalina.base}/logs</Property>
    <Property name="layout">%d %-5p [%t] %c- %m%n</Property>
	<Property name="consoleLayout">%-5p- %m%n</Property>
  </Properties>
  <Appenders>
    <Console name="CONSOLE" target="SYSTEM_OUT">
      <PatternLayout pattern="${consoleLayout}"/>
    </Console>
    <RollingFile name="CATALINA"
        fileName="${logdir}/catalina.log"
        filePattern="${logdir}/catalina.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${layout}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="100 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="10"/>
    </RollingFile>
    <RollingFile name="LOCALHOST"
        fileName="${logdir}/localhost.log"
        filePattern="${logdir}/localhost.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${layout}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="1 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="10"/>
    </RollingFile>
    <RollingFile name="MANAGER"
        fileName="${logdir}/manager.log"
        filePattern="${logdir}/manager.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${layout}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="1 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="10"/>
    </RollingFile>
    <RollingFile name="HOST-MANAGER"
        fileName="${logdir}/host-manager.log"
        filePattern="${logdir}/host-manager.%d{yyyy-MM-dd}-%i.log">
      <PatternLayout pattern="${layout}"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="1 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="10"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="CATALINA"/>
	  <AppenderRef ref="CONSOLE"/>
    </Root>
    <Logger
name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]"
        level="info" additivity="false">
      <AppenderRef ref="LOCALHOST"/>
    </Logger>
    <Logger
name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]"
        level="info" additivity="false">
      <AppenderRef ref="MANAGER"/>
    </Logger>
    <Logger
name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]"
        level="info" additivity="false">
      <AppenderRef ref="HOST-MANAGER"/>
    </Logger>
  </Loggers>
</Configuration>

Regards,
Antony



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Using-log4j2-with-Tomee-7-tp4677585p4677589.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Using log4j2 with Tomee 7

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi Antony,

depends what it means:

I- log4j2 as container logging solution:

* add log4j2 jars and
http://repo.maven.apache.org/maven2/org/apache/tomee/log4j2-tomee/7.0.0-M1/log4j2-tomee-7.0.0-M1.jar
in tomee/lib (or in common.loader if you don't want to change default
folders)
* add openejb.log.factory = log4j2 in conf/system.properties
* add log4j2.xml in common.loader (can be lib/ directory directly or any
other folder in the common.loader)
* Note: the log4j2-tomee jar ensures logs are flushed at container shutdown
cause log4j2 uses shutdown hooks which are not reliable (you can loose log
using them). Bug have been reported but without a container integration it
is hard to solve and log4j2 didnt want to integrate with all containers.
See https://issues.apache.org/jira/browse/LOG4J2-658

II- log4j2 as application logging solution

* add log4j2 jars in WEB-INF/lib
* add log4j2.xml in WEB-INF/classes
* Note1: log4j2-web is buggy - mainly works only for servlet only in a
reliable way - so if you need it ensure the feature you use work. I would
recommand to start/stop log4j yourself either in a @Singleton or using CDI
listeners (@Initialized/@Destroyed(ApplicationScoped.class)).
* Note2: ear support is not perfect, see
https://issues.apache.org/jira/browse/LOG4J2-788 - but it is solvable using
a custom selector (see the diff)


All the warning I mentionned can just be warnings and not affect you but I
prefer to be complete there.

Last note: in EE it is always better to not use static loggers which is an
anti pattern cause:

- classes are scanned and can be instantiated to validate runtime
constraints in a temp loader where no code will be invoked so no need to
use static loggers which would initialized the whole logging stack for
nothing - and potentially leak streams
- classes are loaded eagerly by the container at several places but no
instance are created eagerly in most of cases. it means you can get
initialized too early using static loggers and before log4j2 initialization
- on this point we could extend log4j2-tomee a bit to initialize/destroy
log4j2 through container hooks but it would have the drawback to sometimes
initialize it before the ServletContext is available.

Hope it helps!



Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2016-02-10 15:52 GMT+01:00 antonypaul <an...@gmail.com>:

> Hi,
>
> Is there any documentation and log4j2.properties/xml file for using log4j2
> with Tomee 7 M1.
>
> Regards,
> Antony
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Using-log4j2-with-Tomee-7-tp4677585.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>