title: Logging with optional parameters
body: <p>I have method where I want to add specific logging:</p>
  <pre><code>@Slf4j
  @Service
  public class SomethingService {
  
      public void doSomething(Something data, String comment, Integer limit) {
          Long id = saveSomethingToDatabase(data, comment);
          boolean sentNotification = doSomething(id);
          // ...
  
          // Log what you done.
          // Variables that always have important data: data.getName(), id
          // Variables that are optional: sentNotification, comment, limit 
          // (optional means they aren't mandatory, rarely contains essential data, often null, false or empty string).
      }
  }
  </code></pre>
  <p>I can simply log all:</p>
  <pre><code>log.info(&quot;Done something '{}' and saved (id {}, sentNotification={}) with comment '{}' and limit {}&quot;,
                  something.getName(), id, sentNotification, comment, limit);
  // Done something 'Name of data' and saved (id 23, sentNotification=true) with comment 'Comment about something' and limit 2
  </code></pre>
  <p>But most of the time most of the parameters are irrelevant. With the above I get logs like:</p>
  <pre><code>// Done something 'Name of data' and saved (id 23, sentNotification=false) with comment 'null' and limit null
  </code></pre>
  <p>That makes logs hard to read, long and unnecessarily complicated (in most cases other parameters aren't present).</p>
  <p>I want to handle all cases with preserving only essential data. Examples:</p>
  <pre><code>// Done something 'Name of data' and saved (id 23)
  // Done something 'Name of data' and saved (id 23) with comment 'Comment about something'
  // Done something 'Name of data' and saved (id 23) with limit 2
  // Done something 'Name of data' and saved (id 23) with comment 'Comment about something' and limit 2
  // Done something 'Name of data' and saved (id 23, sent notification)
  // Done something 'Name of data' and saved (id 23, sent notification) with limit 2
  // Done something 'Name of data' and saved (id 23, sent notification) with comment 'Comment about something'
  // Done something 'Name of data' and saved (id 23, sent notification) with comment 'Comment about something' and limit 2
  </code></pre>
  <p>I can code it by hand:</p>
  <pre><code>String notificationMessage = sentNotification ? &quot;, sent notification&quot; : &quot;&quot;;
  String commentMessage = comment != null ? String.format(&quot; with comment '%s'&quot;, comment) : &quot;&quot;;
  String limitMessage = &quot;&quot;;
  if (limit != null) {
      limitMessage = String.format(&quot;limit %s&quot;, limit);
      limitMessage = comment != null ? String.format(&quot; and %s&quot;, limitMessage) : String.format(&quot; with %s&quot;, limitMessage);
  }
  log.info(&quot;Done something '{}' and saved (id {}{}){}{}&quot;,
          something.getName(), id, notificationMessage, commentMessage, limitMessage);
  </code></pre>
  <p>But it's hard to write, hard to read, complicated and causes errors.</p>
  <p>I would like something like specify part of logs.</p>
  <p>Example pseudocode:</p>
  <pre><code>log.info(&quot;Done something '{}' and saved (id {} $notification) $parameters&quot;,
          something.getName(), id,
          $notification: sentNotification ? &quot;sent notification&quot; : &quot;&quot;,
          $parameters: [comment, limit]);
  </code></pre>
  <p>It should supports optional parameters, replace boolean/condition with given string, supports separating spaces, commas and words <code>with</code> and <code>and</code>.</p>
  <p>Maybe are there existing library for this? Or maybe is there at least a simpler way for coding this?</p>
  <p>If not, it remains for me nothing else to write my own library for messages to logging. Additionally, this kind of library will provide that all logs would be consistent.</p>
  <p>If you don't see a problem with three optional parameters, just imagine there are more (and you can't always pack them into a class - another class layer only for parameter logging cause even more complications).</p>
  <p>At the end, I know I can log each action separately. But with this I get many more logs and I won't have the most important information in one place. Other logs are in the <code>debug</code> level, not <code>info</code>.</p>