0

I would like to add stacktrace to error logging, so anywhere we have

try {
    doSomething();
} catch (Exception ex) {
    something();
    logger.severe("oops");
    something();
}

I would like to have this:

try {
    doSomething();
} catch (Exception ex) {
    something();
    logger.log(Level.SEVERE,"oops",ex);
    something();
}

The exception variable can be anything ex, e etc.

I created a regex, but it is quite slow and does not work as I expect.

Regex:

((?s:.)*catch\s*\(\s*Exception\s*(.+)\s*\)\s*\{(?s:.)*logger\.)severe\(((?s:.)*?)\);((?s:.)*)

Replace:

$1log(Level.SEVERE,$3,$2); $4

It replaces

try {
  doSomething();
} catch (Exception ex) {
    something();
    logger.severe("oops");
    something();
} 
{
   logger.severe("oops2");
}

to

try {
  doSomething();
} catch (Exception ex) {
    something();
    logger.severe("oops");
    something();
} 
{
   logger.log(Level.SEVERE,"oops2",ex); 
}
2
  • 1
    So you're writing code to replace code? Does your development environment not include a find/replace all function? Commented Jun 19, 2015 at 15:14
  • No. I want to replace code with regex using it in the replace all command of my IDE. Commented Jun 19, 2015 at 15:33

1 Answer 1

1

Use lazy matchers instead of greedy ones. Refer to the java.util.regex.Pattern documentation to get more details.

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

1 Comment

yes, thanks I forgot to use lazy matcher by the "logger" word

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.