Log4j 2
  1. Log4j 2
  2. LOG4J2-730

Log4jContextFactory selector discovery logic is not extensible

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0
    • Fix Version/s: 2.0.2
    • Component/s: Core
    • Labels:
      None

      Description

      I want to write my own LoggerContextFactory which forces my specific instance of ContextSelector and sets some system properties.

      I'd like to extend Log4jContextFactory class to just add this logic and reuse the rest of the code, however that doesn't seem possible since the selector attribute is private and the piece of code that fetches it is embedded in the constructor.

      I think it makes sense to have the selector as a private field, but if the code to fetch it could be a protected one so that it can be overridden, that would enable use cases like this.

      Thanks

        Activity

        Hide
        Mariano Gonzalez added a comment -

        Yeah! This does it. Thanks!

        Show
        Mariano Gonzalez added a comment - Yeah! This does it. Thanks!
        Hide
        Remko Popma added a comment -

        Fixed in revision 1616231.
        Please verify and close.

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

        Would it work if we changed the Log4jContextFactory constructor to the code below?

        Your custom factory could then call super(createMyCustomSelector()); in its constructor.

        public Log4jContextFactory() {
            this(createSelector());
        }
        
        public Log4jContextFactory(ContextSelector selector) {
            this.selector = selector;
        }
        
        private static ContextSelector createSelector() {
            final String sel = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR);
            if (sel != null) {
                try {
                    return Loader.newCheckedInstanceOf(sel, ContextSelector.class);
                } catch (final Exception ex) {
                    LOGGER.error("Unable to create context {}", sel, ex);
                }
            }
            return new ClassLoaderContextSelector();
        }
        
        Show
        Remko Popma added a comment - Would it work if we changed the Log4jContextFactory constructor to the code below? Your custom factory could then call super(createMyCustomSelector()); in its constructor. public Log4jContextFactory() { this (createSelector()); } public Log4jContextFactory(ContextSelector selector) { this .selector = selector; } private static ContextSelector createSelector() { final String sel = PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR); if (sel != null ) { try { return Loader.newCheckedInstanceOf(sel, ContextSelector.class); } catch ( final Exception ex) { LOGGER.error( "Unable to create context {}" , sel, ex); } } return new ClassLoaderContextSelector(); }

          People

          • Assignee:
            Remko Popma
            Reporter:
            Mariano Gonzalez
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development