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 Arwen Pond <ap...@book.com> on 2014/08/06 23:03:40 UTC

Set the file name based on command line args

My application takes a parameter where the user can specify the log file.  I'd like to pass this parameter to the Logger which uses a FileAppender.  I am not sure how to do this in log4j 2.

Thanks,
Arwen


This electronic mail message contains information that (a) is or 
may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE 
PROTECTED 
BY LAW FROM DISCLOSURE, and (b) is intended only for the use of 
the addressee(s) named herein.  If you are not an intended 
recipient, please contact the sender immediately and take the 
steps necessary to delete the message completely from your 
computer system.

Not Intended as a Substitute for a Writing: Notwithstanding the 
Uniform Electronic Transaction Act or any other law of similar 
effect, absent an express statement to the contrary, this e-mail 
message, its contents, and any attachments hereto are not 
intended 
to represent an offer or acceptance to enter into a contract and 
are not otherwise intended to bind this sender, 
barnesandnoble.com 
llc, barnesandnoble.com inc. or any other person or entity.

Re: Set the file name based on command line args

Posted by Ralph Goers <ra...@dslextreme.com>.
Wouldn’t it be easier to have a Lookup that your main called and just passed the arguments?  But yeah, it should be possible to do what you are suggesting.

Ralph

On Aug 6, 2014, at 4:09 PM, Gary Gregory <ga...@gmail.com> wrote:

> I wonder if we could write a "MainArgumentsLookup" that finds the main
> thread and walks up the stack to the public static main(String[]) main
> methods and gets the arguments from the stack...
> 
> Gary
> 
> 
> On Wed, Aug 6, 2014 at 6:54 PM, Ralph Goers <ra...@dslextreme.com>
> wrote:
> 
>> Well….
>> 
>> You would really be passing the name of the file to the Appender, not a
>> Logger.  Loggers only reference Appenders so you can’t really configure
>> them with Appender configuration data. That said, Log4j 2 provides a few
>> ways you could do this:
>> 1. Set a system property and reference it via ${sys:propertyName} in the
>> configuration.  The manual has examples of this.
>> 2. Create a custom Lookup Plugin and then register the file name with the
>> Lookup.  Then reference it from the configuration as  ${myLookup: key}
>> where myLookup is the name of your custom Lookup and key is the name that
>> represents the file name value.
>> 3. Create a custom Configuration the extends XMLConfiguration or
>> JSONConfiguration. Pass the file name to that class and have it add that as
>> a property to the properties Map in the configuration. The XML could then
>> just reference it as ${propertyName}.
>> 
>> Ralph
>> 
>> On Aug 6, 2014, at 2:03 PM, Arwen Pond <ap...@book.com> wrote:
>> 
>>> My application takes a parameter where the user can specify the log
>> file.  I'd like to pass this parameter to the Logger which uses a
>> FileAppender.  I am not sure how to do this in log4j 2.
>>> 
>>> Thanks,
>>> Arwen
>>> 
>>> 
>>> This electronic mail message contains information that (a) is or
>>> may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE
>>> PROTECTED
>>> BY LAW FROM DISCLOSURE, and (b) is intended only for the use of
>>> the addressee(s) named herein.  If you are not an intended
>>> recipient, please contact the sender immediately and take the
>>> steps necessary to delete the message completely from your
>>> computer system.
>>> 
>>> Not Intended as a Substitute for a Writing: Notwithstanding the
>>> Uniform Electronic Transaction Act or any other law of similar
>>> effect, absent an express statement to the contrary, this e-mail
>>> message, its contents, and any attachments hereto are not
>>> intended
>>> to represent an offer or acceptance to enter into a contract and
>>> are not otherwise intended to bind this sender,
>>> barnesandnoble.com
>>> llc, barnesandnoble.com inc. or any other person or entity.
>> 
>> 
>> ---------------------------------------------------------------------
>> 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: Set the file name based on command line args

Posted by Gary Gregory <ga...@gmail.com>.
I wonder if we could write a "MainArgumentsLookup" that finds the main
thread and walks up the stack to the public static main(String[]) main
methods and gets the arguments from the stack...

Gary


On Wed, Aug 6, 2014 at 6:54 PM, Ralph Goers <ra...@dslextreme.com>
wrote:

> Well….
>
> You would really be passing the name of the file to the Appender, not a
> Logger.  Loggers only reference Appenders so you can’t really configure
> them with Appender configuration data. That said, Log4j 2 provides a few
> ways you could do this:
> 1. Set a system property and reference it via ${sys:propertyName} in the
> configuration.  The manual has examples of this.
> 2. Create a custom Lookup Plugin and then register the file name with the
> Lookup.  Then reference it from the configuration as  ${myLookup: key}
>  where myLookup is the name of your custom Lookup and key is the name that
> represents the file name value.
> 3. Create a custom Configuration the extends XMLConfiguration or
> JSONConfiguration. Pass the file name to that class and have it add that as
> a property to the properties Map in the configuration. The XML could then
> just reference it as ${propertyName}.
>
> Ralph
>
> On Aug 6, 2014, at 2:03 PM, Arwen Pond <ap...@book.com> wrote:
>
> > My application takes a parameter where the user can specify the log
> file.  I'd like to pass this parameter to the Logger which uses a
> FileAppender.  I am not sure how to do this in log4j 2.
> >
> > Thanks,
> > Arwen
> >
> >
> > This electronic mail message contains information that (a) is or
> > may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE
> > PROTECTED
> > BY LAW FROM DISCLOSURE, and (b) is intended only for the use of
> > the addressee(s) named herein.  If you are not an intended
> > recipient, please contact the sender immediately and take the
> > steps necessary to delete the message completely from your
> > computer system.
> >
> > Not Intended as a Substitute for a Writing: Notwithstanding the
> > Uniform Electronic Transaction Act or any other law of similar
> > effect, absent an express statement to the contrary, this e-mail
> > message, its contents, and any attachments hereto are not
> > intended
> > to represent an offer or acceptance to enter into a contract and
> > are not otherwise intended to bind this sender,
> > barnesandnoble.com
> > llc, barnesandnoble.com inc. or any other person or entity.
>
>
> ---------------------------------------------------------------------
> 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: Set the file name based on command line args

Posted by Ralph Goers <ra...@dslextreme.com>.
Well….  

You would really be passing the name of the file to the Appender, not a Logger.  Loggers only reference Appenders so you can’t really configure them with Appender configuration data. That said, Log4j 2 provides a few ways you could do this:
1. Set a system property and reference it via ${sys:propertyName} in the configuration.  The manual has examples of this.
2. Create a custom Lookup Plugin and then register the file name with the Lookup.  Then reference it from the configuration as  ${myLookup: key}  where myLookup is the name of your custom Lookup and key is the name that represents the file name value.
3. Create a custom Configuration the extends XMLConfiguration or JSONConfiguration. Pass the file name to that class and have it add that as a property to the properties Map in the configuration. The XML could then just reference it as ${propertyName}.

Ralph

On Aug 6, 2014, at 2:03 PM, Arwen Pond <ap...@book.com> wrote:

> My application takes a parameter where the user can specify the log file.  I'd like to pass this parameter to the Logger which uses a FileAppender.  I am not sure how to do this in log4j 2.
> 
> Thanks,
> Arwen
> 
> 
> This electronic mail message contains information that (a) is or
> may be CONFIDENTIAL, PROPRIETARY IN NATURE, OR OTHERWISE
> PROTECTED
> BY LAW FROM DISCLOSURE, and (b) is intended only for the use of
> the addressee(s) named herein.  If you are not an intended
> recipient, please contact the sender immediately and take the
> steps necessary to delete the message completely from your
> computer system.
> 
> Not Intended as a Substitute for a Writing: Notwithstanding the
> Uniform Electronic Transaction Act or any other law of similar
> effect, absent an express statement to the contrary, this e-mail
> message, its contents, and any attachments hereto are not
> intended
> to represent an offer or acceptance to enter into a contract and
> are not otherwise intended to bind this sender,
> barnesandnoble.com
> llc, barnesandnoble.com inc. or any other person or entity.


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