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 Jason Guild <ja...@alaska.gov> on 2023/06/02 22:13:19 UTC
how to use log4j-to-jul with log4j-web
Hi All:
I have a web application that uses log4j (v2.20) which runs inside
Tomcat 9.x on Java 8.
Currently, the WEB-INF/log4j2.xml configuration file in the application
sends its output to a rolling file appender targeting a separate file on
the file system, but what I'd like to do is make all log4j output go to
the regular per-application log under ${catalina_base}/logs which is
managed by the stock Tomcat JULI implementation.
We now have the log4j-to-jul module which can adapt log4j 2 output back
to java.util.logging.
Similar to my understanding of the purpose for log4j-to-slf4j, it seems
that log4j-to-jul is meant to be used as an implementation by itself for
log4j-api and without log4j-core.
The application currently uses log4j-web to get the all the essential
setup for initializing log4j 2 under an application container. The
log4j-web library appears to have a hard dependency on log4j-core.
After adding log4j-to-jul to the application classpath, I can see that
it is recognized:
> Multiple logging implementations found:
> Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory,
> Weighting: 10
> Factory: org.apache.logging.log4j.tojul.JULLoggerContextFactory,
> Weighting: 20
But then the following error is reported:
> ERROR StatusLogger LogManager returned an instance of
> org.apache.logging.log4j.tojul.JULLoggerContextFactory which does not
> implement org.apache.logging.log4j.core.impl.Log4jContextFactory.
> Unable to initialize Log4j.
In org.apache.logging.core.config.Configurator, line 47 there is a
conditional that checks the return type of the logging implementation
is-a Log4jContextFactory. The problem is then that
JULLoggerContextFactory implements only LoggerContextFactory.
I'd like to keep my tomcat container as stock as possible. Particularly,
continuing to use the built-in JULI per-application logging and *not*
switching the entire container to use log4j as its logging implementation.
Can anyone suggest how I can use log4j-to-jul successfully for my use case?
Thanks,
Jason
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
Re: how to use log4j-to-jul with log4j-web
Posted by Ralph Goers <ra...@dslextreme.com>.
You are correct that log4j-to-jul routes the log4j2 API to java.util.logging. When using log4j-to-jul you cannot use ANY other log4j modules except for log4j-api. You must configure Tomcat’s logging to handle all the logging for your application.
Ralph
> On Jun 2, 2023, at 3:13 PM, Jason Guild <ja...@alaska.gov> wrote:
>
> Hi All:
>
> I have a web application that uses log4j (v2.20) which runs inside Tomcat 9.x on Java 8.
>
> Currently, the WEB-INF/log4j2.xml configuration file in the application sends its output to a rolling file appender targeting a separate file on the file system, but what I'd like to do is make all log4j output go to the regular per-application log under ${catalina_base}/logs which is managed by the stock Tomcat JULI implementation.
>
> We now have the log4j-to-jul module which can adapt log4j 2 output back to java.util.logging.
> Similar to my understanding of the purpose for log4j-to-slf4j, it seems that log4j-to-jul is meant to be used as an implementation by itself for log4j-api and without log4j-core.
>
> The application currently uses log4j-web to get the all the essential setup for initializing log4j 2 under an application container. The log4j-web library appears to have a hard dependency on log4j-core.
>
> After adding log4j-to-jul to the application classpath, I can see that it is recognized:
>> Multiple logging implementations found:
>> Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory, Weighting: 10
>> Factory: org.apache.logging.log4j.tojul.JULLoggerContextFactory, Weighting: 20
>
> But then the following error is reported:
>> ERROR StatusLogger LogManager returned an instance of org.apache.logging.log4j.tojul.JULLoggerContextFactory which does not implement org.apache.logging.log4j.core.impl.Log4jContextFactory. Unable to initialize Log4j.
>
> In org.apache.logging.core.config.Configurator, line 47 there is a conditional that checks the return type of the logging implementation is-a Log4jContextFactory. The problem is then that JULLoggerContextFactory implements only LoggerContextFactory.
>
> I'd like to keep my tomcat container as stock as possible. Particularly, continuing to use the built-in JULI per-application logging and *not* switching the entire container to use log4j as its logging implementation.
>
> Can anyone suggest how I can use log4j-to-jul successfully for my use case?
>
> Thanks,
> Jason
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
Re: how to use log4j-to-jul with log4j-web
Posted by "Piotr P. Karwasz" <pi...@gmail.com>.
Hi Jason,
On Sat, 3 Jun 2023 at 00:13, Jason Guild <ja...@alaska.gov> wrote:
> The application currently uses log4j-web to get the all the essential
> setup for initializing log4j 2 under an application container. The
> log4j-web library appears to have a hard dependency on log4j-core.
The `log4j-web` module is only useful to initialize the `log4j-core`
implementation of the Log4j 2.x API. If you don't use `log4j-core` you
don't need it.
On Sat, 3 Jun 2023 at 02:23, Ralph Goers <ra...@dslextreme.com> wrote:
>
> When using log4j-to-jul you cannot use ANY other log4j modules except for log4j-api. You must configure Tomcat’s logging to handle all the logging for your application.
That is a little bit oversimplified. There are a lot of Log4j modules:
* some like `log4j-layout-template-json` or `log4j-web` contain
components for `log4j-core`. These can not be used with
`log4j-to-jul`.
* other modules contain adapters from framework X to Log4j 2.x API
(e.g. `log4j-slf4j-impl`) or depend only on the Log4j 2.x API (e.g.
`log4j-iostreams`). These can be used with `log4j-to-jul`.
At work we use `log4j-api` + `log4j-to-jul` for all application
logging together with:
* `log4j-slf4j-impl` (the dependency on `log4j-core` is just for the
comfort of most users and can be safely excluded), `log4j-jcl` and
`log4j-iostreams` to forward everything other logging framework to
Log4j 2.x API,
* a `WEB-INF/classes/logging.properties` file that provides
per-application configuration (using the standard
ClassLoaderLogManager JUL implementation shipped with Tomcat). The
`${classloader.webappName}` variable comes very handy here.
Logging is not very important for our clients and this limits our
responsibility in case of problems with `log4j-core`.
Obviously this is hardly a useful setup during development and tests,
so our test machines use my Log4j extensions[1] to delegate all Log4j
2.x API calls to a `log4j-core` installation in Tomcat's system
classloader.
Piotr
[1] https://github.com/copernik-eu/log4j-plugins
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
Re: how to use log4j-to-jul with log4j-web
Posted by Jason Guild <ja...@alaska.gov>.
Hi All:
Thank you all for your help and the good information about log4j-web and
log4-core.
I already had my per-application logging configured in
WEB-INF/logging.properties and all output is going to the same log file
now that I've switched to log4j-to-jul.
Cheers,
Jason
On 6/2/2023 2:13 PM, Jason Guild wrote:
> CAUTION: This email originated from outside the State of Alaska mail
> system. Do not click links or open attachments unless you recognize
> the sender and know the content is safe.
>
> Hi All:
>
> I have a web application that uses log4j (v2.20) which runs inside
> Tomcat 9.x on Java 8.
>
> Currently, the WEB-INF/log4j2.xml configuration file in the application
> sends its output to a rolling file appender targeting a separate file on
> the file system, but what I'd like to do is make all log4j output go to
> the regular per-application log under ${catalina_base}/logs which is
> managed by the stock Tomcat JULI implementation.
>
> We now have the log4j-to-jul module which can adapt log4j 2 output back
> to java.util.logging.
> Similar to my understanding of the purpose for log4j-to-slf4j, it seems
> that log4j-to-jul is meant to be used as an implementation by itself for
> log4j-api and without log4j-core.
>
> The application currently uses log4j-web to get the all the essential
> setup for initializing log4j 2 under an application container. The
> log4j-web library appears to have a hard dependency on log4j-core.
>
> After adding log4j-to-jul to the application classpath, I can see that
> it is recognized:
>> Multiple logging implementations found:
>> Factory: org.apache.logging.log4j.core.impl.Log4jContextFactory,
>> Weighting: 10
>> Factory: org.apache.logging.log4j.tojul.JULLoggerContextFactory,
>> Weighting: 20
>
> But then the following error is reported:
>> ERROR StatusLogger LogManager returned an instance of
>> org.apache.logging.log4j.tojul.JULLoggerContextFactory which does not
>> implement org.apache.logging.log4j.core.impl.Log4jContextFactory.
>> Unable to initialize Log4j.
>
> In org.apache.logging.core.config.Configurator, line 47 there is a
> conditional that checks the return type of the logging implementation
> is-a Log4jContextFactory. The problem is then that
> JULLoggerContextFactory implements only LoggerContextFactory.
>
> I'd like to keep my tomcat container as stock as possible. Particularly,
> continuing to use the built-in JULI per-application logging and *not*
> switching the entire container to use log4j as its logging
> implementation.
>
> Can anyone suggest how I can use log4j-to-jul successfully for my use
> case?
>
> Thanks,
> Jason
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
--
Jason Guild
Systems Programmer
Alaska Department of Transportation & Public Facilities
Statewide Administrative Services
820 E. 15th Ave.
Anchorage, Alaska 99501