Example log4j2.xml (development)

For local development, I like to change some things in my log4j2 configuration.

  • Make lines shorter, e.g. no worker names (just pid) or dates (just times).
  • Prevent all logging to files, just console.
  • Use colors for errors and warnings so they stand out.
  • Filter out specific types of messages, and keep only specific other ones at debug level.
  • Use buffering and async for better performance.

So here it is!

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration monitorInterval="30">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" bufferSize="100">
            <PatternLayout pattern="%d{HH:mm:ss,SSS} %highlight{%-5p}{FATAL=red, ERROR=red, WARN=red, INFO=blue, DEBUG=black, TRACE=black} %2tid %-26c{1} - %m%n"/>
            <Filters>
                <RegexFilter regex='Filter out pattern ".*' onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="NEUTRAL" />
                <RegexFilter regex="Debug-level accept only this pattern .*" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </Console>
        <Async name="AsyncConsole">
            <AppenderRef ref="Console"/>
        </Async>
    </Appenders>
    <Loggers>
        <!-- Change next two lines to DEBUG or INFO to change logging level -->
        <Logger name="package.outside.my.control" level="WARN"/>
        <Logger name="my.package" level="DEBUG"/>
        <Root level="DEBUG">
            <AppenderRef ref="AsyncConsole"/>
        </Root>
    </Loggers>
</Configuration>

 

Topics: #coding #imadethis #java #performance #example #ide #logs

Comments

No comments yet

You need to be logged in to comment.