Java Reference
In-Depth Information
public
public class
class
Slf4jDemo2
Slf4jDemo2
{
final
final static
static
Logger theLogger
=
LoggerFactory
.
getLogger
(
Slf4jDemo2
.
class
);
public
public static
static
void
void
main
(
String
[]
args
) {
try
try
{
Person p
=
new
new
Person
();
// populate person's fields here...
theLogger
.
info
(
"I created an object {}"
,
p
);
iif
(
p
!=
null
null
) {
// bogus, just to show logging
throw
throw new
new
IllegalArgumentException
(
"Just testing"
);
}
}
catch
catch
(
Exception ex
) {
theLogger
.
error
(
"Caught Exception: "
+
ex
,
ex
);
}
}
}
Although this doesn't demonstrate network logging, it is easy to accomplish this in conjunc-
tion with a logging implementation like
Log4J
or
JUL
which allow you to provide configur-
able logging.
Log4J
is described in the next recipe.
See Also
The SLF4J website contains a
Getting Started manual
that discusses the various classpath
options. There is also the
Maven artifacts
for the various options.
Network Logging with log4j
Problem
You wish to write log file messages using
log4j
.