For years, Carla's company had a Java application with a memory leak. No one had the time, energy, or budget to actually do anything about the leak, so they "fixed" it by just forcing a nightly reboot.

When Carla asked about the nightly reboot, the elders who had been around for awhile simply said, "there were some issues we couldn't track down."

Carla was assigned a task to go track down some of those untrackable issues. The first thing she discovered was that when she changed the logging level from its production settings to "DEBUG" mode, the application crashed in minutes instead of days. That piece of information sent Carla looking at the logging code to see what was happening.

private static StringWriter logWriter; static { logWriter = new StringWriter(); Layout layout = new PatternLayout(); WriterAppender writerAppender = new WriterAppender(layout, logWriter); writerAppender.setThreshold(Level.DEBUG); writerAppender.activateOptions(); Logger.getRootLogger().addAppender(writerAppender); }

As one might gather from the names, the StringWriter logging just… writes to an in memory string. This code just adds that to the logging chain, ensuring that every log message gets stored in memory, and thus ensuring the application crashes if you leave it running for too long.

[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!