如何从代码库中删除System.out.println [英] How to remove System.out.println's from codebase

查看:222
本文介绍了如何从代码库中删除System.out.println的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个庞大的(旧的遗留Java)代码库,其中许多文件(大约5k)都有System.out.println。我们计划因清理/性能原因删除它们。我们如何编写一个脚本来替换它们而不会在代码中引入任何问题?该脚本不能盲目删除它们,因为以下情况可能是一个问题:

We have a huge (old legacy java) code-base, where many files (around 5k) have System.out.println's. We are planning to remove them for cleanup/performance reasons. How can we write a script that will replace them without introducing any issues in the code? The script cannot blindly delete them as following case can be an issue:

if ()
  some.code...
else
  System.out.println(...);
DB.close();

我正在考虑用';'替换它们。这将照顾上述情况。你还有其他问题吗?还有其他任何建议吗?

I'm thinking of replacing them with ';'. That will take care of above case. Do you see any other issues? Any other suggestions?

推荐答案

您是否认为这是个愚蠢的案例:

Have you consider the silly case:

System.out.println(" Print " +  object.changeState() );

我认为不会发生这种情况但是println可能会执行一个实际执行某些操作的方法系统所依赖的并且可能会引入微妙的错误(相信我或不相信,但我亲眼目睹了这一点)

I don't think it happen but chances are the println executes a method that is actually performing some action on which the system depends on and may introduce subtle bugs ( believe me or not, but I have witnessed this )

可能用记录器替换并禁用记录器可能会这样做。

Probably replacing with a logger and disabling the loggers may do.

或者使用NullObject模式创建一个空对象:

Or creating a null object using the NullObject pattern:

public final class DevNull { 
    public final static PrintStream out = new PrintStream(new OutputStream() {
        public void close() {}
        public void flush() {}
        public void write(byte[] b) {}
        public void write(byte[] b, int off, int len) {}
        public void write(int b) {}

    } );
}

并替换

 System.out.println();

使用

 DevNull.out.println();

这篇关于如何从代码库中删除System.out.println的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆