Uploaded image for project: 'SLF4J'
  1. SLF4J
  2. SLF4J-490

Better combination for logging and exceptions

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor Minor
    • 2.0.17
    • 1.7.29
    • Core API
    • None

    Description

      Hi,
       
      we have quite a few places in our code where we do:   

      logger.error("Param {} must be in [{}-{}]", name, low, high);
      throw new ValidationException("Param " + name + " must be in [" + low + "-" + high + "]"); 

      This is obviously ugly.  Other options would be to use

      String msg = String.format("Param %s must be in [%s-%s]", name, low, high);
      logger.error(msg);
      throw new ValidationException(msg);

        or

      String msg = MessageFormatter.format("Param {} must be in [{}-{}]", new Object[] {name, low, high}).getMessage();
      logger.error(msg);
      throw new ValidationException(msg);  

       Both are not ideal.  Can't we have a `Logger.format` method which returns a `FormattingTuple` w/o the explicit array creation
      and allow `Logger.error` etc. to be called with a `FormattingTuple`?  Then I could write

      FormattingTuple entry = logger.format("Param {} must be in [{}-{}]", name, low, high);
      logger.error(entry);
      throw new ValidationException(entry.getMessage());

       
      For my own exception classes I could then even offer a constructor that takes a FormattingTuple and internally use the message and the throwable (if it is not null).

      Bonus: pick a nicer name than `FormattingTuple` like `LogEntry`.

      Attachments

        Activity

          People

            slf4j-dev SLF4J developers list
            nkiesel Norbert Kiesel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: