468,242 Members | 1,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

log file parsing

I have a log file like this:

2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] server:
shutting down server
2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] 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
Aug 26 '08 #1
3 1141
On Aug 26, 1:18*pm, anush <anushshe...@gmail.comwrote:
2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] 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.
Aug 26 '08 #2
On 26 Aug, 12:48, Sjoerd <sjoer...@gmail.comwrote:
On Aug 26, 1:18 pm, anush <anushshe...@gmail.comwrote:
2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] 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.
Aug 26 '08 #3
anush wrote:
I have a log file like this:

2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] server:
shutting down server
2008-07-24 17:20:33 W [prog.c:415:prog_cleanup_and_exit] 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
Aug 26 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Roberto A. F. De Almeida | last post: by
5 posts views Thread by baskarpr | last post: by
AdrianH
5 posts views Thread by AdrianH | last post: by
AdrianH
1 post views Thread by AdrianH | last post: by
2 posts views Thread by Felipe De Bene | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.