hark-Tooth has asked for the wisdom of the Perl Monks concerning the following question:

Hello Fellow Monks, I am using Net::SFTP module for transferring files from one server to another, File Count is very huge. Now my superior wants me log these FTP transfers into Log-file Could your suggest any solution for same.

Replies are listed 'Best First'.
Re: To Log FTP Transfers.
by Athanasius (Archbishop) on May 08, 2014 at 07:02 UTC

    Hello hark-Tooth, and welcome to the Monastery!

    First, you need a logging mechanism. You could roll your own, but there are many options available on : Log::Minimal, Log::Lite, Log::Log4perl, etc.

    Next, you need a way to call your logging mechanism on each transfer operation (assuming you need this level of detail in your log). Fortunately, Net::SFTP provides callback hooks for its get and put methods, so you just need to write a small callback subroutine which invokes the logger, and pass in a reference to this sub when you invoke the get or put method.

    As the number of file transfers is “huge,” you will want to configure the logger to rotate the log file fairly regularly.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hi Athanasius, Can you please give me some sample code for "callbacks" which will invoke logger when get/put method is invoke.

        The Net::SFTP documentation already gives examples of the callbacks to monitor progress.

        This is not a code writing service. We will readily provide you with the appropriate documentation and support you with your problems, but you will have to do the learning and the work yourself.

Re: To Log FTP Transfers.
by Corion (Patriarch) on May 08, 2014 at 06:52 UTC

    Have you talked to your superior about the information that needs to be written to the log file?

    If you do not already have a logging infrastructure, the easiest approach is to just open a file and write to it. See open and print.

    As an alternative, there is Log::Log4Perl, which many people like.

    I suggest writing the file size of the file before you transfer it, the source file name, the remote directory and file name and at the end of the transfer the file size as reported by the remote end.

      Thanks for your reply I have talked with my seniors they want log to be stored in "vsftpd" log format Thu Mar 4 08:12:30 2004 1 202.114.40.242 37 /incoming/index.html a _ o a guest@my.net ftp 0 * c
      1. Thu Mar 4 08:12:30 2004
      current-time
      2. 1
      transfer-time
      3. 202.114.40.242
      remote-host
      4. 37
      byte-count
      5. /incoming/index.html
      filename
      6. a
      transfer-type
      7. _
      special-action-flag
      8. o
      direction
      9. a
      access-mode
      10. guest@my.net
      username
      11. ftp
      service-name
      12 0
      authentication-method
      13 *
      authenticated-user-id
      14. c
      completion-status

Re: To Log FTP Transfers.
by karlgoethebier (Abbot) on May 08, 2014 at 18:36 UTC

    Here is the more verbose version (from the manual of Net::SFTP:

    For GET use:

    $sftp->get($remote [, $local [, \&callback ] ]) sub callback { my($sftp, $data, $offset, $size) = @_; print "Read $offset / $size bytes\r"; }

    For PUT use:

    $sftp->put($local, $remote [, \&callback ]) sub callback { my($sftp, $data, $offset, $size) = @_; print "Wrote $offset / $size bytes\r"; }

    See also Callback (computer programming)

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: To Log FTP Transfers.
by RonW (Parson) on May 08, 2014 at 17:19 UTC
    Many systems have an equivalent to syslogd, especially Linux, BSD and other Unix-like systems. You might want to consider using Net::Syslog to interface with that.