开发者

Java translator for println usages to convenient logger

I often see (and reuse) 3rd party source code that doesn't have appropriate output. Is there any tool (code translator) that convert println output to suitable log framework code

private void processCreateTraining() {
    System.out.println("Training set created");
    //..
}

to something like

private void processCreateTraining() {
    LOG.info("Traini开发者_如何转开发ng set created");
    //..
}

It could be done by search-and-replace or Structural Replace in IDEA. But is there more sophisticated/robust solution that provide more flexibility: different logging framework support, ask severity on occurrence replace, string concatenation via StringBuilder.


If I come across third party libraries that use println all over the place, I tend NOT to use it. It is a sign of poor workmanship, and that implies (to me) that there are likely to be other more insidious problems with the code.

But no, I'm not aware of any plugin or tool to deal specifically with this case.


Sure, try something like this:

public static void main(String[] args) {
    WrappedOutputStream los = new WrappedOutputStream();
    System.setOut(new PrintStream(los, true));
    System.out.println("catch me");
}

WrappedOutputStream.java

class WrappedOutputStream extends ByteArrayOutputStream { 
    public void flush() throws IOException {
        String str = "WRAPPED: " + this.toString();
        // process
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜