Perhaps the simplest thing to do is to add the packages attribute to your 
log4j2.xml configuration file. Set the value to the package of your custom 
plugin. 

<Configuration status="trace" packages="com.mycomp.pluginpackage">
...

Remko

Sent from my iPhone

> On 2015/08/28, at 6:42, Nicholas Duane <nic...@msn.com> wrote:
> 
> 
> 
> 
> I don't think the implementation is the issue, though if/when I get my plugin 
> loading and it doesn't work I will certainly try to compare against what Gary 
> wrote.  Right now I'm just trying to figure out how to get my plugin loaded.  
> I've got my class file, LevelRangeFilter.java which I compiled to a .class 
> file.  It resides in the same directory as my HelloWorld.class file which 
> contains my "main".  I was hoping my plugin would get loaded but it doesn't 
> seem to be.  What's the easiest way for me to get it loaded?  Right now when 
> I run my program I'm seeing the following error from log4net:
> 
> 2015-08-27 15:23:09,682 ERROR File contains an invalid element or attribute 
> "LevelRangeFilter"
> 
> Here is my config:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration status="trace" verbose="true" packages="LevelRangeFilter">
>  <CustomLevels>
>    <CustomLevel name="INFOM1" intLevel="399"/>
>    <CustomLevel name="INFOP1" intLevel="401"/>
>  </CustomLevels>
>  <Appenders>
>    <File name="info" fileName="info.log">
>      <PatternLayout>
>    <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
>      </PatternLayout>
>      <LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT"/>
>    </File>
>  </Appenders>
>  <Loggers>
>    <Logger name="HelloWorld" level="ALL">
>      <AppenderRef ref="info"/>
>    </Logger>
>    <Root>
>    </Root>
>  </Loggers>
> </Configuration>
> 
> Thanks,
> Nick
> 
>> Subject: Re: plugins
>> From: ralph.go...@dslextreme.com
>> Date: Thu, 27 Aug 2015 14:20:28 -0700
>> To: log4j-user@logging.apache.org
>> 
>> Can’t you just compare what you wrote to what Gary wrote?  If you build your 
>> project then it should get packaged in a jar and automatically have the file 
>> Log4j uses to quickly load the plugin.
>> 
>> Ralph
>> 
>>> On Aug 27, 2015, at 12:50 PM, Nicholas Duane <nic...@msn.com> wrote:
>>> 
>>> Based on your previous comments I assume one would show up out-of-the-box 
>>> at some point.  Of course I need mine now so unfortunately I have to write 
>>> one.
>>> 
>>> Any input on my questions below?
>>> 
>>> Thanks,
>>> Nick
>>> 
>>>> Date: Thu, 27 Aug 2015 12:20:07 -0700
>>>> Subject: Re: plugins
>>>> From: garydgreg...@gmail.com
>>>> To: log4j-user@logging.apache.org
>>>> 
>>>> Note that I wrote such a filter, which is under review ATM here:
>>>> 
>>>> - https://issues.apache.org/jira/browse/LOG4J2-1106
>>>> - https://issues.apache.org/jira/browse/LOG4J2-1105
>>>> 
>>>> Gary
>>>> 
>>>>> On Thu, Aug 27, 2015 at 11:51 AM, Nicholas Duane <nic...@msn.com> wrote:
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> I've got a couple questions regarding plugins I'm hoping someone might be
>>>>> able to help me with.  I checked the docs and it's still not quite clear.
>>>>> 
>>>>> 1. I'm unsure what to set printObject to in my Plugin annotation.  From
>>>>> http://logging.apache.org/log4j/2.x/manual/extending.html it says:
>>>>> 
>>>>> "Specifying the printObject attribute with a value of "true" indicates
>>>>> that a call to toString will format the arguments to the filter as the
>>>>> configuration is being processed."
>>>>> 
>>>>> which unfortunately doesn't clear things up any.  I looked at the docs for
>>>>> printObject and that doesn't say anything other than it's a Boolean and 
>>>>> its
>>>>> default is false.
>>>>> 
>>>>> 2. I created a LevelRangeFiler and I'm trying to figure out how to get it
>>>>> loaded by log4j.  I read over the instructions on plugins located at
>>>>> http://logging.apache.org/log4j/2.x/manual/plugins.html but since I'm a
>>>>> java noob I'm still a bit lost.  I'm wondering, if I just have a .java
>>>>> class which I compile to a .class file, can that be used?  If so, how?
>>>>> 
>>>>> Below is the filter I wrote based on looking at the threshold filter
>>>>> example at http://logging.apache.org/log4j/2.x/manual/extending.html.
>>>>> 
>>>>> import org.apache.logging.log4j.core.filter.AbstractFilter;
>>>>> import org.apache.logging.log4j.core.Filter;
>>>>> import org.apache.logging.log4j.core.config.plugins.Plugin;
>>>>> import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>>>>> import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>>>> import org.apache.logging.log4j.Level;
>>>>> import org.apache.logging.log4j.core.LogEvent;
>>>>> import org.apache.logging.log4j.core.Logger;
>>>>> import org.apache.logging.log4j.Marker;
>>>>> import org.apache.logging.log4j.message.Message;
>>>>> 
>>>>> @Plugin(name="LevelRangeFilter", category="Core", elementType="filter",
>>>>>       printObject=true)
>>>>> public class LevelRangeFilter extends AbstractFilter
>>>>> {
>>>>>   private final Level minLevel;
>>>>>   private final Level maxLevel;
>>>>> 
>>>>>   public LevelRangeFilter(Level minLevel, Level maxLevel,
>>>>>           Filter.Result onMatch, Filter.Result onMismatch)
>>>>>   {
>>>>>   super(onMatch, onMismatch);
>>>>>   this.minLevel = minLevel;
>>>>>   this.maxLevel = maxLevel;
>>>>>   }
>>>>> 
>>>>>   private Result filter(Level level)
>>>>>   {
>>>>>   if (level.isMoreSpecificThan(this.minLevel) &&
>>>>>           level.isLessSpecificThan(this.maxLevel))
>>>>>       {
>>>>>       return this.onMatch;
>>>>>       }
>>>>>   return this.onMismatch;
>>>>>   }
>>>>> 
>>>>>   public Filter.Result filter(Logger logger, Level level,
>>>>>           Marker marker, String msg, Object[] params)
>>>>>   {
>>>>>   return filter(level);
>>>>>   }
>>>>> 
>>>>>   public Filter.Result filter(Logger logger, Level level,
>>>>>       Marker marker, Object msg, Throwable t)
>>>>>   {
>>>>>   return filter(level);
>>>>>   }
>>>>> 
>>>>>   public Filter.Result filter(Logger logger, Level level,
>>>>>       Marker marker, Message msg, Throwable t)
>>>>>   {
>>>>>   return filter(level);
>>>>>   }
>>>>> 
>>>>>   public Filter.Result filter(LogEvent event)
>>>>>   {
>>>>>   return filter(event.getLevel());
>>>>>   }
>>>>> 
>>>>>   @PluginFactory
>>>>>   public static LevelRangeFilter createFilter(
>>>>>       @PluginAttribute(value="minLevel",
>>>>>       defaultString="DEBUG") Level minLevel,
>>>>>       @PluginAttribute(value="maxLevel",
>>>>>       defaultString="FATAL") Level maxLevel,
>>>>>       @PluginAttribute(value="onMatch",
>>>>>       defaultString="NEUTRAL") Result onMatch,
>>>>>       @PluginAttribute(value="onMismatch",
>>>>>       defaultString="DENY") Result onMismatch)
>>>>>   {
>>>>>   return new LevelRangeFilter(minLevel, maxLevel, onMatch, onMismatch);
>>>>>   }
>>>>> }
>>>>> 
>>>>> Thanks,
>>>>> Nick
>>>> 
>>>> 
>>>> 
>>>> 
>>>> -- 
>>>> E-Mail: garydgreg...@gmail.com | ggreg...@apache.org
>>>> Java Persistence with Hibernate, Second Edition
>>>> <http://www.manning.com/bauer3/>
>>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>>> Spring Batch in Action <http://www.manning.com/templier/>
>>>> Blog: http://garygregory.wordpress.com
>>>> Home: http://garygregory.com/
>>>> Tweet! http://twitter.com/GaryGregory
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
>> For additional commands, e-mail: log4j-user-h...@logging.apache.org
> 
>                         

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to