blue-sky
blue-sky

Reputation: 53896

Getting class name for logging

To log errors for a given class, I'm accessing the class name like so: Is this a 'good' way to return the class name as a String, so it can be used for logging?

private static final String CLASS_NAME = MyClass.class.getName();
logger.error("Error occurred in "+CLASS_NAME);

Upvotes: 16

Views: 40733

Answers (6)

Daniele Licitra
Daniele Licitra

Reputation: 1608

I'm seeing a lot of people using external library, like log4j, slf4j, jboss.logger and so on...

I prefer the old way, using a simple

java.util.logging.Logger

It's standard and you don't need libraries to import.

You can initialize it as:

private static final Logger LOG = Logger.getLogger(A.class.getName());

and then each time the logger write on a stream know that you used the A class:

String errorcode = "ABC123";
LOG.log(Level.SEVERE,"This is a very important error with code {0}", new Object[]{errorcode});

whatever is you standard stream, for example a console or a logging file, it write something like:

[date]     [hour]        [level] [class]       [description]
2020-01-01 00:00:10.123  SEVERE  my.package.A  This is a very important error with code AAA123

If you want a log customization, you can use java.util.logging.Handler.

At this link you can find an example of Handler.

Upvotes: 3

raven1981
raven1981

Reputation: 1445

I'd do it this way:

logger.error("Error occured in " + this.getClass().getName());

It's easier to mantain if something changes, but there are another ways to do this.

Upvotes: 2

MariuszS
MariuszS

Reputation: 31595

Java 8 Solution

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.invoke.MethodHandles;
...

private final static Logger LOG = 
                     LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

Upvotes: 23

buc
buc

Reputation: 6358

If you initialize the logger this way:

private static Logger logger = Logger.getLogger(MyClass.class.getName())>

then the name of the class will be present in every logging event, you don't need to explicitly put it in every log call:

logger.error("Error occured while doing this and that");

The you can configure the logging service (in logging.properties in case of java.util.logging, or log4j.properties if you use Apache log4j) to include the class name in every log message.

Upvotes: 8

Pramod Kumar
Pramod Kumar

Reputation: 8014

You can set up your logging parameters in log4j.xml itself.

For exp -

<appender name="swcd-web" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="Threshold" value="DEBUG"/>
    <param name="Append" value="true"/>
    <param name="File" value="${catalina.home}/logs/swcd-web.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
    </layout>
</appender>

It would log exceptions like this in swcd-web.log file -

2012-05-23 16:34:51,632 [main] ERROR com.idc.sage.sso.dynamo.SsoDbStorage - cannot get configuration for max SSO age

Upvotes: 4

Adam Arold
Adam Arold

Reputation: 30578

Since you did not state which logging library you are using I suggest to use slf4j. I think it is the easiest you can get. You simply ask for a Logger object from slf4j's LoggerFactory like this:

private static final Logger LOGGER = LoggerFactory.getLogger(YouerClass.class);

now you can use the LOGGER object for logging.

Logging an error for example looks like this:

LOGGER.error(yourMessage, throwable);

You can send a simple string message or the whole exception.

Upvotes: 1

Related Questions