Monday, December 29th, 2008...10:32 am

Regular Expression to Replace System.out.println with Log4j

Jump to Comments

This is nothing fancy, but may help some of you log4j newbies.

I found myself replacing a project’s hideous System.out.println statements with log4j log calls.

Just do a File Search in eclipse , tick the regular expression box and do a Replace:

[code]
Find: System\.out\.println\(“(.+?)”\);
Replace: log.info(“\1″);
[/code]

Now you just have to go in and add your imports and your log field variable to your classes.

2 Comments

  • Nice idea, but it does not account for the all to common case of:
    System.out.println(myClassName + “my quoted text” +
    myVar1 +
    myVar2 +
    myVarN);

    So I tried this:
    System\.out\.println\((.+\R?){0,5}\);$

    which works fine… you ask why the {0,5}? because eclipse seems to choke on the greedier ‘+’ implementation. Perhaps a limitation in Eclipse.

  • Why don’t you just replace “System.out.println” with “logger.info”?

Leave a Reply