-1

I need to change the logging design in my application. Currently my application uses a utility class to all the all the errors, info and debug statement. I need to change that. For example I need to change all the occurrences of

Log.send("XXX", Log.DEBUG); 

to

logger.debug("XXX"); 

and

Log.send("XXX", Log.INFO); 

to

logger.info("XXX");  or

Can I do it using any tool or using regular expression in editors like Notepad++ or textpad?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 4
    Your IDE should be able to do this. What are you using? Eclipse? Netbeans? IDEA? Other...? – Jim Garrison Jun 28 '16 at 01:03
  • I want to understand Why dont you prefer writing logger. info or logger. debug inside static function Log. send based on different values of Log. INFO or Log. DEBUG – Gaurava Agarwal Jun 28 '16 at 01:47

3 Answers3

1

Using a regex search and find (I'm using atom's style):

FIND: Log.send\((\"\w+\"), Log.DEBUG\);

REPLACE: logger.debug($1)

FIND: Log.send\((\"\w+\"), Log.INFO\);

REPLACE: logger.info($1)

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
0

In Notepad++ you can use regex like so

logger.debug

Find:

Log\.send\("([^"]+)", Log\.DEBUG\);

Replace:

logger\.debug\("\1"\);

logger.info

Find:

Log\.send\("([^"]+)", Log\.INFO\);

Replace:

logger\.info\("\1"\);

I tested this in eclipse and it works. For Netbeans, just replace \1 with $1. If you want to be lazy and just use one regex find-and-replace match for the two, then you can use this:

Find:

Log\.send\("([^"]+)", Log\.([^\)]+)\);

Replace

logger\.\L\2\E\("\1"\);

The difference in this is that I capture DEBUG and INFO too, then use them as the method name for the logger. However, the keywords are in upper case, so I put them to lowercase first (hence, the \L and \E to cut off the lower case.

Community
  • 1
  • 1
Keale
  • 3,924
  • 3
  • 29
  • 46
  • @user3669515 no problem. If this answered your question, kindly accept it by clicking on the green check mark so that we can close this question. Cheers~ – Keale Jun 29 '16 at 04:56
  • HI Keale,Thanks a lot for your earlier help . Can you please help in the regular expression for the below one Log.send("XXX" + xyz, Log.DEBUG); to logger.debug("XXX" + xyz); – user3669515 Jul 01 '16 at 05:11
0

If you have access to vi on cygwin(windows) or shell (os x or linux), or even other linux commands should make this very simple. Its another way of doing this. Also you don't have to open each file, if you do it right it should be one command on shell for the entire project source code.

Example: The below code looks for test search string for all files in current directory (recursively) and replaces it with 'replacetext'

grep -r -l 'test' . | sort | uniq | xargs perl -e "s/test/replacetext/" -pi
Razor Tag
  • 36
  • 4