You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback's architecture.

Information

Problems

  1. New package
    • org.apache.log4j changed to org.apache.logging.log4j

    • We may need to change in a bunch of modules when we try to convert to the new package


  2. DOM and Property Configurator was removed
  3. Log4J 2 over the old fashioned properties file is not supported so far
  4. API for setLevel / getEffectiveLevel - http://apache-logging.6191.n7.nabble.com/API-and-setLevel-getEffectiveLevel-td36238.html
  5. Unit-testing
    1. Add Appender

      Writer loggerOut = new StringWriter();
      final Layout layout = new EnhancedPatternLayout(EnhancedPatternLayout.TTCC_CONVERSION_PATTERN);
      Logger.getRootLogger().setLevel(Level.INFO); // Other tests might have set this to silent
      Logger.getRootLogger().addAppender(new WriterAppender(layout, loggerOut));

       

    2. Custom Appender

       
      public static class TestAppender extends AppenderSkeleton {
          public List<LoggingEvent> events = new ArrayList<>();
      
          @Override
          public void close() {
          }
      
          @Override
          public boolean requiresLayout() {
              return false;
          }
      
          @Override
          protected void append(LoggingEvent event) {
              events.add(event);
          }
      }

      Need to rewrite as a plugin.

    3. Custom Level

      public class LoggingLevel extends Level {
      
          public static final LoggingLevel AUDIT_TRAIL = new LoggingLevel(99, "AUDIT_TRAIL", 0);
      
          protected LoggingLevel(int level, String levelStr, int syslogEquivalent) {
              super(level, levelStr, syslogEquivalent);
          }
      
          public static Level toLevel(String sArg) {
              return AUDIT_TRAIL;
          }
      
          public static Level toLevel(int val) {
              return AUDIT_TRAIL;
          }
      
      }
  6. Log Tools can not start due to it relies on Log4j

    private AbstractBeanContainer populateContainer() {
        BeanItemContainer<LogLevelBean> container = new BeanItemContainer<>(LogLevelBean.class);
        Enumeration<Logger> loggers = logManager.getCurrentLoggers();
    
        while (loggers.hasMoreElements()) {
            Logger logger = loggers.nextElement();
            LogLevelBean logLevelBean = new LogLevelBean(
                    logger.getName(),
                    logger.getEffectiveLevel(),
                    // if the level is null, then that means the effectiveLevel is inherited from the parent,
                    // rather than copied from the level itself. therefore, we want to show the inherited tick
                    // (inherited = true)
                    logger.getLevel() == null
            );
    
            container.addBean(logLevelBean);
        }
    
        container.setItemSorter(new DefaultItemSorter() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((LogLevelBean) o1).getName().compareTo(((LogLevelBean) o2).getName());
            }
        });
    
        return container;
    }


 

Code Snippets

 

    public static void updateLoggers(final Appender appender, final Configuration config) {
        final Level level = null;
        final Filter filter = null;
        for (final LoggerConfig loggerConfig : config.getLoggers().values()) {
            loggerConfig.addAppender(appender, level, filter);
        }
        config.getRootLogger().addAppender(appender, level, filter);
    }
    /**
     * http://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2
     */
    public static void setLevel(Logger logger, Level level) {
        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final Configuration config = ctx.getConfiguration();

        LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
        LoggerConfig specificConfig = loggerConfig;

        // We need a specific configuration for this logger,
        // otherwise we would change the level of all other loggers
        // having the original configuration as parent as well

        if (!loggerConfig.getName().equals(logger.getName())) {
            specificConfig = new LoggerConfig(logger.getName(), level, true);
            specificConfig.setParent(loggerConfig);
            config.addLogger(logger.getName(), specificConfig);
        }
        specificConfig.setLevel(level);
        ctx.updateLoggers();
    }

 

 

 

  • No labels