You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Chathura Widanage <ch...@adroitlogic.com> on 2015/12/02 08:29:35 UTC

Migrating from log4j to log4j2

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

Re: Migrating from log4j to log4j2

Posted by Chathura Widanage <ch...@adroitlogic.com>.
Yes Remoko, You are absolutely correct.

On Thu, Dec 3, 2015 at 4:25 AM, Remko Popma <re...@gmail.com> wrote:

> If I understand correctly, your users can modify the log configuration via
> a web interface? You can use the log4j 2 JMX MBeans to get the internal
> log4j status messages and display those to the users.
>
> Sent from my iPhone
>
> > On 2015/12/03, at 2:12, Chathura Widanage <ch...@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)
> >
> > 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 <ra...@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
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: Migrating from log4j to log4j2

Posted by Remko Popma <re...@gmail.com>.
If I understand correctly, your users can modify the log configuration via a web interface? You can use the log4j 2 JMX MBeans to get the internal log4j status messages and display those to the users. 

Sent from my iPhone

> On 2015/12/03, at 2:12, Chathura Widanage <ch...@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)
> 
> 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 <ra...@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 <ch...@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
>> 
>> 

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


Re: Migrating from log4j to log4j2

Posted by Chathura Widanage <ch...@adroitlogic.com>.
Thank you very much Ralph & Gary.

On Thu, Dec 3, 2015 at 11:18 AM, Ralph Goers 
<ra...@dslextreme.com> wrote:
> 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 
>> <ch...@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 
>> <ga...@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 
>>>> <ra...@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
> 

Re: Migrating from log4j to log4j2

Posted by Ralph Goers <ra...@dslextreme.com>.
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 <ch...@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 <ga...@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 <ra...@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


Re: Migrating from log4j to log4j2

Posted by Gary Gregory <ga...@gmail.com>.
On Thu, Dec 3, 2015 at 12:34 AM, 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.
>

Hi,

That's not the way v2 works in general. Objects are usually immutable. You
need to reconfigure the system to update it. You can use the Configurator
API to update levels but for more serious work you can use the new builder
APIs introduced in 2.4.

Gary


>
> Thanks.
>
> On Thu, Dec 3, 2015 at 11:00 AM, Gary Gregory <ga...@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
> >
>



-- 
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

Re: Migrating from log4j to log4j2

Posted by Chathura Widanage <ch...@adroitlogic.com>.
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 <ga...@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 <ra...@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
>

Re: Migrating from log4j to log4j2

Posted by Gary Gregory <ga...@gmail.com>.
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 <ra...@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

Re: Migrating from log4j to log4j2

Posted by Chathura Widanage <ch...@adroitlogic.com>.
Remoko,

Any idea on how to achieve that goal? We were able to do that using log4j
1.2 without any issue.

Thanks

On Thu, Dec 3, 2015 at 10:51 AM, Chathura Widanage <chathura@adroitlogic.com
> wrote:

> Gary,
> I don't find getLayout method in any of the class that are related to
> Appenders.
>
> On Thu, Dec 3, 2015 at 12:29 AM, Gary Gregory <ga...@gmail.com>
> wrote:
>
>> For the console appender getting the encoding will look like:
>>
>> Layout layout = consoleAppender.getLayout();
>> if (layout instanceof StringLayout) {
>>    return ((StringLayout)layout).getCharset();
>> }
>>
>> Gary
>>
>> 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)
>> >
>> > 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
>>
>
>

Re: Migrating from log4j to log4j2

Posted by Chathura Widanage <ch...@adroitlogic.com>.
Gary,
I don't find getLayout method in any of the class that are related to
Appenders.

On Thu, Dec 3, 2015 at 12:29 AM, Gary Gregory <ga...@gmail.com>
wrote:

> For the console appender getting the encoding will look like:
>
> Layout layout = consoleAppender.getLayout();
> if (layout instanceof StringLayout) {
>    return ((StringLayout)layout).getCharset();
> }
>
> Gary
>
> 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)
> >
> > 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 <ra...@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
>

Re: Migrating from log4j to log4j2

Posted by Gary Gregory <ga...@gmail.com>.
For the console appender getting the encoding will look like:

Layout layout = consoleAppender.getLayout();
if (layout instanceof StringLayout) {
   return ((StringLayout)layout).getCharset();
}

Gary

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)
>
> 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 <ra...@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

Re: Migrating from log4j to log4j2

Posted by Gary Gregory <ga...@gmail.com>.
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)
>
> getTarget
>
> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/ConsoleAppender.html#getTarget()


I added ConsoleAppender.getTarget() to git master. This is just a detail in
this conversation but I like the idea of being able to get this value of
our the console appender.

Gary

>
>
> 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 <ra...@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

Re: Migrating from log4j to log4j2

Posted by Chathura Widanage <ch...@adroitlogic.com>.
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)

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 <ra...@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 <ch...@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
>
>

Re: Migrating from log4j to log4j2

Posted by Ralph Goers <ra...@dslextreme.com>.
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 <ch...@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