开发者

How best to perform search and replace Log4J logging on Java files

开发者 https://www.devze.com 2023-03-25 01:04 出处:网络
I am trying to write a script or a program in Java to find and replace all instances and variants of Log4J into logback / SLF4J.

I am trying to write a script or a program in Java to find and replace all instances and variants of Log4J into logback / SLF4J.

Here are some examples of my Log4J loggers:

log.error(myName+ "Ctrl: Exception: caught in runBcpOrderInfoSearchState "+ e.get开发者_如何学CMessage());

To change to:

log.error("{}Ctrl: Exception: caught in runBcpOrderInfoSearchState {}", myName, e.getMessage());

Please share some experience if you have.


I've had to do things like this several times before. You can write regexps, but it's usually too difficult (too many corner cases) to get correct.

My best advice is to use a keyboard macro system - I usually fire up Emacs - and get clever. Find the quoted string, cut it, move it to where the parenthesis starts, replace + with ,. etc. Make a macro, fire it on the next case, self-correct if necessary. It's a great way to make it slightly less tedious without getting lost in the complicated crud of doing something automated that you'll probably never use again.


I have made an enhancement to the slf4j migrator that converts log messages to be parameterized: https://github.com/JamesStauffer/slf4j


Use the slf4j migrator.

0

精彩评论

暂无评论...
验证码 换一张
取 消