title: Seeking for a lightweight, but flexible solution to log method entry and exit (possibly by an exception) with log4j
body: <p>I need a lightweight solution, which would enable me to log method entry and/or exit and/or exception. I would like to be able to configure:</p>
  
  <ul>
  <li>The entry/exit/exception log level. For instance, I might have <code>Debug</code> level for exit, <code>Info</code> level for entry and <code>Error</code> level for exception.</li>
  <li>The entry/exit/exception log message. The log message should allow me to reference the method name, the method parameters and the method result.</li>
  </ul>
  
  <p>Let us take this method, for example:</p>
  
  <pre><code>public ObjectB MyFunc(int x, string s, ObjectA y)
  {
    // implementation
  }
  </code></pre>
  
  <p>Then, assuming <code>@Log</code> is such a magic annotation, one could have any of these <code>@Log</code> annotations on the <code>MyFunc</code> method:</p>
  
  <pre><code>@Log                   // should be a reasonable default
  @Log(EntryLevel = LogLevel.Info, ExitLevel = LogLevel.Debug) // makes sense to have  ExceptionLevel = LogLevel.Error by default
  @Log(EntryText = "{method}({methodArguments}) = ?", ExitText = "{method} = {methodResult}")
  @Log(EntryText = "{method}({@x}, {@s}, ...)")
  </code></pre>
  
  <p>Where:</p>
  
  <ul>
  <li><code>{method}</code> would be replaced by "MyFunc"</li>
  <li><code>{methodResult}</code> would be replaced by the <code>toString</code> of the method result</li>
  <li><code>{@x}</code> would be replaced by the <code>toString</code> of the <code>x</code> method argument</li>
  <li><code>{@s}</code> would be replaced by the <code>toString</code> of the <code>s</code> method argument</li>
  <li><code>{@y}</code> would be replaced by the <code>toString</code> of the <code>y</code> method argument</li>
  <li><code>{methodArguments}</code> would be replaced by something like <strong>"x: <code>{@x}</code>, s: <code>{@s}</code>, y: <code>{@y}</code>"</strong>, or <strong>"<code>{@x}</code>, <code>{@s}</code>, <code>{@y}</code>"</strong>, which could be governed by a boolean flag, like <code>includeArgumentName</code>.</li>
  </ul>
  
  <p>For the record, in .NET we use a modified (by us) version of the Log4PostSharp library, which does all of the above by injecting the right bytecode during the compilation phase. Hence, I have a pretty good idea what I am looking for, though I have absolutely no idea how to do it in Java and whether something like this has already been done.</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I would like to address the performance issue. The code may be abundant with the logging statements. Executing these statements must not incur any visible performance penalty when no actual logging is performed (due to log level constraints, for instance).</p>