Java Reference
In-Depth Information
System.out.println("writing "+msg+" to file "+d-
stName);
return true;
}
}
Unlike
Console
,
File
requiresaonnullargument.Eachmethodfirstverifiesthat
thisargumentisnot
null
.Iftheargumentis
null
,themethodreturnsfalsetosignify
Thelogginglibraryallowsustointroduceportableloggingcodeintoanapplication.
Apart from a call to
newLogger()
, this code will remain the same regardless of the
logging destination.
Listing 3-21
presents an application that tests this library.
Listing 3-21.
Testing the logging library
import logging.Logger;
import logging.LoggerFactory;
class TestLogger
{
public static void main(String[] args)
{
Logger
logger
=
LoggerFact-
ory.newLogger(LoggerFactory.CONSOLE);
if (logger.connect())
{
logger.log("test message #1");
logger.disconnect();
}
else
System.out.println("cannot connect to console-
based logger");
logger = LoggerFactory.newLogger(LoggerFactory.FILE,
"x.txt");
if (logger.connect())
{
logger.log("test message #2");