Log4j 2
  1. Log4j 2
  2. LOG4J2-133

Allow custom message creation via a message factory

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta3
    • Fix Version/s: 2.0-beta4
    • Component/s: API
    • Labels:
      None
    • Environment:

      Description

      My goal here is to be able to say:

      Logger log = LogManager.getLogger(Foo.class, new MyMessageFactory())

      Where MyMessageFactory extends a MessageFactory:

      and overrides:

      public Message newMessage(String message, Object... params)

      { return new ParameterizedMessage(message, params); }

      as:

      public Message newMessage(String message, Object... params)

      { return new StringFormattedMessage(message, params); }

      which would let me use native Java formatting instead of {} formatting /easily/

      I've attach the idea as the beginning of patch, which is missing the LogManager bits.

      Maybe we need a sub-interface of Message for param message, which would make me want to rename the class ParameterizedMessage to SimpleParameterizedMessage.

      1. log4j2-133.diff
        34 kB
        Gary Gregory
      2. LogManagerAPI.diff
        22 kB
        Gary Gregory

        Activity

        Hide
        Gary Gregory added a comment -

        Start of a patch for discussion.

        Show
        Gary Gregory added a comment - Start of a patch for discussion.
        Hide
        Ralph Goers added a comment -

        I like the concept as it makes sense that user's would want to use a particular style within the scope of a Logger. A few comments on the patch (also posted to the mailing list).

        First, I expected MessageFactory to be an interface, not a class.

        Second, I expected to see 3 MessageFactory implementations to start that would allow the user to choose between ParameterizedMessage, StringFormatMessage and MessageFormatMessage (I have that in the works for JUL support).

        Third, I expected that only the methods that accept a format and a parameter would use the MessageFactory and SimpleMessage and ObjectMessage would just be left as they are. That doesn't mean I couldn't be persuaded to include them in the interface as I could see uses customizing that for their needs.

        Lastly, I'm not sure how adding getThrowable to the Message interface is related or required for this change. I'm not necessarily opposed but I'm not sure if it should be tied to this.

        Show
        Ralph Goers added a comment - I like the concept as it makes sense that user's would want to use a particular style within the scope of a Logger. A few comments on the patch (also posted to the mailing list). First, I expected MessageFactory to be an interface, not a class. Second, I expected to see 3 MessageFactory implementations to start that would allow the user to choose between ParameterizedMessage, StringFormatMessage and MessageFormatMessage (I have that in the works for JUL support). Third, I expected that only the methods that accept a format and a parameter would use the MessageFactory and SimpleMessage and ObjectMessage would just be left as they are. That doesn't mean I couldn't be persuaded to include them in the interface as I could see uses customizing that for their needs. Lastly, I'm not sure how adding getThrowable to the Message interface is related or required for this change. I'm not necessarily opposed but I'm not sure if it should be tied to this.
        Hide
        Gary Gregory added a comment -

        Current version of the patch.

        Show
        Gary Gregory added a comment - Current version of the patch.
        Hide
        Ralph Goers added a comment -

        I recommend creating an AbstractMessageFactory as

        public abstract class AbstractMessageFactory implements MessageFactory {
        public Message newMessage(Object object)

        { return new ObjectMessage(object); }

        public Message new Message(String string)

        { return new StringMessage(object): }

        public abstract newMessage(String format, Object... params);
        }

        and then creating StringFormatMessageFactory and ObjectMessageFactory. Then change DefaultMessageFactory to extend the abstract class. You might want to rename DefaultMessageFactory to ParameterizedMessageFactory to be consistent with the other two factory classes.

        Show
        Ralph Goers added a comment - I recommend creating an AbstractMessageFactory as public abstract class AbstractMessageFactory implements MessageFactory { public Message newMessage(Object object) { return new ObjectMessage(object); } public Message new Message(String string) { return new StringMessage(object): } public abstract newMessage(String format, Object... params); } and then creating StringFormatMessageFactory and ObjectMessageFactory. Then change DefaultMessageFactory to extend the abstract class. You might want to rename DefaultMessageFactory to ParameterizedMessageFactory to be consistent with the other two factory classes.
        Hide
        Gary Gregory added a comment -

        Committed first batch of changes. Next up: Surface this in the LogManager API.

        Show
        Gary Gregory added a comment - Committed first batch of changes. Next up: Surface this in the LogManager API.
        Hide
        Gary Gregory added a comment -

        This is the second part of this feature: using the feature from the LogManager class.

        Show
        Gary Gregory added a comment - This is the second part of this feature: using the feature from the LogManager class.
        Hide
        Ralph Goers added a comment -

        Why does SimpleLoggerContext.getLogger() ignore the MessageFactory that is passed to it? Other than that it looks OK.

        Show
        Ralph Goers added a comment - Why does SimpleLoggerContext.getLogger() ignore the MessageFactory that is passed to it? Other than that it looks OK.
        Hide
        Gary Gregory added a comment -

        Fixed and committed.

        Show
        Gary Gregory added a comment - Fixed and committed.

          People

          • Assignee:
            Unassigned
            Reporter:
            Gary Gregory
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development