Explain Codes LogoExplain Codes Logo

Log4j: Log output of a specific class to a specific appender

java
log4j
logging
configuration
Alex KataevbyAlex Kataev·Feb 28, 2025
TLDR

To channel the logs of a specific class to a unique appender in log4j, configure a logger using the class's fully qualified name and tie it with a specific appender. Here's a short log4j.properties configuration example:

# Fun fact: Appenders in log4j are just like your delivery guy. They "deliver" your logs! log4j.appender.CUSTOM=org.apache.log4j.FileAppender log4j.appender.CUSTOM.File=particular.log # Here, logger is your trusted sidekick, handling logs from your class, like Batman handles Gotham. log4j.logger.com.yoursite.YourClass=INFO, CUSTOM # Because no one likes duplicate gifts! Similarly, logger might not appreciate duplicate logs. log4j.additivity.com.yoursite.YourClass=false

In this snippet, replace CUSTOM with a suitable name for your appender, com.yoursite.YourClass with the name of your target class. Now, YourClass logs will exclusively go to particular.log.

How to structure loggers and appenders

For a clean and organized logging experience, you can arrange loggers and appenders in your log4j configuration file. Presenting structure in XML configuration enables a clear visual understanding:

<!-- Just like Spiderman swings between buildings, log4j swings logs into your files --> <appender name="FileAppender" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="specific.log"/> <param name="MaxFileSize" value="10MB"/> <param name="MaxBackupIndex" value="5"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%c{1}] %m%n"/> </layout> </appender> <!-- Loggers are like your own personal Jarvis, they sort and manage your logs --> <logger name="com.yoursite.YourClass" additivity="false"> <level value="INFO"/> <appender-ref ref="FileAppender"/> </logger>

Setting a proper log policy

Setting a log policy is to prevent your logs from becoming the blackhole of disk space. One such technique is to use rolling policies, such as TimeBasedTriggeringPolicy and strategies for automatic deletion of outdated logs.

Increasing log benefits

Increase the granularity of your log output by specifying log levels for each logger. This refines verbosity and helps filter out unnecessary information. Segregate log categories according to class/package levels for easy debugging.

Enhancing logging capability

Employing the log4j.logger property

Every class has its specific logger, and it's the log4j.logger property that helps you manage these effectively. Associating a logger with a class ensures logs are originating from the appropriate source, adding to accuracy & precision.

Tackling duplicate logs

An essential tool to stop clones in the log world is the additivity property. By preventing logs from being caught by multiple loggers, it stops duplication and ensures each log is unique.

Uniform log formatting

Ensure you have a consistent format for clear log comprehension by specifying a ConversionPattern. Offering users a homogenous and organized log overview.

Using LogManager effectively

LogManager.getLogger() lets you spawn dedicated loggers at runtime, presenting you with the opportunity to direct distinct log streams on the fly, without disturbing the primary configuration.