wood burning stoves 2.0*
The moose likes Java in General and the fly likes Will someone put log4j usage into small words? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Will someone put log4j usage into small words?" Watch "Will someone put log4j usage into small words?" New topic
Author

Will someone put log4j usage into small words?

Tony Ennis
Ranch Hand

Joined: May 22, 2003
Posts: 86
My brain is very very small.

What I really want to do is write a diagnostic line to a text file. With the maligned System.out.println() it's drop dead easy and I'm on to another task. My app is a web service running in axis2, which is in Tomcat container, which is in Jetspeed, to make things interesting...

I included the typical



At the top. I can log messages. To the console. I tried making them log to a file with

in the log4j.properties file and no file was created. I no longer logged to the console, but I saw no log file either.

The config file says the log file should be


How can I log to a file, and in which directory will the log file appear?


Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Using log4j is really easy. Setting it up (i.e. configuring it, can be hard.)

In the code that has your:

private static final Logger log = Logger.getLogger(MyService.class);

just write a line that looks like

log.debug("hello world";

or

log.info("the meaning of life is " + 42);

Your config file line: axis2.log says that the log goes to the current working directory file named axis2.log

The CWD depends on your working enviromnent. Most folks setup their configuration file to put the log files in a specific place, either absolute or relative
Tony Ennis
Ranch Hand

Joined: May 22, 2003
Posts: 86
Thanks for the prompt reply. I know how to write a line using log(), and I sort of have some idea of log.info() versus log.error() and so forth.

So I suppose my frustration deals with the config file and the sinking feeling I get when I make a change and ... nothing changes.

I've search a large amount of my hard drive. I don't see the log file anywhere.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Tony Ennis wrote:So I suppose my frustration deals with the config file and the sinking feeling I get when I make a change and ... nothing changes.
I've search a large amount of my hard drive. I don't see the log file anywhere.


I don't see the line(s) that tell log4j to write the file. I find the standard documentation very frustrating, so I'm on the log4j mailing list.

or rather,
log4j.rootCategory=INFO, LOGFILE

but I don't see where rootCategory is defined.

In addition to weak documentation, log4j fails silently, so if you have any problems, it doesn't tell you.

I'll past my working file as a guide

Tony Ennis
Ranch Hand

Joined: May 22, 2003
Posts: 86
There's nothing else in the file that refers to rootCategory.

There is a section for LOGFILE


But this looks boilerplate.

I frankly think Java & Co. is needlessly complex. It seems everything beyond "hello world" needs some arcane xml file located in some mysterious location. The amount of research necessary before I can write a line to a file amazes me.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Yes, its bolierplate, but with nothing else refering to the keyword, one would expect nothing to happen with it

Try this from mine:

log4j.rootLogger=INFO, stdout, base
log4j.rootLogger.layout=org.apache.log4j.PatternLayout
log4j.rootLogger.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

(i.e. replace log4j.rootCategory with log4j.rootLogger)

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Tony Ennis wrote:I frankly think Java & Co. is needlessly complex. It seems everything beyond "hello world" needs some arcane xml file located in some mysterious location.


To be accurate, Java is a very simple language that can be completely described in a very thin book.

And the standard Java logging tools are easy to use and take no configuration.

log4j is designed to be flexible and allow dynamic changes. This is what you want for a huge webapp that is deployed on redundant servers, made of parts from here and there. That functionality requires configuration stuff. Granted, the log4j configuration is not obvious, but it is extremely powerful.

If you don't need the power of log4j, don't use it. Use the standard Java JDK logger instead.
Tony Ennis
Ranch Hand

Joined: May 22, 2003
Posts: 86


Yes, its bolierplate, but with nothing else refering to the keyword, one would expect nothing to happen with it


Well, it isn't inert, it is logging or not logging to the console, so *something* is making this happen. Perhaps some other config file is including this one.

Thanks for the help!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
As Pat said, "axis2.log" is a relative path, and those don't work well in a web app environment. Try using an absolute path instead.


Ping & DNS - my free Android networking tools app
Tony Ennis
Ranch Hand

Joined: May 22, 2003
Posts: 86
I'll try that. Sounds harmless enough. I found it the way it is and didn't want to start changing too much before I understood it.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

In addition, a fair amount of existing libraries tend to use Apache logging and then configure that to use Log4j

In this case, you need a


org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

in a file named
commons-logging.properties

which, of course, has to be in the right place
Vivek Kr Singh
Ranch Hand

Joined: Oct 12, 2007
Posts: 56
while learning how to use log4j; i suggest adding log4j.debug=true in log4j.properties. This immediately shows log4j internal debug messages once the configuration file is picked up.


SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Will someone put log4j usage into small words?
 
Similar Threads
Debugging/Profiling Applications directly on Android Phone
Clear old logs
Axis2 - Handlers for SOAP headers
Learning jax-ws what should one know?
Reading colsole logs.