Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-3237

Log4j 1.2 bridge API hard codes the Syslog protocol to TCP

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Blocker
    • Resolution: Fixed
    • 2.16.0
    • 2.17.1
    • API, Appenders
    • None

    Description

      Log4j 1.2 bridge API hard codes protocol to TCP and host address and port to localhost:514

       

       

       -Dlog4j1.compatibility=true -jar 

      backtrace :-

       

       

      2021-12-16 00:35:32,904 main ERROR TcpSocketManager (TCP:localhost:514) caught exception and will continue: java.io.IOException: Unable to create socket for localhost at port 514 using ip addresses and ports , 0:0:0:0:0:0:0:1:514
      at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createSocket(TcpSocketManager.java:509)
      at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createManager(TcpSocketManager.java:478)
      at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createManager(TcpSocketManager.java:459)
      at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:113)
      at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
      at org.apache.logging.log4j.core.net.TcpSocketManager.getSocketManager(TcpSocketManager.java:202)
      at org.apache.logging.log4j.core.appender.SocketAppender.createSocketManager(SocketAppender.java:443)
      at org.apache.logging.log4j.core.appender.SocketAppender$Builder.build(SocketAppender.java:221)
      at org.apache.log4j.builders.appender.SyslogAppenderBuilder.createAppender(SyslogAppenderBuilder.java:150)
      at org.apache.log4j.builders.appender.SyslogAppenderBuilder.parseAppender(SyslogAppenderBuilder.java:121)
      at org.apache.log4j.builders.BuilderManager.parseAppender(BuilderManager.java:76)
      at org.apache.log4j.config.PropertiesConfiguration.parseAppender(PropertiesConfiguration.java:427)
      at org.apache.log4j.config.PropertiesConfiguration.parseLogger(PropertiesConfiguration.java:405)
      at org.apache.log4j.config.PropertiesConfiguration.configureRoot(PropertiesConfiguration.java:325)
      at org.apache.log4j.config.PropertiesConfiguration.doConfigure(PropertiesConfiguration.java:302)
      at org.apache.log4j.config.PropertiesConfiguration.doConfigure(PropertiesConfiguration.java:92)
      at org.apache.log4j.config.Log4j1Configuration.initialize(Log4j1Configuration.java:59)
      at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:289)
      at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:626)
      at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:699)
      at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:716)
      at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:270)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:155)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
      at org.apache.logging.log4j.LogManager.getContext(LogManager.java:309)
      at org.apache.log4j.Logger$PrivateManager.getContext(Logger.java:59)
      at org.apache.log4j.Logger.getLogger(Logger.java:37)
      at test.logger.Example.<clinit>(Example.java:12)
      Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:607)
      at org.apache.logging.log4j.core.net.TcpSocketManager.createSocket(TcpSocketManager.java:409)
      at org.apache.logging.log4j.core.net.TcpSocketManager$TcpSocketManagerFactory.createSocket(TcpSocketManager.java:504)
      ... 27 more
      

       

       

      Relevant code   SyslogAppenderBuilder.java :-

       

       {{@Override
      public Appender parseAppender(final String name, final String appenderPrefix, final String layoutPrefix,
      final String filterPrefix, final Properties props, final PropertiesConfiguration configuration) {
      Filter filter = configuration.parseAppenderFilters(props, filterPrefix, name);
      Layout layout = configuration.parseLayout(layoutPrefix, name, props);
      String level = getProperty(THRESHOLD_PARAM);
      String facility = getProperty(FACILITY_PARAM, "LOCAL0");
      String syslogHost = getProperty(SYSLOG_HOST_PARAM, "localhost:514");
      return createAppender(name, configuration, layout, facility, filter, syslogHost, level);
      }
      private Appender createAppender(final String name, final Log4j1Configuration configuration, Layout layout,
      String facility, final Filter filter, final String syslogHost, final String level) {
      Holder<String> host = new Holder<>();
      Holder<Integer> port = new Holder<>();
      resolveSyslogHost(syslogHost, host, port);
      org.apache.logging.log4j.core.Layout appenderLayout;
      if (layout instanceof LayoutWrapper)
      { appenderLayout = ((LayoutWrapper) layout).getLayout(); }
      else if (layout != null) {
      appenderLayout = new LayoutAdapter(layout);
      } else {
      appenderLayout = SyslogLayout.newBuilder()
      .setFacility(Facility.toFacility(facility))
      .setConfiguration(configuration)
      .build();
      }
      org.apache.logging.log4j.core.Filter fileFilter = buildFilters(level, filter);
      return new AppenderWrapper(SyslogAppender.newBuilder()
      .setName(name)
      .setConfiguration(configuration)
      .setLayout(appenderLayout)
      .setFilter(fileFilter)
      .withPort(port.get())
      .withProtocol(Protocol.TCP)
      .withHost(host.get())
      .build());
      }}}
      

      log4j.properties file 

       

       log4j.rootLogger=DEBUG,SYSLOG
      log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
      log4j.appender.SYSLOG.Threshold=DEBUG
      log4j.appender.SYSLOG.syslogHost=10.133.68.197
      log4j.appender.SYSLOG.port=514
      log4j.appender.SYSLOG.protocol=UDP
      log4j.appender.SYSLOG.header=true
      log4j.appender.SYSLOG.Facility=LOCAL3
      log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
      log4j.appender.SYSLOG.layout.conversionPattern=Main[%pid] :%t: %c %-4p - %m\n
      

       

       

      Attachments

        Activity

          People

            ggregory Gary D. Gregory
            TukeshK Tukesh
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: