log file parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anush

    log file parsing

    I have a log file like this:

    2008-07-24 17:20:33 W [prog.c:415:prog _cleanup_and_ex it] server:
    shutting down server
    2008-07-24 17:20:33 W [prog.c:415:prog _cleanup_and_ex it] client:
    shutting down client

    I need to parse this using php. I have split them by spaces and
    everything is put into an array. The problem comes at the end of the
    line. Eg. In line 1, server: shutting down server will be parsed as 4
    different array values. This will make things ugly when the warning
    message is long. Is there any way where I could just display the rest
    of the message from a certain point in the line.

    regards,
    Anush
  • Sjoerd

    #2
    Re: log file parsing

    On Aug 26, 1:18 pm, anush <anushshe...@gm ail.comwrote:
    2008-07-24 17:20:33 W [prog.c:415:prog _cleanup_and_ex it] client:
    shutting down client
    >
    I need to parse this using php. I have split them by spaces and
    everything is put into an array. The problem comes at the end of the
    line. Eg. In line 1, server: shutting down server will be parsed as 4
    different array values. This will make things ugly when the warning
    message is long. Is there any way where I could just display the rest
    of the message from a certain point in the line.
    You can use the limit parameter to explode() to limit the number of
    elements in the array. Or you can use preg_match() to do some more
    advanced splitting and parsing.

    Comment

    • C. (http://symcbean.blogspot.com/)

      #3
      Re: log file parsing

      On 26 Aug, 12:48, Sjoerd <sjoer...@gmail .comwrote:
      On Aug 26, 1:18 pm, anush <anushshe...@gm ail.comwrote:
      >
      2008-07-24 17:20:33 W [prog.c:415:prog _cleanup_and_ex it] client:
      shutting down client
      >
      I need to parse this using php. I have split them by spaces and
      everything is put into an array. The problem comes at the end of the
      line. Eg. In line 1, server: shutting down server will be parsed as 4
      different array values. This will make things ugly when the warning
      message is long. Is there any way where I could just display the rest
      of the message from a certain point in the line.
      >
      You can use the limit parameter to explode() to limit the number of
      elements in the array. Or you can use preg_match() to do some more
      advanced splitting and parsing.
      It rather depends where the log is being generated. Looking at the
      sample data provided I would suggest exploding around the ':' or using
      str_tok on spaces then rebuilding the chunks of data.

      Regexes can be a bit expensive - which may be a problem parsing
      thousands of lines of log files.

      C.

      Comment

      • Curtis

        #4
        Re: log file parsing

        anush wrote:
        I have a log file like this:
        >
        2008-07-24 17:20:33 W [prog.c:415:prog _cleanup_and_ex it] server:
        shutting down server
        2008-07-24 17:20:33 W [prog.c:415:prog _cleanup_and_ex it] client:
        shutting down client
        >
        I need to parse this using php. I have split them by spaces and
        everything is put into an array. The problem comes at the end of the
        line. Eg. In line 1, server: shutting down server will be parsed as 4
        different array values. This will make things ugly when the warning
        message is long. Is there any way where I could just display the rest
        of the message from a certain point in the line.
        >
        regards,
        Anush
        Should be pretty easy using explode, as long as the format is always
        the same as your sample data, otherwise, the limit would need to be
        adjusted, or a more dynamic means of parsing would be needed.

        <?php
        // ...

        $data = explode(' ', $line, 5);

        // ...
        ?>

        --
        Curtis

        Comment

        Working...