wood burning stoves 2.0*
The moose likes Other Open Source Projects and the fly likes How to use log across objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "How to use log across objects" Watch "How to use log across objects" New topic

How to use log across objects

John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Hi there,
I am new to log4j. I have a few questions and hope to for your generous answer.

1) How to create a Logger? What are different of getLogger(name) and getLogger(class)?

2) I created a logger in the main one logger in a class specific. Why can I logged the specific class?

* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
import java.io.File;
import java.io.IOException;

import com.foo.Bar;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.SimpleLayout;

public class MyApp
// Define a static logger variable so that it references the
// Logger instance named "MyApp".
static Logger logger = Logger.getLogger(MyApp.class);

public static void main(String[] args)
File curDir = new File("");
File logDir = new File(curDir.getAbsolutePath() + "\\" + "logs");

if (!logDir.exists())
System.out.println("absolute path " + logDir.getAbsolutePath());

File logFile = new File(logDir.getAbsoluteFile() + "\\log.txt");
} catch (IOException e)
// TODO Auto-generated catch block

SimpleLayout layout = new SimpleLayout();
FileAppender appender = null;
try {
appender = new FileAppender(layout, logFile.getAbsolutePath(),true);
} catch(Exception e) {}

logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");


// Set up a simple configuration that logs on the console.
// BasicConfigurator.configure();
logger.info("Entering application.");
Bar bar = new Bar();
logger.info("Exiting application.");


package com.foo;
import org.apache.log4j.Logger;

public class Bar
Logger logger = Logger.getLogger("com.foo.Bar");

public void doIt()
logger.debug("Did it again!");


Follow is my output:
DEBUG - Here is some DEBUG
INFO - Here is some INFO
WARN - Here is some WARN
ERROR - Here is some ERROR
FATAL - Here is some FATAL
INFO - Entering application.
INFO - Exiting application.

No did it again has been logged. Thank you very much

Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
1. Logger.getLogger(Class clazz) is shorthand for getLogger(clazz.getName()), it uses the fully qualified class name as the name of the logger. When you use getLogger(String name) you can pass in whatever name you want to ... class name or use some other naming convention.

2. You are only configuring an appender attached to the logger in your main class. The "com.foo.Bar" Logger has no appenders.

Are you trying to use PropertyConfigurator to configure log4j? You aren't passing in a property file to the configure method, you're passing the log file, check the API for PropertyConfigurator:


I'd recommend either configuring with a property/xml file OR configure programmatically. It can be complicated if you try to do both.

If you go with the property/xml file you can just drop it on the classpath.

Did this work when you used the BasicConfigurator?

BasicConfigurator configures the root logger to log to console. When you configure the root logger you don't have to configure each of your loggers.
[ April 16, 2006: Message edited by: Carol Enderlin ]
John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Thanks Carole for the reply.
I did not use property and don't yet know how. Basically, I just want a simple log so that all classes can log their messages on the same log file. I don't know how to do that yet. So all classes which desire to log have to obtain the Logger by getLogger(package.ThisClass) statically?

Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Originally posted by John McDonald:
So all classes which desire to log have to obtain the Logger by getLogger(package.ThisClass) statically?

That's a very common way to use log4j.

Regarding using a properties file, the Short introduction to log4j gives a couple of examples of property configuration. The simplest way to configure log4j with a properties file is to name it log4j.properties and put it on the classpath.
I agree. Here's the link: http://aspose.com/file-tools
subject: How to use log across objects