Log4j 2
  1. Log4j 2
  2. LOG4J2-85

Replace ThreadContext.push(String msg) with push(String msg, Object args...)

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0-beta2
    • Component/s: None
    • Labels:
      None

      Description

      Provide the option to use similar message formatting like the usual log methods. The API is still alpha so we can safely replace the method.

      If NDC functionality would be disabled (see LOG4J2-84) then message formatting wouldn't be performed at all, thus being significantly faster than always composing the message.

        Activity

        Hide
        Ralph Goers added a comment -

        As always, patches are welcome. And in your case, encouraged!

        Show
        Ralph Goers added a comment - As always, patches are welcome. And in your case, encouraged!
        Hide
        Ralph Goers added a comment -

        I'm not sure how to do this as it would require encapsulating the msg and args into another object on the stack. In that case, what would pop return? Are you really wanting push(Message)?

        Show
        Ralph Goers added a comment - I'm not sure how to do this as it would require encapsulating the msg and args into another object on the stack. In that case, what would pop return? Are you really wanting push(Message)?
        Hide
        Joern Huxhorn added a comment -

        The evaluation/formatting of the message should be performed immediately before adding it as a String to the backend collection.

        This is a convenience feature for the user since a message pattern with placeholders is nicer to write than a list of concatenated strings, especially if being accustomed to it by writing the same patterns in log messages.

        Beside the added convenience this has a performance impact if the NDC is disabled entirely. In that case, no formatting would be performed at all.

        I implemented an NDC like that over at https://github.com/huxi/lilith/blob/master/logback/classic/src/main/java/de/huxhorn/lilith/logback/classic/SimpleNDCAdapter.java
        (It keeps the message pattern and arguments in separate ThreadLocals to prevent a classloader leak. That way only standard java classes are put into a ThreadLocal.)

        Pop (in your case) would always just return the formatted string.

        Show
        Joern Huxhorn added a comment - The evaluation/formatting of the message should be performed immediately before adding it as a String to the backend collection. This is a convenience feature for the user since a message pattern with placeholders is nicer to write than a list of concatenated strings, especially if being accustomed to it by writing the same patterns in log messages. Beside the added convenience this has a performance impact if the NDC is disabled entirely. In that case, no formatting would be performed at all. I implemented an NDC like that over at https://github.com/huxi/lilith/blob/master/logback/classic/src/main/java/de/huxhorn/lilith/logback/classic/SimpleNDCAdapter.java (It keeps the message pattern and arguments in separate ThreadLocals to prevent a classloader leak. That way only standard java classes are put into a ThreadLocal.) Pop (in your case) would always just return the formatted string.
        Hide
        Ralph Goers added a comment -

        OK - So push(String msg, Object args...) is just a wrapper around push(String msg). As I said in LOG4J2-83 and LOG4J2-84, I am not convinced that disabling the NDC itself is a good idea.

        Show
        Ralph Goers added a comment - OK - So push(String msg, Object args...) is just a wrapper around push(String msg). As I said in LOG4J2-83 and LOG4J2-84 , I am not convinced that disabling the NDC itself is a good idea.
        Hide
        Ralph Goers added a comment -

        I added a new push(String format, Object... args) method in revision 1403143. Please verify and close.

        Show
        Ralph Goers added a comment - I added a new push(String format, Object... args) method in revision 1403143. Please verify and close.
        Hide
        Joern Huxhorn added a comment -

        Fixed, thanks!

        Show
        Joern Huxhorn added a comment - Fixed, thanks!

          People

          • Assignee:
            Ralph Goers
            Reporter:
            Joern Huxhorn
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development