File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes log4j w/apache Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Reply Bookmark "log4j w/apache" Watch "log4j w/apache" New topic
Author

log4j w/apache

Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hello
How can I stop the Apache debug log4j log statements but still use framework?
I am not clear if I put in log4j.rootlogger=OFF do I have to put in the properties file a
log4j.logger.[com.xyz.aaa]=DEBUG,A1
og4j.logger.[com.xyz.bbb]=DEBUG,A1
and then from within the each object I want to log do I do:
Logger aaa = Logger.getLogger(aaa.class)
and then just use api from there? That would be elegant, if not in understanding, grin.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I"m not sure what you're asking.
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
David Newton wrote:I"m not sure what you're asking.


Hi
I appear to groping my way through.
I cant use the std configuration of l4j, b/c all of the debug code for apache ...digester & digester.sax shows. I thought I could disable it with log4j.rootlogger=OFF and configure my own withing the individual object, loosing elegant runtime configuration, but at least it would work.

I am still seeing the org.apache.commons.digester.Digester, and all the parsing of the struts configuration files. However my technique did work and I am getting log lines from my individually configured objects.

My questions are two
1. How can I stop debug from Apache without setting a higher leve on the root logger? I want to use debug level in my code.
2. Is there a configuration which which I can have runtime configuration of my object specific loggers? What is the syntax to configure them from the log4j.properties file specific for each object's class name AND how do I access them from within the specific object class?


Figures:
Fig1
This is the configuration is /src
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Fig2
This is how I am creating the child Loggers per desired object
static Logger ver02_lfj=Logger.getLogger(obj_ver02.class.getName());

java.util.Properties props = new java.util.Properties();
try {
props.load(getClass().getResourceAsStream("/log4j.properties"));
}
catch (IOException ex) {
}
PropertyConfigurator.configure(props);
ver02_lfj.setLevel(Level.DEBUG);
ver02_lfj.debug("My FIRST Log4J. obj_ver02");
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you set the rootlogger to DEBUG sure, you're going to get mass quantities of debug log messages.

You can turn specific packages on/off by configuring individual loggers for them.

Personally I usually just set the root logger to INFO or WARN and set *my* packages to DEBUG. I don't find anything particularly elegant about runtime configuration, and it's a rare occasion I ever do anything more interesting with my Log instances than just create class-specific loggers.
Chris Pat
Ranch Hand

Joined: Aug 27, 2005
Posts: 156
Hi Dave
Thanks. But I thought I set the root to OFF, if that is even a command, that I "picked" up ad hoc on some google search.

The reason I find runtime configuration elegant is I have many objects, and now as I understand it I am configuring them in the constructor of each object, and setting their logging level. This means, as I understand it, that if I want to simulate production I have to go to each object and set its level to something high. This requires a source edit and recompile. I would like to just do it from the configuration file. And this is b/c the apache code has taken over the DEBUG level with its line. Yes, I am sure I could recompile it, but I prefer to buy my groceries and pasta vbg.

If that is possible, what is the syntax to create those object specific debuggers in the log4j.properties AND what is the calling syntax in the object specific code to set the logging information?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56192
    
  13

If I understand correctly, you are setting the levels programmatically within the code?

Then of course you need to change it every time you wan tto change the logging behavior. Why are you doing that? The whole point of Log4J is to set up the logging and then be able to control it completely from configuration without the need for code changes.

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yeah, I'm not quite sure I understand the issue.

In general you'll see configuration like this:This means that everything not explicitly defined has a log level of WARN: the app will be relatively quiet. However I've explicitly configured several high-level packages: I'll get lots of log messages from things in the com.davenewton.foo and org.apache.struts2 packages, almost nothing from ognl, and INFO msgs from com.opensymphony.xwork2.

In the Java code itself logs are defined like this:To make that log message not appear I simply change the com.davenewton.foo package's log level to, for example, INFO--in the log4j.properties file.

Maybe if you showed some of your code and/or explained what you're trying to do we'd understand the problem more clearly.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: log4j w/apache
 
Similar Threads
Reading log file w/out stopping tomcat
Log4J Help: Assign differen levels to a logger's appenders
Server loggng with log4j Tomcat 6.x
Logging with WSS4j
selected pacakges logging