Log4j 2
  1. Log4j 2
  2. LOG4J2-631

Update docs to clarify how to use formatter logger and standard logger together

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-rc1
    • Fix Version/s: 2.0
    • Component/s: Documentation
    • Labels:
      None

      Description

      public class Example {
      
      	private final static Logger FORMATTER_LOGGER = LogManager.getFormatterLogger(Example.class);
      
      	private final static Logger LOGGER = LogManager.getLogger(Example.class);
      
      	public static void main(String[] args) {
      		LOGGER.log(ERROR, "{} happened", "Something");
      		FORMATTER_LOGGER.log(ERROR, "%s happened", "Something");
      	}
      }
      
      13:40:35.401 [main] ERROR com.example.Example - {} happened
      13:40:35.404 [main] ERROR com.example.Example - Something happened
      

      After inverting the two constant declarations:

      13:41:13.730 [main] ERROR com.example.Example - Something happened
      13:41:13.732 [main] ERROR com.example.Example - %s happened
      

        Activity

        Hide
        Remko Popma added a comment -

        Fixed in revision 1609755.
        Please verify and close.

        Show
        Remko Popma added a comment - Fixed in revision 1609755. Please verify and close.
        Hide
        Remko Popma added a comment -

        Changed title to reflect the remaining task.

        Show
        Remko Popma added a comment - Changed title to reflect the remaining task.
        Hide
        Matt Sicker added a comment -

        I'd also like to point out that the formatter logger is a lot slower than than normal one.

        Show
        Matt Sicker added a comment - I'd also like to point out that the formatter logger is a lot slower than than normal one.
        Hide
        Alexandre Gattiker added a comment -

        Remko Popma thanks a lot, that's exactly what I needed! Perhaps these methods might be documented on http://logging.apache.org/log4j/2.x/manual/api.html before or even rather than the use of getFormatterLogger.

        Show
        Alexandre Gattiker added a comment - Remko Popma thanks a lot, that's exactly what I needed! Perhaps these methods might be documented on http://logging.apache.org/log4j/2.x/manual/api.html before or even rather than the use of getFormatterLogger.
        Hide
        Remko Popma added a comment -

        Alexandre, are you aware of this option: you can get a standard logger (with the standard MessageFactory), but for the occasional log statement where you want to use a formatter message format, you can call one of the Logger.printf methods.

        For example:

        Logger logger = LogManager.getLogger(MyClass.class);
        
        logger.info("My {} parameterized message", "standard");
        logger.printf(Level.INFO, "Special formatting: int maxval=%+,d, pi=%10.3f", Integer.MAX_VALUE, Math.PI);
        
        Show
        Remko Popma added a comment - Alexandre, are you aware of this option: you can get a standard logger (with the standard MessageFactory), but for the occasional log statement where you want to use a formatter message format, you can call one of the Logger.printf methods. For example: Logger logger = LogManager.getLogger(MyClass.class); logger.info( "My {} parameterized message" , "standard" ); logger.printf(Level.INFO, "Special formatting: int maxval=%+,d, pi=%10.3f" , Integer .MAX_VALUE, Math .PI);
        Hide
        Alexandre Gattiker added a comment -

        Matt Sicker, could we key by both name and MessageFactory?

        My use case is that I have a code base that uses the standard MessageFactory, but I would like to introduce one logging statement that can benefit from FormatterLogger. It is very impractical to have a mix of various log formatters over a code base, so my idea was to use the a different name for an additional, 'non-standard' logger.

        If this limitation is to remain, I think it is only practical to use the formatter logger exclusively and uniformly over a code base. But then, as it is vastly superior to the standard formatter, why not advertise it as the recommended usage in Log4j2? It is no more difficult to write %s than {}.

        Gary Gregory, I agree it would make sense to document this limitation where getFormatterLogger is introduced in http://logging.apache.org/log4j/2.x/manual/api.html

        Show
        Alexandre Gattiker added a comment - Matt Sicker , could we key by both name and MessageFactory? My use case is that I have a code base that uses the standard MessageFactory, but I would like to introduce one logging statement that can benefit from FormatterLogger. It is very impractical to have a mix of various log formatters over a code base, so my idea was to use the a different name for an additional, 'non-standard' logger. If this limitation is to remain, I think it is only practical to use the formatter logger exclusively and uniformly over a code base. But then, as it is vastly superior to the standard formatter, why not advertise it as the recommended usage in Log4j2? It is no more difficult to write %s than {}. Gary Gregory , I agree it would make sense to document this limitation where getFormatterLogger is introduced in http://logging.apache.org/log4j/2.x/manual/api.html
        Hide
        Gary Gregory added a comment -

        This is unfortunate but I think we should leave this be for now.

        Perhaps some documentation is needed here.

        If you really want something like that, we could document a patten like:

        private final static Logger FORMATTER_LOGGER = LogManager.getFormatterLogger(Example.class.getName() + ".Formatted");
        private final static Logger LOGGER = LogManager.getLogger(Example.class);
        
        Show
        Gary Gregory added a comment - This is unfortunate but I think we should leave this be for now. Perhaps some documentation is needed here. If you really want something like that, we could document a patten like: private final static Logger FORMATTER_LOGGER = LogManager.getFormatterLogger(Example.class.getName() + ".Formatted" ); private final static Logger LOGGER = LogManager.getLogger(Example.class);
        Hide
        Matt Sicker added a comment -

        Looking at AbstractLoggerProvider in log4j-api, the MessageFactory is a private final field. This is done at construction, and since Loggers are keyed by name, you can't use multiple loggers with the same name and different MessageFactory instances. If you were to allow having two named loggers with different message factories, what would LogManager.getLogger("name") do? You have two potential Logger objects to get. I don't think this makes any sense. Probably invalid.

        Show
        Matt Sicker added a comment - Looking at AbstractLoggerProvider in log4j-api, the MessageFactory is a private final field. This is done at construction, and since Loggers are keyed by name, you can't use multiple loggers with the same name and different MessageFactory instances. If you were to allow having two named loggers with different message factories, what would LogManager.getLogger("name") do? You have two potential Logger objects to get. I don't think this makes any sense. Probably invalid.

          People

          • Assignee:
            Remko Popma
            Reporter:
            Alexandre Gattiker
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development