I'm using String.format method of Java API to log something. My method is like:
public static void log(String message, Object... params){
System.out.println(String.format(message, params));
}
However the problem is, if the user sends a message that has % character somewhere in it, it throws exception. Here's a scenario:
log("SELECT * FROM my WHERE name like '%six%'");
and Java looks for something to replace %s (that's ok) and %' (oops). I want to fix that. Because there there are no params and %s will be lost and %' causes exception.
One solution can be message.replace("%", "%%") but I'm not sure it is an elegant solution or not.
%swould be OK if%isn't. If they give you broken input they can't expect working output. Just throw an exception.likeclause as a broken input.%sand%, and one is considered OK and the other is not, then the input is broken. If the input is a format string forString.format()and you don't want the%sin thelikeclaused to be replaced by a parameter value, anything but%%sand%%in the input is broken.