如何最好地执行搜索并替换 Java 文件上的 Log4J 日志记录
我正在尝试用 Java 编写一个脚本或程序来查找 Log4J 的所有实例和变体并将其替换为 logback / SLF4J。
以下是我的 Log4J 记录器的一些示例:
log.error(myName+ "Ctrl: Exception: caught in runBcpOrderInfoSearchState "+ e.getMessage());
更改为:
log.error("{}Ctrl: Exception: caught in runBcpOrderInfoSearchState {}", myName, e.getMessage());
如果有,请分享一些经验。
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.getMessage());
To change to:
log.error("{}Ctrl: Exception: caught in runBcpOrderInfoSearchState {}", myName, e.getMessage());
Please share some experience if you have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我以前曾多次不得不做这样的事情。您可以编写正则表达式,但通常很难(有太多极端情况)而无法获得正确结果。
我最好的建议是使用键盘宏系统 - 我通常启动 Emacs - 并且变得聪明。找到引用的字符串,剪切它,将其移动到括号开始的位置,将 + 替换为 ,。等等。制作一个宏,在下一个案例中触发它,必要时进行自我更正。这是一个很好的方法,可以让它稍微不那么乏味,而不会迷失在自动化做一些你可能永远不会再使用的复杂事情中。
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.
我对 slf4j 迁移器进行了增强,可将日志消息转换为参数化: https://github.com/JamesStauffer/ slf4j
I have made an enhancement to the slf4j migrator that converts log messages to be parameterized: https://github.com/JamesStauffer/slf4j
使用 slf4j 迁移器。
Use the slf4j migrator.