Dominic Charley-Roy
https://github.com/dominiccharleyroy
dominic.charley-roy @ mail.mcgill

Think of these use cases.

To work with an external jar file, we need to add it to our class path.
In Eclipse:
In the Package Explorer, right-click on the jar file and select Build Path > Add to Build Path
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>public class MyApplication {
private static final Logger logger =
LogManager.getLogger("My Application");
}
logger.info("Hello, world!");
The Logger has methods for the various levels:
logger.warn("This is the warning level.");
logger.error("This is the error level.");
logger.fatal("This is the fatal level.");
logger.debug("This is the debug level.");
logger.trace("This is the trace level.");
There is also a generic logging method:
logger.log(Level.WARN, "This is a warning.");The logging methods let you some basic string interpolation. Instead of...
We can do:
logger.info("The user " + username + " reset their password to " + password);logger.info("The user {} reset their password to {}", username, password);We can get even fancier! If we use a formatted logger:
Then we can mix in variables and format them! This is kind of like printf in C/C++ or the StringFormatter.
Logger logger = LogManager.getFormatterLogger("MyApplication");logger.info("The order of %s had %d item(s) for a total of %10.2f$.",
username, items, price);
When you call a logging method, a LogEvent is created using your text.
This has a message, a level, and an associated logger.
It's very easy to have more than one appender! Each has a unique name. For example, if you have a File appender and a Console appender:
<Appenders>
<Console name="Console" ...>
...
</Console>
<File name="FileAppender" ...>
...
</File>
</Appenders>To use both:
<Root level="all">
<AppenderRef ref="Console"/>
<AppenderRef ref="FileAppender"/>
</Root>The File tag lets you write log events to a file! It can be configured using these parameters:
<File name="FileAppender"
fileName="problems.log"
append="true"/>This will keep up to 4 log files (log.txt, log-1.txt, log-2.txt, log-3.txt). The last three files are archives, so log.txt has the freshest logs! Each log file is limited to 5MB.
You can also do time-based rolling over with this!
<RollingFile name="RollingFile" fileName="log.txt"
filePattern="log-%i.txt">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="3"/>
</RollingFile>This can send out an email every time you get x log events! Suppose every 50 messages you want an email:
Be careful with this!
<SMTP name="Mail" subject="Error Log"
to="errors@mywebsite.com"
from="errors@mywebsite.com"
smtpHost="localhost" smtpPort="25"
bufferSize="50">
</SMTP>Filters let you filter log events such that only some will get sent to particular log appenders!
For example, you could keep all events of level warn or higher in a file and ignore anything less.
Filters are added as a child node of an Appender.
This lets you specify a particular log level. You can then say what to do with log events that are that level or higher (match) as well as with those that are lower (don't match).
Example: If you only want to keep events of level WARN or higher:
<Console name="Console" target="SYSTEM_OUT">
<ThresholdFilter level="WARN"
onMatch="ACCEPT"
onMismatch="DENY"/>
</Console><BurstFilter level="INFO" rate="10" maxBurst="50"/>Suppose you want to write all log messages to a console and only messages with a level of warning or higher to a file "problems.log". We have two appenders:
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
...
</Console>
<File name="FileAppender"
fileName="problems.log"
append="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<ThresholdFilter level="WARN" onMatch="ACCEPT"/>
</File>
</Appenders>To use both:
<Root level="all">
<AppenderRef ref="Console"/>
<AppenderRef ref="FileAppender"/>
</Root>