edtFTPj/Free - Open-source FTP library for Java | Download
 How to set up logging

Often, it can be very helpful to look at edtFTPj/Free's extensive logging output if any problems are experienced communicating with FTP servers. edtFTPj/Free has a powerful logging API modelled on the popular log4j library – in fact full integration with log4j is supported.

All commands sent to the FTP server and subsequent replies are logged, and can be made available on the console or directed to a log file. Much other useful information is also logged.

Statements within edtFTPj/Free are logged at different levels, ranging from least verbose (FATAL), to most verbose (DEBUG). Intermediate levels are ERROR, WARN, and INFO. An overall level can be set for the library, and all statements up to and including that level will be outputted.

For example, if the overall level is set to INFO then FATAL, ERROR, WARN and INFO log statements will be outputted, but DEBUG statements will not be.

There are also two additional levels that can be conveniently used - OFF which means no logging will occur, and ALL which means all log statements will be outputted, no matter what level they are. The ALL level is actually an additional level which is used for extremely verbose output - so when the level is set to ALL more log statements may be outputted than at  DEBUG.

Logging levels are encapsulated in the Level class. For example, the WARN level is represented by Level.WARN. 

By default, the log level is switched to OFF, so that no logging will appear.

The log level can be changed in two ways. Firstly, it can be changed explicitly by calling the
setLevel method on the Logger class. For example:

Logger.setLevel(Level.DEBUG);

will set the global logging level to DEBUG.

A System property, edtftp.log.level, can also be used to set the logging level. For example, using the -D option to set an application's System property, you could use:

java -Dedtftp.log.level=INFO com.mypackage.myclass

Using edtFTPj/Free's logging in your own application is very similar to using log4j. A logger is created, and its methods used to write logging information. Rather than use the Level class, the Logger class has convenience methods for logging at each level. An example is shown below:

Logger log = Logger.getLogger(MyClass.class);
log.info("Connecting to server " + host);

Logging to a file

As noted, all logging by default goes to standard output. A FileAppender must be added if logging is to go to a file (and this will disable logging to standard out). An example is shown below:

Logger.addAppender(new FileAppender(myLogFileName));

Now all logging output will go to the FileAppender's file, and no logging will go to standard output. Multiple FileAppenders can be added. If the StandardOutputAppender is added to the Logger as well, logging will be directed to the file and to standard output.

Log4j integration

Full integration with log4j is possible. A System property, edtftp.log.log4j, is used to indicate that log4j integration should be attempted. It must be set to “true”. Also, the log4j jar file must be available in the CLASSPATH. Once this is done all logging calls are directed via log4j, using reflection, and the standard log4j settings are used. More details on log4j can be found at the log4j site listed in the references.