Logger 3.0.0: printf support

One of the most debated topics for the Logger's 3.0.0 release was to add support for simple substitution strings. There were numerous requests to have support similar to C's printf procedure.

Since C's printf only prints a message (and not return one) we named this function sprintf which is what C and PHP call it.

The spec for sprintf is:

function sprintf(
  p_str in varchar2,
  p_s1 in varchar2 default null,
  p_s2 in varchar2 default null,
  p_s10 in varchar2 default null)
  return varchar2

sprintf takes in a string and will replace substitution strings with p_s<n>. It uses the following rules for string substitution:

  • Replaces %s<n> with p_s<n>
  • Occurrences of %s (no number) are replaced with p_s1..p_s10 in order that they appear in text
  • %% is escaped to %

The following example demonstraints all of the rules:

select logger.sprintf('%s %s2 %s1 %s %%', 'one', 'two', 'three') sprintf
from dual;

one two one two %

You can use sprintf to enhance your log messages with whatever variations you need. The following example shows a very basic usecase for sprintf:

  logger.log(logger.sprintf('hello %s', 'Martin'));

select id, text
from logger_logs_5_min;

---- -------------
 829 hello Martin

Unlike most of the methods in Logger, logger.sprintf is not affected by the current logging level nor if Logger is being run in no_op mode. This is because we think that developers will use it for other purposes besides logging.