Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to assign Log4J logging level to individual Class?

Status
Not open for further replies.

murthypatnaik

Programmer
Feb 26, 2006
15
0
0
US
How to assign logging level(info, debug, error, fatal) to individual classes?

Normally, logging level is applicable to app level.
But I need to change the logging level to each class on demand.

Please help me in this regard.

Thanks,
Murthy P
 
You assign appenders to classes/packages, for example, like this:-

Code:
log4j.logger.your.package.or.class=INFO, FileApp

If you specify a class, your assigning a logging level to that class only, whereas specifiying a package sets the level for all classes in that package abd below.

This is clearly described in the quick start guide in the log4j distro.

Tim
 
I have tried this option, but everytime this is taking rootlogger, as log4j.rootLogger=ERROR, A1

After that, as you specified, I kept app as, log4j.logger.samplePackage.sampleClass=DEBUG, A1

But this is not working as expected, as rootLogger is only taking considerarion.

Please help me in this regard.

Thanks,
Murthy P




 
I think you'd best post your entire log4.properties file. Also, how are you setting up the Logger instance in the class producing log output?

Tim
 
I am using Properties instead of log4j.properties. Here sample is the package, which is to be WARN level. I am not successful in this case. Please help me in this regard.

Following is the Properties I am setting...

________________________________________________

Properties props = new Properties();

props.put("log4j.rootCategory",value+",A2");

props.put("log4j.appender.A2","org.apache.log4j.RollingFileAppender");

props.put("log4j.appender.A2.File","./config/"+domainName+"/out");

props.put("log4j.appender.A2.MaxFileSize","1000KB");

props.put("log4j.appender.A2.MaxBackupIndex","30");

props.put("log4j.appender.A2.layout","org.apache.log4j.PatternLayout");

props.put("log4j.appender.A2.layout.ConversionPattern","%d{DATE} %-5p %x : %c - %m%n");

props.put("log4j.logger.sample","WARN,A2");

if (!log4jPropsLoaded) {
PropertyConfigurator.configure(props);
log4jPropsLoaded = true;
}
________________________________________________

Thanks,
Murthy P
 
Yes, but I also asked for how you were setting up the Logger instance in the class in question. In other words, how are you preparing and using an instance of the org.apache.log4j.Logger class in your 'sample'?

Tim
 
public static void logDebugMessage(String msg, String className, String methodName)
{
//loadLog4JProperties("ERROR");
ILogger logger = LoggerFactory.getLogger(className);

logger.debug(methodName + " - " + msg);
logger.error(methodName+ " - "+msg);
}

this is what you are talking about, right?

Thanks,
Murthy P
 
Mmmmmm.

I generally use the logger like this:-

Code:
public SomeClass {
  private static final Logger LOGGER = Logger.getLogger(SomeClass.class);

  public void someMethod(){

    LOGGER.info("Writing an information line");

    LOGGER.error("Drat, it's gone bang.");
  }
}

This makes the logger aware of the class context it belongs to and then responds correctly to the assigned levels in my log4j.properties

I prefer to use the log4j.properties file since it allows me to change logging levels without having to recompile any code.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top