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 power97 <cj...@shaw.ca> on 2009/05/20 15:41:56 UTC

Dynamic File Name and Turn off File Logging

Hi,
  I am dynamically setting the File name that log4j is logging to with the
FileAppender. However , to do this I am first setting the file to "nul" and
then in script setting it to the real log name. This works fine in Windows
but when run on Unix it actually creates a file called "nul". I can remove
it with a shell script but I would prefer to not have to do that. Here is my
code and is there a better way to do this ?

log4j.properties
log4j.appender.LOGFILE.File=nul

Code
// Set log file name
FileAppender appender = (FileAppender)logger.getAppender("LOGFILE");
appender.setFile(Filename.generateTimestampFilename(logFolderPath,
strLogFileName, ".log"));
appender.activateOptions();

  Second , is there a way to dynamically turn off the FileAppender ?

Thanks In Advance


-- 
View this message in context: http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23635686.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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


RE: Dynamic File Name and Turn off File Logging

Posted by Douglas E Wegscheid <Do...@whirlpool.com>.
system properties are set on the Java command line with the -D switch, not 
with environment variables.

java -DLOG4J_NULL=nul .....

power97 <cj...@shaw.ca> wrote on 05/20/2009 03:10:48 PM:

> 
> It didn't work. Any ideas , here is my code:
> 
> Windows .cmd file:
> set LOG4J_NULL=nul
> 
> log4j.properties file
> log4j.appender.LOGFILE.File=${LOG4J_NULL}

RE: Dynamic File Name and Turn off File Logging

Posted by power97 <cj...@shaw.ca>.
It didn't work. Any ideas , here is my code:

Windows .cmd file:
set LOG4J_NULL=nul

log4j.properties file
log4j.appender.LOGFILE.File=${LOG4J_NULL}


Douglas E Wegscheid wrote:
> 
> if you set -DLOG4J_NULL=/dev/null in the unix shell script, and set 
> -DLOG4J_NULL=nul in the Windows command file, then you can just say:
> 
> log4j.appender.R.File=${LOG4J_NULL}
> 
> in your log4j.properties, and it will work in both cases (assuming you use 
> the correct appender name, and not the one I just wrote!). See the top of 
> the javadocs for PropertyConfigurator.
> 
> Property substitution is also supported in the DomConfigurator, see 
> http://mail-archives.apache.org/mod_mbox/logging-log4j-dev/200105.mbox/%3C5.0.2.1.0.20010506010420.01cabb58@mail.qos.ch%3E
> for a good example.
> 
> Douglas E Wegscheid
> Lead Technical Analyst, Whirlpool Corporation
> (269)-923-5278
> 
> "A wrong note played hesitatingly is a wrong note. A wrong note played 
> with conviction is interpretation."
> 
> 
> 
> 
> 
> power97 <cj...@shaw.ca> 
> 05/20/2009 11:11 AM
> Please respond to
> "Log4J Users List" <lo...@logging.apache.org>
> 
> 
> To
> log4j-user@logging.apache.org
> cc
> 
> Subject
> RE: Dynamic File Name and Turn off File Logging
> 
> 
> 
> 
> 
> 
> 
> I'm setting it right in the log4j.properties file. I'm thinking it won't 
> be a
> big deal to just move the log4j outside the JAR file and change it when on
> Unix. 
> 
> Even if I set the value in batch script on windows and unix I still have 
> to
> manually set the values so setting it in the log4j.properties maybe isn't
> anymore work. 
> 
> 
> 
> Douglas E Wegscheid wrote:
>> 
>> how are you setting it now? inside your program, or in the 
> configuration?
>> 
>> you have a number of options: programmatically you can determine what OS 
> 
>> you are on and set it accordingly. You can set it appropriately as a 
>> system property from whatever script or batch file you are invoking the 
>> program from ( set -DLOG4J_NULL=/dev/null and the ${LOG4J_NULL} in your 
>> config.
>> 
>> options limited by imagination...
>> 
>> Douglas E Wegscheid
>> Lead Technical Analyst, Whirlpool Corporation
>> (269)-923-5278
>> 
>> "A wrong note played hesitatingly is a wrong note. A wrong note played 
>> with conviction is interpretation."
>> 
>> 
>> 
>> 
>> 
>> power97 <cj...@shaw.ca> 
>> 05/20/2009 10:25 AM
>> Please respond to
>> "Log4J Users List" <lo...@logging.apache.org>
>> 
>> 
>> To
>> log4j-user@logging.apache.org
>> cc
>> 
>> Subject
>> RE: Dynamic File Name and Turn off File Logging
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> How do I do that ? It does need to run on Windows and Unix. 
>> 
>> 
>> 
>> Michael Erskine wrote:
>>> 
>>>> This works fine in Windows
>>>> but when run on Unix it actually creates a file called "nul".
>>> 
>>> That's because what you're doing is Windows specific. Fix that if you 
>> need
>>> a cross-platform configuration.
>>> 
>>> Regards,
>>> Michael Erskine
>>> 
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>>> For additional commands, e-mail: log4j-user-help@logging.apache.org
>>> 
>>> 
>>> 
>> 
>> -- 
>> View this message in context: 
>> 
> http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23636539.html
> 
>> 
>> Sent from the Log4j - Users mailing list archive at Nabble.com.
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-user-help@logging.apache.org
>> 
>> 
>> 
>> 
> 
> -- 
> View this message in context: 
> http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23637185.html
> 
> Sent from the Log4j - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23641601.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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


RE: Dynamic File Name and Turn off File Logging

Posted by Douglas E Wegscheid <Do...@whirlpool.com>.
if you set -DLOG4J_NULL=/dev/null in the unix shell script, and set 
-DLOG4J_NULL=nul in the Windows command file, then you can just say:

log4j.appender.R.File=${LOG4J_NULL}

in your log4j.properties, and it will work in both cases (assuming you use 
the correct appender name, and not the one I just wrote!). See the top of 
the javadocs for PropertyConfigurator.

Property substitution is also supported in the DomConfigurator, see 
http://mail-archives.apache.org/mod_mbox/logging-log4j-dev/200105.mbox/%3C5.0.2.1.0.20010506010420.01cabb58@mail.qos.ch%3E
for a good example.

Douglas E Wegscheid
Lead Technical Analyst, Whirlpool Corporation
(269)-923-5278

"A wrong note played hesitatingly is a wrong note. A wrong note played 
with conviction is interpretation."





power97 <cj...@shaw.ca> 
05/20/2009 11:11 AM
Please respond to
"Log4J Users List" <lo...@logging.apache.org>


To
log4j-user@logging.apache.org
cc

Subject
RE: Dynamic File Name and Turn off File Logging







I'm setting it right in the log4j.properties file. I'm thinking it won't 
be a
big deal to just move the log4j outside the JAR file and change it when on
Unix. 

Even if I set the value in batch script on windows and unix I still have 
to
manually set the values so setting it in the log4j.properties maybe isn't
anymore work. 



Douglas E Wegscheid wrote:
> 
> how are you setting it now? inside your program, or in the 
configuration?
> 
> you have a number of options: programmatically you can determine what OS 

> you are on and set it accordingly. You can set it appropriately as a 
> system property from whatever script or batch file you are invoking the 
> program from ( set -DLOG4J_NULL=/dev/null and the ${LOG4J_NULL} in your 
> config.
> 
> options limited by imagination...
> 
> Douglas E Wegscheid
> Lead Technical Analyst, Whirlpool Corporation
> (269)-923-5278
> 
> "A wrong note played hesitatingly is a wrong note. A wrong note played 
> with conviction is interpretation."
> 
> 
> 
> 
> 
> power97 <cj...@shaw.ca> 
> 05/20/2009 10:25 AM
> Please respond to
> "Log4J Users List" <lo...@logging.apache.org>
> 
> 
> To
> log4j-user@logging.apache.org
> cc
> 
> Subject
> RE: Dynamic File Name and Turn off File Logging
> 
> 
> 
> 
> 
> 
> 
> How do I do that ? It does need to run on Windows and Unix. 
> 
> 
> 
> Michael Erskine wrote:
>> 
>>> This works fine in Windows
>>> but when run on Unix it actually creates a file called "nul".
>> 
>> That's because what you're doing is Windows specific. Fix that if you 
> need
>> a cross-platform configuration.
>> 
>> Regards,
>> Michael Erskine
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-user-help@logging.apache.org
>> 
>> 
>> 
> 
> -- 
> View this message in context: 
> 
http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23636539.html

> 
> Sent from the Log4j - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23637185.html

Sent from the Log4j - Users mailing list archive at Nabble.com.


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



RE: Dynamic File Name and Turn off File Logging

Posted by power97 <cj...@shaw.ca>.
I'm setting it right in the log4j.properties file. I'm thinking it won't be a
big deal to just move the log4j outside the JAR file and change it when on
Unix. 

Even if I set the value in batch script on windows and unix I still have to
manually set the values so setting it in the log4j.properties maybe isn't
anymore work. 



Douglas E Wegscheid wrote:
> 
> how are you setting it now? inside your program, or in the configuration?
> 
> you have a number of options: programmatically you can determine what OS 
> you are on and set it accordingly. You can set it appropriately as a 
> system property from whatever script or batch file you are invoking the 
> program from ( set -DLOG4J_NULL=/dev/null and the ${LOG4J_NULL} in your 
> config.
> 
> options limited by imagination...
> 
> Douglas E Wegscheid
> Lead Technical Analyst, Whirlpool Corporation
> (269)-923-5278
> 
> "A wrong note played hesitatingly is a wrong note. A wrong note played 
> with conviction is interpretation."
> 
> 
> 
> 
> 
> power97 <cj...@shaw.ca> 
> 05/20/2009 10:25 AM
> Please respond to
> "Log4J Users List" <lo...@logging.apache.org>
> 
> 
> To
> log4j-user@logging.apache.org
> cc
> 
> Subject
> RE: Dynamic File Name and Turn off File Logging
> 
> 
> 
> 
> 
> 
> 
> How do I do that ? It does need to run on Windows and Unix. 
> 
> 
> 
> Michael Erskine wrote:
>> 
>>> This works fine in Windows
>>> but when run on Unix it actually creates a file called "nul".
>> 
>> That's because what you're doing is Windows specific. Fix that if you 
> need
>> a cross-platform configuration.
>> 
>> Regards,
>> Michael Erskine
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-user-help@logging.apache.org
>> 
>> 
>> 
> 
> -- 
> View this message in context: 
> http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23636539.html
> 
> Sent from the Log4j - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23637185.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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


RE: Dynamic File Name and Turn off File Logging

Posted by Douglas E Wegscheid <Do...@whirlpool.com>.
how are you setting it now? inside your program, or in the configuration?

you have a number of options: programmatically you can determine what OS 
you are on and set it accordingly. You can set it appropriately as a 
system property from whatever script or batch file you are invoking the 
program from ( set -DLOG4J_NULL=/dev/null and the ${LOG4J_NULL} in your 
config.

options limited by imagination...

Douglas E Wegscheid
Lead Technical Analyst, Whirlpool Corporation
(269)-923-5278

"A wrong note played hesitatingly is a wrong note. A wrong note played 
with conviction is interpretation."





power97 <cj...@shaw.ca> 
05/20/2009 10:25 AM
Please respond to
"Log4J Users List" <lo...@logging.apache.org>


To
log4j-user@logging.apache.org
cc

Subject
RE: Dynamic File Name and Turn off File Logging







How do I do that ? It does need to run on Windows and Unix. 



Michael Erskine wrote:
> 
>> This works fine in Windows
>> but when run on Unix it actually creates a file called "nul".
> 
> That's because what you're doing is Windows specific. Fix that if you 
need
> a cross-platform configuration.
> 
> Regards,
> Michael Erskine
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23636539.html

Sent from the Log4j - Users mailing list archive at Nabble.com.


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



RE: Dynamic File Name and Turn off File Logging

Posted by power97 <cj...@shaw.ca>.
How do I do that ? It does need to run on Windows and Unix. 



Michael Erskine wrote:
> 
>> This works fine in Windows
>> but when run on Unix it actually creates a file called "nul".
> 
> That's because what you're doing is Windows specific. Fix that if you need
> a cross-platform configuration.
> 
> Regards,
> Michael Erskine
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23636539.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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


RE: Dynamic File Name and Turn off File Logging

Posted by Douglas E Wegscheid <Do...@whirlpool.com>.
on Unix-like systems, /dev/null works great.

Douglas E Wegscheid
Lead Technical Analyst, Whirlpool Corporation
(269)-923-5278

"A wrong note played hesitatingly is a wrong note. A wrong note played 
with conviction is interpretation."





Michael Erskine <mi...@ketech.com> 
05/20/2009 10:06 AM
Please respond to
"Log4J Users List" <lo...@logging.apache.org>


To
Log4J Users List <lo...@logging.apache.org>
cc

Subject
RE: Dynamic File Name and Turn off File Logging






> This works fine in Windows
> but when run on Unix it actually creates a file called "nul".

That's because what you're doing is Windows specific. Fix that if you need 
a cross-platform configuration.

Regards,
Michael Erskine

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



RE: Dynamic File Name and Turn off File Logging

Posted by Michael Erskine <mi...@ketech.com>.
> This works fine in Windows
> but when run on Unix it actually creates a file called "nul".

That's because what you're doing is Windows specific. Fix that if you need a cross-platform configuration.

Regards,
Michael Erskine

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


Re: Dynamic File Name and Turn off File Logging

Posted by Jacob Kjome <ho...@visi.com>.
Why not place a temporary Log4j config file in the classspath that does 
something minimal like configuring only the root logger and setting the 
appender to console with the level set to WARN.  Then, when you are ready to 
configure at runtime, you can load up your real log4j.properties file and pass 
in a properties object containing a property you can reference in 
log4j.properties with the path for the log file.  For instance...

log4j.appender.LOGFILE.File=${file.appender.path}

Note that you could also use this properties file as your primary Log4j 
auto-configuration file and set a system property instead.  You'd reference it 
the same way as above.  For instance...

java -Dfile.appender.path=/path/to/logfile.log MyApplcation


Jake


On Wed, 20 May 2009 06:41:56 -0700 (PDT)
  power97 <cj...@shaw.ca> wrote:
> 
> Hi,
>  I am dynamically setting the File name that log4j is logging to with the
>FileAppender. However , to do this I am first setting the file to "nul" and
> then in script setting it to the real log name. This works fine in Windows
> but when run on Unix it actually creates a file called "nul". I can remove
> it with a shell script but I would prefer to not have to do that. Here is my
> code and is there a better way to do this ?
> 
> log4j.properties
> log4j.appender.LOGFILE.File=nul
> 
> Code
> // Set log file name
>FileAppender appender = (FileAppender)logger.getAppender("LOGFILE");
> appender.setFile(Filename.generateTimestampFilename(logFolderPath,
> strLogFileName, ".log"));
> appender.activateOptions();
> 
>  Second , is there a way to dynamically turn off the FileAppender ?
> 
> Thanks In Advance
> 
> 
> -- 
> View this message in context: 
>http://www.nabble.com/Dynamic-File-Name-and-Turn-off-File-Logging-tp23635686p23635686.html
> Sent from the Log4j - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> 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