0

i'm asking if it's possible to separate .log line to variable as it was done to construct a .log file. Let me explain :

As it shows on : https://httpd.apache.org/docs/2.4/logs.html

Combined Log format :

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined

I have to make this with Java:

Sample line:

xxx.xxx.xxx.xxx - - [03/Jul/2017:09:05:56 +0000] "GET /index.php?route=journal2/assets/css&j2v=2.7.6 HTTP/1.1" xxx xxx "http://xxxxxxxx.xxx.xxx/index.php?route=product/search&search=asus" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.90 Safari/537.36 Vivaldi/1.91.867.38"

I need to separate this string to :

  • xxx.xxx.xxx.xxx
  • short stroke
  • short stroke
  • [03/Jul/2017:09:05:56 +0000]
  • "GET /index,php?route=journal2/assets/css&j2v=2.7.6 HTTP/1.1"
  • xxx
  • xxx
  • "xxxxxxxx.xxx.xxx/index.php?route=product/search&search=asus" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.90 Safari/537.36 Vivaldi/1.91.867.38"

Got the idea? How can i make it without regex? P.S. it will get like hundreds of lines, so any ideas how to process them like that?

4
  • Why don't you want to use regex? Commented Aug 2, 2017 at 8:46
  • I can't use it, i was told not to use that, to make it harder. Commented Aug 2, 2017 at 8:47
  • I don't want that to be done, i want ideas how to make that. Because i'm new at java and trying to understand everything. The attempts are zero at the moment, i was figuring out how should i start doing this. Commented Aug 2, 2017 at 8:48
  • I do not expect that. I want only suggestions and better ideas to make it working. Commented Aug 2, 2017 at 8:51

1 Answer 1

0

The string class has methods such as indexOf() which you can use to manually "search" a string for specific "separators". To then use methods like substring() to fetch, well substrings.

In other words: it is absolutely possible to write your own very specific parsing code that extracts values from a string.

But that means trying to re-invent an existing wheel; knowing that your attempts will cost a lot of time, and the outcome will be most likely incomplete and break on the very first log line you didn't anticipate.

And when I get your question right, you want to be able to provide a "format" such as: LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" that is then somehow used to dissect the log lines. Sorry - that is exactly what you use regular expressions for. So again: your approach boils down to inventing and implementing your own regex language and engine. Of course this is possible, but a waste of time.

Beyond that, consider this task to be very advanced. This is nothing a newbie can think of doing in reasonable amounts of time. Instead: spend your energy to learn to apply existing, well-proven, well-tested technology. In such situations, that is the way to go (also from a business perspective - anything else is also a waste of money).

And assuming this is for "learning purposes" - then the first paragraph tells you where to start. But beyond that:

  • you have to first define your own "pattern language"
  • then you have to implement a parser for such formats
  • then you have to implement an engine that takes parsed formats as input and matches them onto input strings

As said: possible, but a ton of hard work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, for answer. But is it possible to make something similar to LogFormat as it mentioned above? Because i'm planning to use Configuration file such as json that will have file format which it has to use.
Thanks, i guess i will have to tell them that this is just a waste of time, that i have to you regex. :) Thanks a lot!
The point is: it is a lot of work. And in case the idea is to use this for anything else than a learning product (meaning: it is not "throw away code") then inventing your own wheel is the less optimal strategy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.