java - Regex to replace logging -
i add stacktrace error logging, anywhere have
try { dosomething(); } catch (exception ex) { something(); logger.severe("oops"); something(); }
i have this:
try { dosomething(); } catch (exception ex) { something(); logger.log(level.severe,"oops",ex); something(); }
the exception variable can ex, e etc.
i created regex, quite slow , not work 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); }
use lazy matchers instead of greedy ones. refer java.util.regex.pattern documentation more details.
Comments
Post a Comment