Enable logging in Apache Commons Net for FTP protocol

All protocol implementations in Apache Commons Net, including FTPClient, derive from SocketClient, which has a method addProtocolCommandListener. You can pass it an implementation of ProtocolCommandListener to implement logging.

There’s a ready-made implementation PrintCommandListener, which prints the protocol log to provided PrintStream.

With a code like this:

ftpClient.addProtocolCommandListener(
    new PrintCommandListener(
        new PrintWriter(new OutputStreamWriter(System.out, "UTF-8")), true));

…, you will get exactly the output that you have asked for.

Leave a Comment