• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Will someone put log4j usage into small words?

 
Tony Ennis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Ennis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic