• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use log across objects

 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

<PRE>
* 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())
{
logDir.mkdir();
}
System.out.println("absolute path " + logDir.getAbsolutePath());



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



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

logger.addAppender(appender);
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");




System.out.println(logFile.getPath());
PropertyConfigurator.configure(logFile.getPath());

// Set up a simple configuration that logs on the console.
// BasicConfigurator.configure();
logger.info("Entering application.");
Bar bar = new Bar();
bar.doIt();
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!");
}
}

</PRE>


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

John
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

PropertyConfigurator.configure(logFile.getPath());



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
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


John
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic