• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parse Stacktrace only regex

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have logs created in the below manner, I want to parse only the stacktrace? how we can go that
using regex?





I want only the below to be retrieved





Please provide your suggestion in doing this.

Thanks.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rithanya Laxmi wrote:
Please provide your suggestion in doing this.



You don't need a regular expression. Just read the log file a line at a time writing out only those lines that don't start with ~~ !
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

All lines in your logs (Except Stack trace lines ) have "ACTIVE" and "Execute" String in them,
you can read lines using buffered reader and check each line for the above strings and print others (eventually Stack trace)
you can use something like below,

 
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Sahas! You might like to add code tags and indent your code so that its readable.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sahas Jan wrote:pt Stack trace lines ) have "ACTIVE" and "Execute" String in them,



As I said in my previous response, all the non-stacktrace lines start with ~~ so the OP does not need a regex and he/she can just use String.startsWith("~~").
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:You don't need a regular expression....


@Rithanya: And that's a very good lesson.

Never start out solving a problem by saying "I need to do this" or "I need to do [it] this way", because that presumes that you already know the solution, in which case why are you bothering to ask us?

Regexes are wonderful things, but they are NOT a panacaea. They're also not user-friendly. Would you fancy trying to explain this one
"\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b."
to someone who isn't familiar with them?

Richard's solution, while possibly not complete, is extremely simple, probably faster than a regex, and will certainly remove any lines that are NOT stacktrace elements from consideration.

And if you need to reduce the results further, then maybe it's worth thinking about a regex.

Winston
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the event that you find there are other lines in your logs which do not start with ~~ and aren't stack traces, here's a possible additional refinement: just look for lines that start with " at ". Then figure out how to grab those lines, plus any line immediately before those.
 
Sahas Jangam
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:

Sahas Jan wrote:pt Stack trace lines ) have "ACTIVE" and "Execute" String in them,



As I said in my previous response, all the non-stacktrace lines start with ~~ so the OP does not need a regex and he/she can just use String.startsWith("~~").



Hi, i thought of proprosing .startsWith() or .contains() methods,
But, as she specifically asked for Reg ex, also, as we dont know how other parts of log looks like, i had to post it, just to make her happy.

Thanks for the suggestion !

- SJ.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahsan Bagwan wrote:Welcome to the Ranch Sahas!

Agree. Welcome again

You might like to add code tags and indent your code so that its readable.

I have done that, since he is new, and we can all see how much better the post looks.
 
Rithanya Laxmi
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys, since using regex is making it complex as the .log file can have content with "~~" and "ACTIVE" which
doesnt fall under stacktrace. Hence what about the below options.

1) Write only the log stacktrace in a separate .log file
2) Add a opening and closing tags only for log stacktrace so that it can be easily parsed.

Please provide your suggestion on the same. If i have to implement the first option how i can go about it?
what changes i need to make to log4j xml file? Similarly for the second option how we can do it?
Please clarify.

Thanks.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree with the others who already suggested you should just use ordinary string-processing methods to extract stack traces. You don't need regexes (as they already said) and I think it's pointless to reject the simple solution in favour of complex workarounds which might allow you to use the complex solution.
 
Rithanya Laxmi
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys, It is not easy as what I have given in the above example there are some complex log files as well which is not simple, hence the better way I can think of is using the below options:-

1) Write only the log stacktrace in a separate .log file
2) Add a opening and closing tags only for log stacktrace so that it can be easily parsed.

Please provide your suggestion on the same. If i have to implement the first option how i can go about it?
what changes i need to make to log4j xml file? Similarly for the second option how we can do it?
Please clarify.


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rithanya Laxmi wrote: . . . It is not easy as what I have given in the above example there are some complex log files as well . . .

GIGO.

You have not told us the full details. Indeed by providing incomplete information, you have managed to get what may be the wrong answer.
I agree that going back to the Exceptions might be a better solution to your problem.

This is no longer a “beginning” question, so I shall move this discussion.
 
Rithanya Laxmi
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys. To make it clear, what I was trying to do is, to capture only the exception stacktrace and write that in a .log file, this log file should contain only the exception stacktrace and shouldn't include other log details which we will print normally using debug or info.
Now my question is how we can create a log4j XML with an appender to ensure that all the exception stacktrace for the application is logged in to that single log file? The adv. i see with this approch is we wont have to parse the log to retrieve the stacktrace which is expensive as our logs files have MB's of log details. Please clarify the above and the configuration we need to do to achieve the above.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you just need an appender! Take a look at http://logging.apache.org/log4j/2.x/manual/appenders.html paying particular attention to 'filter' .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic