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 Aidan Delaney <ai...@phoric.eu> on 2012/07/18 17:00:39 UTC
Initializing log4j in a library
Dear all,
I have a bunch of loosely related class files that I distrubute in a single jar. So, for instance I have Foo.java and Bar.java, where there is not top-level init() for the library. I also want to use log4j to replace the antiquated not-quite-logging system that is in this code. I'm wondering what's the best-practice way to do this.
>From what I can see, it's best to create a ProjectLogger with a static initialiser
public class ProjectLogger {
static {
Logger logger = Logger.getLogger(ProjectLogger.class.getName());
BasicConfigurator.configure();
}
static void doInit() {}
}
Then importing this into each of Foo.java and Bar.java.
Foo.java:
import ProjectLogger;
pubilc class Foo {
static {
ProjectLogger.doInit();
Logger logger = Logger.getLogger(Foo.class.getName());
}
...
}
and so forth.
This is not pretty as you need the ProjectLogger.init() in each file. However, does this concur with best practice? Or is it a bit insane?
--
Aidan
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
RE: Initializing log4j in a library
Posted by Aidan Delaney <ai...@phoric.eu>.
Dear all,
Thank you for your time. It works on a small example, so it looks like I have fouled up my classpath for my larger project.
--
Aidan
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
RE: Initializing log4j in a library
Posted by Douglas E Wegscheid <Do...@whirlpool.com>.
if you try this, you will find that it will work just fine, provided you
have a log4j.properties or log4j.xml around....
■ DOUGLAS E. WEGSCHEID // LEAD ENGINEER
(269) 923-5278 // Douglas_E_Wegscheid@whirlpool.com
"A wrong note played hesitatingly is a wrong note. A wrong note played
with conviction is interpretation."
Aidan Delaney <ai...@phoric.eu>
07/18/2012 02:05 PM
Please respond to
"Log4J Users List" <lo...@logging.apache.org>
To
log4j-user@logging.apache.org <lo...@logging.apache.org>
cc
Subject
RE: Initializing log4j in a library
Tim,
On Wed, 2012-07-18 at 12:27 -0400, Tim Watts wrote:
On Wed, 2012-07-18 at 16:00 +0100, Aidan Delaney wrote:
> > >From what I can see, it's best to create a ProjectLogger with a
> > static initialiser
> >=20
> > public class ProjectLogger {
> > static {
> > Logger logger =3D Logger.getLogger(ProjectLogger.class.getName());
> > BasicConfigurator.configure();
> > }
> It's a bit insane. :-) The easiest thing is to put a log4j.xml or
> log4j.properties file in the classpath and you're good to go.
>
http://logging.apache.org/log4j/1.2/manual.html#Default_Initialization_Procedure
> I've read, and re-read, and tried all the examples. However, I seem to
be missing how I can create a Logger without -- at some stage -- using a
configurator.
So I can use a BasicConfigurator, or take your suggestion, an use a
log4j.properties and a PropertyConfigurator.
As a minimal working example, should the following work?
import org.apache.log4j.Logger;
public class MyApp {
static Logger logger = Logger.getLogger(MyApp.class.getName());
public static void main(String[] args) {
logger.info("Entering application.");
logger.info("Exiting application.");
}
}
From my understanding of the documentation, it shouldn't. In which case,
as I have no mainline in my application, I still think I need to provide
some central place where the Basic/PropertyConfigurator configures the
root log4j logger.
--
Aidan
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
RE: Initializing log4j in a library
Posted by Aidan Delaney <ai...@phoric.eu>.
Tim,
On Wed, 2012-07-18 at 12:27 -0400, Tim Watts wrote:
On Wed, 2012-07-18 at 16:00 +0100, Aidan Delaney wrote:
> > >From what I can see, it's best to create a ProjectLogger with a
> > static initialiser
> >=20
> > public class ProjectLogger {
> > static {
> > Logger logger =3D Logger.getLogger(ProjectLogger.class.getName());
> > BasicConfigurator.configure();
> > }
> It's a bit insane. :-) The easiest thing is to put a log4j.xml or
> log4j.properties file in the classpath and you're good to go.
> http://logging.apache.org/log4j/1.2/manual.html#Default_Initialization_Procedure
> I've read, and re-read, and tried all the examples. However, I seem to be missing how I can create a Logger without -- at some stage -- using a configurator.
So I can use a BasicConfigurator, or take your suggestion, an use a log4j.properties and a PropertyConfigurator.
As a minimal working example, should the following work?
import org.apache.log4j.Logger;
public class MyApp {
static Logger logger = Logger.getLogger(MyApp.class.getName());
public static void main(String[] args) {
logger.info("Entering application.");
logger.info("Exiting application.");
}
}
>From my understanding of the documentation, it shouldn't. In which case, as I have no mainline in my application, I still think I need to provide some central place where the Basic/PropertyConfigurator configures the root log4j logger.
--
Aidan
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
Re: Initializing log4j in a library
Posted by Tim Watts <ti...@cliftonfarm.org>.
On Wed, 2012-07-18 at 16:00 +0100, Aidan Delaney wrote:
> Dear all,
> I have a bunch of loosely related class files that I distrubute in a
> single jar. So, for instance I have Foo.java and Bar.java, where
> there is not top-level init() for the library. I also want to use
> log4j to replace the antiquated not-quite-logging system that is in
> this code. I'm wondering what's the best-practice way to do this.
>
> >From what I can see, it's best to create a ProjectLogger with a
> static initialiser
>
> public class ProjectLogger {
> static {
> Logger logger = Logger.getLogger(ProjectLogger.class.getName());
> BasicConfigurator.configure();
> }
>
> static void doInit() {}
> }
>
> Then importing this into each of Foo.java and Bar.java.
>
> Foo.java:
>
> import ProjectLogger;
>
> pubilc class Foo {
> static {
> ProjectLogger.doInit();
> Logger logger = Logger.getLogger(Foo.class.getName());
> }
>
> ...
> }
>
> and so forth.
>
> This is not pretty as you need the ProjectLogger.init() in each file.
> However, does this concur with best practice? Or is it a bit insane?
>
It's a bit insane. :-) The easiest thing is to put a log4j.xml or
log4j.properties file in the classpath and you're good to go.
See
http://logging.apache.org/log4j/1.2/manual.html#Default_Initialization_Procedure
for more details.
You *could* include such a file in the jar (but it's much better keep it
outside the jar). Be aware that users of your jar can/will specify
their own config file in the classpath that precedes (i.e overrides)
yours (or worse, yours precedes theirs -- which is the main reason to
keep it out of the jar). So the logging output and destinations could
be different than your shipped configuration. This is generally a Good
Thing since users should/must have control of these things (so long as
they understand the implications of what they're doing).