iseif has asked for the wisdom of the Perl Monks concerning the following question:


I'm building a Server-Client app with Perl/Mojolicious and JQuery. The server generates a log file using the Log::Log4perl and send the log file name back to the client so the client can see the progress. In the client I use a regular HTML A link to the log file as:

<a href="logs/blabla.log">See log file</a>

The problem is I click on the 'See log file' in the client side, the browser shows that the file is loading and stuck until the server finishing writing to the file - just then i can see the content of the file.

I configed the Log::Log4perl to autoflush and tried to use 'log4perl.appender.Syncer' and even set the buffer to 0 or 1: log4perl.appender.Buffer but nothing helped - my Log::Log4perl config is:

log4perl.appender.myFILE = Log::Log4perl::Appender::File log4perl.appender.myFILE.filename = $logfile_name log4perl.appender.myFILE.create_at_logtime = 1 log4perl.appender.myFILE.mode = write log4perl.appender.myFILE.autoflush = 1 log4perl.appender.myFILE.umask = 0000, log4perl.appender.myFILE.layout = Log::Log4perl::Layout::Pattern +Layout log4perl.appender.myFILE.layout.ConversionPattern = [%p][%d{HH:mm} +]: %m%n
How can I see the content of the log file when the server is updating it?

Thanks a lot and have a nice week,


Replies are listed 'Best First'.
Re: View content of Log::Log4perl log file while logger is running
by karlgoethebier (Abbot) on Dec 08, 2013 at 20:27 UTC

    Inquisitorial question: can you tail the log?

    BTW, i admit that i'm shure that this will work :-)

    This doesn't help, OK - but IMHO the truth is: Log::Log4perl is hell.


    Someone might ask why. A long story. Perhaps this has something to do with the author, introducing himself as Perlmeister? What a hubris.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: View content of Log::Log4perl log file while logger is running
by taint (Chaplain) on Dec 08, 2013 at 09:41 UTC
    Unless I've misunderstood your question. You're not going to be able to view the log file, until the system has completely flushed the file to disk, and released the filehandle. So that your app can parse it for reading.

    Best wishes.


    Hey. I'm not completely useless. I can be used as a bad example.
      Thanks a lot for your replay. There is no way to 'view the file'? not opening it? for example, in Notepad++ we can open a file while Notepad is updating it - the notepad++ will tell us that the content of the file has been updated. I need to show the file content to the user and when he reload the page (F5) then we will see the new updated file.
        Well. I seem to be having a bit of difficulty following you completely.

        If I understand you correctly. You'd be likening it to opening an already created log file in your favorite editor. Then, refreshing the editor's view. Will show any additions/subtractions that may have occurred. Yes?

        If that's the case, you should be able to open an already created log file. Providing that the system is not already using it. In other words, as I stated before; if your system is either opening, writing, or otherwise, has it open for processing. You will NOT be able to open it, UNTIL the system has finished closing it. That's the way it works -- and for good reason. That's how you prevent data corruption.

        If you can get handle on the file, before the system does. You can maintain that handle, and refresh the view of that file, to see if there have been any changes. But to do that. You will need to create temp variable, that holds your current view. Thereby releasing the original file handle for other processes (whatever process is logging to log file you're viewing). So that, like you're viewing the file. They can write to it.

        Is this helping. Do you understand better how the whole process works? Did I understand your question correctly?

        Best wishes.


        Hey. I'm not completely useless. I can be used as a bad example.
Re: View content of Log::Log4perl log file while logger is running
by Tanktalus (Canon) on Dec 10, 2013 at 17:34 UTC

    I expect this is simply because the file is opened, and there is no end-of-file reached yet, so the webserver keeps looking for the rest of the input until end-of-file. I'm not sure if eof is signaled or not in this case, so this is all theory.

    You may need to write your own "tail -f" replacement for your webserver, grabbing everything that's in the file currently from a beginning index (first time through this will be zero), to the last byte (*), and send it back with the index of the end. Then repeated requests from the browser can ask for the same log file with the new index, receive the new data (if any) and append it to the data in the browser.

    (*) This byte issue could haunt you if you write any multi-byte characters to the log file. However, given your use of autoflush and %n, this is unlikely to be an issue. One workaround is to have your server-side process only read up to \n's, and not return the last line unless it includes the \n in it. Then you can be assured you're sending full lines back to the browser. If you're using ISO-88591 characters only, or at least a single-byte encoding and language, then this is moot. I just have to worry about Chinese, Japanese, etc., characters showing up in my logs, so I pay attention to this.