System.out.println removal/comment from multiple java source files

后端 未结 7 1867
失恋的感觉
失恋的感觉 2021-01-13 11:08

I want to remove/comment all the occurrences of System.out.println from my java code. This System.out.println may be inside if ,

7条回答
  •  时光取名叫无心
    2021-01-13 11:47

    You could use raw find/replace functionality in source code files to comment out (or remove) statements, but whatever regular expression can be easily broken, so maybe you'd better be aware of logging frameworks like log4j or slf4j.

    Big picture first: instead of using the usual System.out.println(), you'll end up using a line of code like:

    logger.debug("Just entered main");
    

    The logging framework can be configured with a simple property file, so you can have multiple appenders (console, file, database, whatever) and shut down each one separately on demand (for example the console appender). To switch to a logging API you still have to perform raw find/replace on source files, and possibly fix a couple of things by hand, either whithin your IDE, or with a command like:

    find src/ -name '*.java' | \
    xargs sed -i -e 's/System.out.println/logger.verbose/g'
    

提交回复
热议问题