Ahmet Karakaya
Ahmet Karakaya

Reputation: 10139

Avoiding of printing full package name of method in log4j

I have an API that uses log4j for logging. When I have used the API in my project, though log statements related to project printed with ontl method name, but log statements coming from API is printed full package name format. In log4j.properties file I am using "%c" (lowercase).

How I can force all project log statements get printed only method name.

Lets say;

I have two classes Main.java and AlarmCategoryImpl.java AlarmCategroryImpl.java is located on API class, Main.java is defined in my project class.

 static Logger                       logger = Logger.getLogger(AlarmCategoryImpl.class);
 static Logger               logger   = Logger.getLogger(Main.class);

and its log4j output.

 2012-12-01/18:13:22.220/EET [INFO][Main->main] starting...  
2012-12-01/18:13:22.447/EET [INFO][com.monitor.base.alarmmanagement.alarmconfigurationImpl.AlarmCategoryImpl->copyStructureRecursive] Copying AlarmCategoryImpl

Upvotes: 2

Views: 6634

Answers (1)

Isaac
Isaac

Reputation: 16736

%c means "category name", which is synonymous to "logger name". That means that %c will be expanded to the logger's name.

The logger name is not necessarily the fully-qualified class name. The logger name is the string that is passed in to Logger.getLogger(). Therefore, if you have a class named x.y.z.MyClass, and it has this:

private static final Logger logger = Logger.getLogger("hello");

Then log statements will be generated with hello expanded instead of %c.

That means that the classes in your API are using getLogger(), passing the class name as a parameter. That causes %c to be expanded to the fully-qualified class name when the logs print.

I'm guessing that your non-API classes (in other words, your own project's classes) don't pass-in any value to Logger.getLogger(), or perhaps they use the root logger. To be sure, paste here the line of your code that retrieves the Logger instance.

EDIT as per comment:

Well, is it possible that your Main class is inside the default package? (that is, it is not associated with any package)? If yes, then I don't see any problem.

[INFO][Main->main]: INFO is the level, Main is the class, main is the method.

[INFO][com.monitor.base.alarmmanagement.alarmconfigurationImpl.AlarmCategoryImpl->copyStructureRecursive]: INFO is the level, com.monitor.base.alarmmanagement.alarmconfigurationImpl.AlarmCategoryImpl is the class, copyStructureRecursive is the method.

Upvotes: 0

Related Questions