Log4j 2
  1. Log4j 2
  2. LOG4J2-561

Allow spaces around commas in Configuration's package attribute

    Details

      Description

      Can anyone please explain how to add a new(custom) appender..?
      here is my code it did not work..

      @Plugin(name = "EM_Appender", category = "Core", elementType = "appender", printObject = true)
      public final class EventManagerAppender extends AbstractAppender{

      protected EventManagerAppender(String name, Filter filter, Layout<? extends Serializable> layout,
      boolean ignoreExceptions)

      { super(name, filter, layout, ignoreExceptions); // TODO Auto-generated constructor stub }

      /* @SuppressWarnings("rawtypes")
      private EventManagerAppender(String name, Layout layout, Filter filter,
      boolean ignoreExceptions) {

      }*/

      @PluginFactory
      public static EventManagerAppender createAppender(@PluginAttribute("name") String name,
      @PluginAttribute("ignoreExceptions") String ignore,
      @PluginElement("Layout") Layout layout,
      @PluginElement("Filters") Filter filter) {

      boolean ignoreExceptions = Boolean.parseBoolean(ignore);
      if (name == null)

      { return null; }

      if (layout == null)

      { layout = PatternLayout.createLayout(null, null, null, null, ignore, ignore); }

      return new EventManagerAppender(name, filter,layout, ignoreExceptions);
      }

      public void append(LogEvent event)

      { System.out.println(event.getMessage()); System.out.println("Custome appender"); }

      }

      XML configuration
      **********************
      <?xml version="1.0" encoding="UTF-8"?>
      <Configuration status="warn" strict="true" name="XMLConfigTest"
      packages="org.apache.logging.log4j.test,com.mycompany.abc.def.log.wrapper" monitorInterval="30">
      <Properties>
      <Property name="filename">C:/Logs/test1.log</Property>
      </Properties>
      <Filter type="ThresholdFilter" level="warn"/>

      <Appenders>
      <Appender type="File" name="File" fileName="$

      {filename}

      ">
      <Layout type="PatternLayout">
      <Pattern>%d

      {dd MMM yyyy HH:mm:ss,SSS}

      %-5p [%c

      {1}:%L:%t] %m%n</Pattern>
      </Layout>
      </Appender>
      <EM_Appender name="EM_Appender">
      <Layout type="PatternLayout">
      <Pattern>%d{dd MMM yyyy HH:mm:ss,SSS} %-5p [%c{1}

      :%L:%t] %m%n</Pattern>
      </Layout>
      </EM_Appender>
      </Appenders>

      <Loggers>
      <Logger name="com.mycompany.xyz.ab" level="trace" additivity="false">
      <AppenderRef ref="File"/>
      </Logger>

      <Root level="trace">
      <AppenderRef ref="File"/>
      <AppenderRef ref="EM_Appender"/>
      </Root>
      </Loggers>

      </Configuration>

        Activity

        Hide
        Ralph Goers added a comment -

        I see that you have strict="true" and you specified the FileAppender in that format. With strict mode your Appender should be configured as

        <Appender type="EM_Appender" name="EM_Appender>
        
        Show
        Ralph Goers added a comment - I see that you have strict="true" and you specified the FileAppender in that format. With strict mode your Appender should be configured as <Appender type= "EM_Appender" name="EM_Appender>
        Hide
        vibin added a comment -

        unfortunately it did not work, i am not sure anywhere else in the class file i did a mistake.

        Show
        vibin added a comment - unfortunately it did not work, i am not sure anywhere else in the class file i did a mistake.
        Hide
        vibin added a comment - - edited

        Here is the call file:
        @Plugin(name = "EM_Appender", category = "Core", elementType ="appender", printObject = true)
        public final class EventManagerAppender extends AbstractAppender{

        protected EventManagerAppender(String name, Filter filter, Layout<? extends Serializable> layout,
        boolean ignoreExceptions)

        { super(name, filter, layout, ignoreExceptions); // TODO Auto-generated constructor stub }

        @PluginFactory
        public static EventManagerAppender createAppender(@PluginAttribute("name") String name,
        @PluginAttribute("ignoreExceptions") String ignore,
        @PluginElement("Layout") Layout layout,
        @PluginElement("Filters") Filter filter) {

        boolean ignoreExceptions = Boolean.parseBoolean(ignore);

        if (name == null)

        { return null; }

        if (layout == null)

        { layout = PatternLayout.createLayout(null, null, null, null, ignore, ignore); }

        return new EventManagerAppender(name, filter,layout, ignoreExceptions);
        }

        public void append(LogEvent event)

        { System.out.println(event.getMessage()); System.out.println("Custom appender"); }

        }

        Show
        vibin added a comment - - edited Here is the call file: @Plugin(name = "EM_Appender", category = "Core", elementType ="appender", printObject = true) public final class EventManagerAppender extends AbstractAppender{ protected EventManagerAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions) { super(name, filter, layout, ignoreExceptions); // TODO Auto-generated constructor stub } @PluginFactory public static EventManagerAppender createAppender(@PluginAttribute("name") String name, @PluginAttribute("ignoreExceptions") String ignore, @PluginElement("Layout") Layout layout, @PluginElement("Filters") Filter filter) { boolean ignoreExceptions = Boolean.parseBoolean(ignore); if (name == null) { return null; } if (layout == null) { layout = PatternLayout.createLayout(null, null, null, null, ignore, ignore); } return new EventManagerAppender(name, filter,layout, ignoreExceptions); } public void append(LogEvent event) { System.out.println(event.getMessage()); System.out.println("Custom appender"); } }
        Hide
        vibin added a comment - - edited

        Error message:
        2014-03-04 11:33:43,108 ERROR Appenders contains an invalid element or attribute "EM_Appender"
        2014-03-04 11:33:43,111 ERROR Unable to locate appender EM_Appender for logger

        Show
        vibin added a comment - - edited Error message: 2014-03-04 11:33:43,108 ERROR Appenders contains an invalid element or attribute "EM_Appender" 2014-03-04 11:33:43,111 ERROR Unable to locate appender EM_Appender for logger
        Hide
        Ralph Goers added a comment -

        Your code looks fine. I assume it is in package "com.mycompany.abc.def.log.wrapper"? If not you need to make sure the package it is in is listed in the packages attribute on the configuration.

        You can also set status="debug" on the configuration. That should give you more information on why it is not working.

        Show
        Ralph Goers added a comment - Your code looks fine. I assume it is in package "com.mycompany.abc.def.log.wrapper"? If not you need to make sure the package it is in is listed in the packages attribute on the configuration. You can also set status="debug" on the configuration. That should give you more information on why it is not working.
        Hide
        Ralph Goers added a comment - - edited

        The Error message is saying what I said in my first reply. You have

        <EM_Appender name="EM_Appender">
        

        but with strict="true" it needs to be

        <Appender type="EM_Appender" name="EM_Appender">
        
        Show
        Ralph Goers added a comment - - edited The Error message is saying what I said in my first reply. You have <EM_Appender name= "EM_Appender" > but with strict="true" it needs to be <Appender type= "EM_Appender" name= "EM_Appender" >
        Hide
        vibin added a comment -

        Thanks, it worked. i think issue was with package..

        Show
        vibin added a comment - Thanks, it worked. i think issue was with package..
        Hide
        vibin added a comment - - edited

        If you put a space in between the packages attribute value( packages=".."), doesn't work.
        For example:

        <Configuration status="warn" strict="true" name="xyz"
        packages="com.mycompany.cdef.ghi,<Space here doesn't work >com.mycompany.jkl.mno" monitorInterval="30">

        Show
        vibin added a comment - - edited If you put a space in between the packages attribute value( packages=".."), doesn't work. For example: <Configuration status="warn" strict="true" name="xyz" packages="com.mycompany.cdef.ghi,<Space here doesn't work >com.mycompany.jkl.mno" monitorInterval="30">
        Hide
        Gary Gregory added a comment -

        Thank you for the suggestion. Please test with your configuration. You'll have to download and build the code from trunk.

        commit -m "<action issue="LOG4J2-561" dev="ggregory" type="update" due-to="vibin">..." (7 paths specified)
            Sending        C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
            Sending        C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
            Sending        C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
            Sending        C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java
            Adding         C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Patterns.java
            Sending        C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/AnsiEscape.java
            Sending        C:/vcs/svn/apache/log4j2/trunk/src/changes/changes.xml
            Transmitting file data ...
            Committed revision 1574176.
        
        Show
        Gary Gregory added a comment - Thank you for the suggestion. Please test with your configuration. You'll have to download and build the code from trunk. commit -m "<action issue="LOG4J2-561" dev="ggregory" type="update" due-to="vibin">..." (7 paths specified) Sending C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java Sending C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java Sending C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java Sending C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java Adding C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/helpers/Patterns.java Sending C:/vcs/svn/apache/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/AnsiEscape.java Sending C:/vcs/svn/apache/log4j2/trunk/src/changes/changes.xml Transmitting file data ... Committed revision 1574176.

          People

          • Assignee:
            Gary Gregory
            Reporter:
            vibin
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development