10 Replies Latest reply: Jun 9, 2008 10:48 AM by DrClap RSS

    any ideas creating app to read log file when updated?

    807591
      Afternoon all,

      I have been asked to write a java app which will read the contents of the server log file every time the log file is updated by the server. The app will be deployed onto WebSphere Application Server.

      Can anyone point me in the right direction as I have never written anything like this before and I don't know where to start. Any help will be much appreciated.

      Thanks in advance,

      A.
        • 1. Re: any ideas creating app to read log file when updated?
          807591
          Read the logs, then do what?
          • 2. Re: any ideas creating app to read log file when updated?
            807591
            The log file will be analysed and the relevant entries will be displayed on an web page. This part of the app I know how to code. The part I don't know how to code is the reading of the log file when ever it is updated by the server.

            Any ideas?
            • 3. Re: any ideas creating app to read log file when updated?
              807591
              And this web page will be updating 'live' as the user watches, presumably? Using some Ajax magic or other? I'd find out what logging framework is being used and write a custom appender for it that updates the monitoring site. That way, you don't have to muck about polling log files and such, effectively your monitoring app is updated with the same info as the log file, at the same time
              • 4. Re: any ideas creating app to read log file when updated?
                807591
                I don't know what logging framework WebSphere Application Server uses to write it's own log files and even if I did, I have no idea how I would hook into it.

                I think you maybe getting distracted by details which aren't important to the question I'm asking. However I will explain what the app will be doing as it may help make things clearer.

                When WebSphere Application Server writes updates to it's own server log, I need to read that log file looking for specific entries in it which when found will be written to an xml file. Users can view the contents of the xml file on the company intranet by viewing a web page which will display the contents of the xml file.

                I only need help with the "knowing when the server log has been updated..." bit of the project. I know how to do the rest.

                Any help you can give would be very much appreciated.

                A.
                • 5. Re: any ideas creating app to read log file when updated?
                  807591
                  alex@work wrote:
                  I don't know what logging framework WebSphere Application Server uses to write it's own log files and even if I did, I have no idea how I would hook into it.
                  At a guess, it'll be Log4J - which is well documented
                  I think you maybe getting distracted by details which aren't important to the question I'm asking. However I will explain what the app will be doing as it may help make things clearer.
                  Not at all.
                  When WebSphere Application Server writes updates to it's own server log, I need to read that log file looking for specific entries in it which when found will be written to an xml file. Users can view the contents of the xml file on the company intranet by viewing a web page which will display the contents of the xml file.
                  Sounds to me that your own Logging appender is exactly what you're after. "I need to read that log file" sounds like a requirement, but isn't. It's one way of implementing your requirements. Another way would be as I suggested. This other XML file is, after all, only another log file at the end of the day. Rather than muck about running threads, or even processes, to poll log files - which would have a quite possibly noticeable performance penalty - you could just ask the logging framework to write you this XML file directly, too. A mild learning curve for you to write the appender, but well worth it for the ease of having the logging framework ease your burden overall.
                  • 6. Re: any ideas creating app to read log file when updated?
                    807591
                    I agree with most of what you've said but unfortunately I don't have a say in what the company wants. However, I am interested in the appender idea, perhaps they may go for that if I suggest it.

                    Can you give me a brief overview in how it works?


                    Thanks in advance,

                    A.
                    • 7. Re: any ideas creating app to read log file when updated?
                      807591
                      alex@work wrote:
                      I agree with most of what you've said but unfortunately I don't have a say in what the company wants. However, I am interested in the appender idea, perhaps they may go for that if I suggest it.
                      I'd say it'll take you a day to read up about Log4J and how to write basic appenders, and another day to write your own appender for this problem. Compare that to the effort of writing something to poll a log file, re-read it constantly and update another file, operations which will get slower and slower as they go along. That's a fair amount more code than a single appender would be. There's how to sell it to your company.
                      Can you give me a brief overview in how it works?
                      Log4J uses objects called appenders, which take logging info - generated by your container - and do something with it. It ships with some appenders already in it, for writing to stdout, files, sockets and databases. You can also write your own appenders that do something more than these standard ones do. You write logging code in your application - in this case, your container already does this so you don't have to - and the configuration of Log4J decides what happens to these logging messages. That's what you're interested in. You could write an appender - a simple class - that takes raw logging messages in, and writes them out to file in whatever format you want

                      Come to think of it, depending on how complex the required XML is, you may even be able to do this without writing any code at all. You can write formatting patterns in the Log4J config that existing file appenders will use to write your XML files

                      A bit of an abstract explanation, I guess. Your best bet is to first ascertain that Log4J is indeed in use, and then read the documentation, which is surprisingly good for an Apache project :-)

                      [http://logging.apache.org/log4j/1.2/index.html]
                      • 8. Re: any ideas creating app to read log file when updated?
                        807591
                        How do I find out if Log4J is in use?


                        A.
                        • 9. Re: any ideas creating app to read log file when updated?
                          807591
                          alex@work wrote:
                          How do I find out if Log4J is in use?


                          A.
                          Ask your sysadmin, or someone who installed Websphere for you. It can probably be configured to use various loggers (which can all be extended in similar ways)
                          • 10. Re: any ideas creating app to read log file when updated?
                            DrClap
                            My applications aren't on the latest version of Websphere but I can tell you that, fortunately, my version of Websphere doesn't use Log4J. (I say fortunately because if it did, that would make a mess of my ability to use Log4J in my web apps. I would have to mess about with classloader stuff to get it to work.)

                            Anyway, the short answer to the original question is that the Unix "tail -f" command does exactly what you're asking for.