logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Goers <ralph.go...@dslextreme.com>
Subject Re: Migrating from log4j to log4j2
Date Thu, 03 Dec 2015 05:48:56 GMT
No, you cannot change the layout of an existing Appender. You cannot change the level of the
ThresholdFilter but you can remove the filter and replace it with a new one.  You can do this
with the removeFilter and addFilter methods. However, if you remove the filter then all events
will go to the Appender until the addFilter is completed. If you add the new filter first
then you probably would get closer to the behavior you want.

Keep in mind that Log4j 2 has a lot more immutable objects than Log4j 1 did so that you won’t
run into cases where you might get exceptions or indeterminate behavior. That said, it should
be possible to replace the layout in a safe way.

Also, I don’t know if it helps you at all, but Log4j 2 now supports PatternSelectors, so
if you are using the PatternLayout but want multiple patterns depending on some criteria you
can now do that just through configuration. So you could have a PatternSelector with n predefined
patterns and have the user change a setting on which one he wants to use.

Ralph

> On Dec 2, 2015, at 10:34 PM, Chathura Widanage <chathura@adroitlogic.com> wrote:
> 
> Gary,
> Thank you very much.
> Is there a way to setLayout and setThreshold of an appender at runtime
> using log4j 2, like we used to do with log4j 1.
> 
> Thanks.
> 
> On Thu, Dec 3, 2015 at 11:00 AM, Gary Gregory <garydgregory@gmail.com>
> wrote:
> 
>> On Wed, Dec 2, 2015 at 12:12 PM, Chathura Widanage <
>> chathura@adroitlogic.com
>>> wrote:
>> 
>>> We are developing a product(ESB) that will be used by an end user. In our
>>> product we have provided an interface to alter the logging patterns
>>> according the user preference in run time is self. We were able to do
>> this
>>> using log4j1. But I can't find a way to alter the pattern of appender in
>>> run time using log4j2.
>>> 
>>> In case of Console appender, we want to collect information about the
>>> appenders, so user can view them in a GUI which is provided by our
>> product.
>>> 
>>> Please let me know if there is a possible way of doing these thing using
>>> log4j2.
>>> 
>>> I would be glad if you can mention the log4j2 methods or procedures that
>>> can be used to performs the exact same function of the following log4j1
>>> methods.
>>> 
>>> setlayout
>>> 
>>> 
>> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html#setLayout(org.apache.log4j.Layout)
>>> 
>>> 
>> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/AppenderSkeleton.html#setLayout(org.apache.log4j.Layout)
>>> 
>>> setThreshold
>>> 
>>> 
>> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/AppenderSkeleton.html#setThreshold(org.apache.log4j.Priority)
>> 
>> 
>> Setting the log level is a topic usually address by calling the
>> Configurator class.
>> 
>> Gary
>> 
>> 
>>> 
>>> 
>>> getTarget
>>> 
>>> 
>> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/ConsoleAppender.html#getTarget()
>>> 
>>> getEncoding
>>> 
>>> 
>> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/ConsoleAppender.html#getFollow()
>>> 
>>> Thanks in advance
>>> 
>>> 
>>> On Wed, Dec 2, 2015 at 7:32 PM, Ralph Goers <ralph.goers@dslextreme.com>
>>> wrote:
>>> 
>>>> I’m not sure what you are trying to do with the pattern is necessary.
>> The
>>>> latest version of Log4j supports a PatternSelector that should provide
>>> what
>>>> you need.
>>>> 
>>>> I am not sure why the updating of the ConsoleAppender is necessary. Why
>>>> can’t that just be configured?
>>>> 
>>>> Ralph
>>>> 
>>>>> On Dec 2, 2015, at 12:29 AM, Chathura Widanage <
>>> chathura@adroitlogic.com>
>>>> wrote:
>>>>> 
>>>>> Hi all,
>>>>> 
>>>>> I am in a process of migrating an application from log4j1 to log4j2.
>>>>> I have successfully migrated 98% of the application but I am stuck
>> at a
>>>>> point where it is required to alter the configurations of an Appender
>>> at
>>>>> the run time.
>>>>> 
>>>>> Below is the piece of code that we used in log4j1
>>>>> 
>>>>> public void updateAppender(AppenderView view) {
>>>>>       auditLog.info("Updating the appender with the name : {}, of
>> the
>>>>> instance", view.getName());
>>>>>       AppenderSkeleton appender = (AppenderSkeleton)
>>>>> LogManager.getRootLogger().getAppender(view.getName());
>>>>> 
>>>>>       if (view.getPattern() != null) {
>>>>>           appender.setLayout(new PatternLayout(view.getPattern()));
>>>>>       }
>>>>> 
>>>>>       if (!"null".equals(view.getThreshold())) {
>>>>>           appender.setThreshold(Level.toLevel(view.getThreshold()));
>>>>>       }
>>>>>   }
>>>>> 
>>>>> Since in log4j2, there is no method called setLayout and setThreshold
>>> in
>>>>> Appender class, Can I know a way to implement the above functionality
>>>> using
>>>>> the log4j2 APIs.
>>>>> 
>>>>> Further I want to migrate the following piece of code,
>>>>> 
>>>>> else if (appender instanceof ConsoleAppender) {
>>>>>           ConsoleAppender consoleAppender = (ConsoleAppender)
>>> appender;
>>>>>           properties.put(CONSOLE_APPENDER_TARGET,
>>>>> consoleAppender.getTarget());
>>>>>           properties.put(CONSOLE_APPENDER_ENCODING,
>>>>> consoleAppender.getEncoding());
>>>>>           properties.put(CONSOLE_APPENDER_FOLLOW,
>>>>> String.valueOf(consoleAppender.getFollow()));
>>>>>       }
>>>>> 
>>>>> Here I don't find getTarget,getEncoding,getFollow methods in
>>>>> ConsoleAppender class.
>>>>> 
>>>>> Can some one please lead me to the way which I can implement the
>> above
>>>>> functionalities using log4j2 APIs.
>>>>> 
>>>>> Thanks,
>>>>> Chathura
>>>> 
>>>> 
>>>> 
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>>>> For additional commands, e-mail: log4j-user-help@logging.apache.org
>>>> 
>>>> 
>>> 
>> 
>> 
>> 
>> --
>> E-Mail: garydgregory@gmail.com | ggregory@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-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message