You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by da...@l-3com.com on 2008/12/08 19:29:48 UTC

Modifying logFilePath for FileAppender using a PropertyConfigurator

I am trying to configure log4cxx to write its logging messages to a log
file. The path to the log file is not known until run-time. The way that
I was going to go about doing this is by creating a
log4cxx::helpers::Properties object, setting the property and then
passing the Properties object to the PropertyConfigurator.
Here is the sample code I was using:
**** Start Sample Code ****
log4cxx::helpers::Properties loggerProperties;
const LogString key = "log4j.appender.file.File"; // Line 55
const LogString value = logFilePath.string(); // Line 56 logFilePath is
a boost::filesystem::path object
loggerProperties.setProperty(key, value);
PropertyConfigurator::configure(loggerProperties);
**** End Sample Code ****
The problem is that the log4cxx software is built to support UNICODE so
the initialization of the key and value variables does not compile. Here
are the error messages:
Controller.cpp(55) : error C2440: 'initializing' : cannot convert from
'const char [25]' to 'std::basic_string<_Elem,_Traits,_Ax> with
[_Elem=wchar_t,_Traits=std::char_traits<wchar_t>,_Ax=std::allocator<wcha
r_t>]'; No constructor could take the source type, or constructor over
load resolution was ambiguous
Controller.cpp(56) : error C2440: 'initializing' : cannot convert from
'const boost::filesystem::basic_path<String,Traits>::string_type with
[String=std::string,Traits=boost::filesystem::path_traits]' to
'std::basic_string<_Elem,_Traits,_Ax> with
[_Elem=wchar_t,_Traits=std::char_traits<wchar_t>,_Ax=std::allocator<wcha
r_t>]'; No constructor could take the source type, or constructor
overload resolution was ambiguous
Looking within the logstring.h file I found the LOG4CXX_STR macro which
can be added to line 55 to make the first error message go away (I think
because the macro converts my literal string to a UNICODE literal
string). How can I achieve the same effect for the data within the
logFilePath variable (whose value is only known at run-time)?


Re: Modifying logFilePath for FileAppender using a PropertyConfigurator

Posted by Curt Arnold <ca...@apache.org>.
On Dec 9, 2008, at 10:36 AM, david.weber@l-3com.com wrote:

> So, I answered my own question by searching a bit more on google.
>
> The syntax is ${ENVIRONMENT_VARIABLE}/directory/fileName.txt
>
> Now, one more question:
> How does one set up the logger to create a unique log file per run.   
> I'm
> looking at dailyRollingFileAppender, 'cause I'm pretty sure something
> does what I want.
>
> Thanks
>
> --dw
>

Look at log4cxx::rolling::RollingFileAppender.  You could provide your  
own triggering policy that rotates the files on first log statement or  
you could override the class to rollOver() on startup.

RE: Modifying logFilePath for FileAppender using a PropertyConfigurator

Posted by da...@l-3com.com.
So, I answered my own question by searching a bit more on google.

The syntax is ${ENVIRONMENT_VARIABLE}/directory/fileName.txt

Now, one more question:
How does one set up the logger to create a unique log file per run.  I'm
looking at dailyRollingFileAppender, 'cause I'm pretty sure something
does what I want.

Thanks

--dw

> -----Original Message-----
> From: david.weber@l-3com.com [mailto:david.weber@l-3com.com] 
> Sent: Tuesday, December 09, 2008 10:20 AM
> To: Log4CXX User
> Subject: RE: Modifying logFilePath for FileAppender using a 
> PropertyConfigurator
> 
> Well, let's back up for a second.
> 
> Our problem, is that we need to have the log file specified 
> dynamically at runtime.  Our current mechanism, accomplishes 
> this by specifying an environment variable, having the 
> application read the environment variable, and then modifying 
> the FileAppender's file parameter.
> 
> Our current strategy, is we call configureAndWatch() on the 
> config file, modifying the file parameter using code similar 
> to below, and then calling activateOptions() to make them 
> work.  We have a problem, where we do the following actions:
> 
> 1.) File specifies the logging level to be DEBUG.  Logfile 
> writes fine.
> (expected behavior)
> 2.) File is changed to logging level of WARN.  Output to 
> logfile stops.
> (expected behavior)
> 3.) File is changed to logging level of DEBUG.  Output to 
> logfile does not resume. (unexpected behavior).
> 
> Whenever the log file is read, we get the following error:
> 
> 	"log4cxx: File option not set for appender [file]."
> 
> Because we do not specify the log file in the file, but it is 
> done at runtime.
> 
> 
> 
> Ideally, log4cxx would allow us to specify the file as:
> 
> $FOO/log_files/logfile.txt
> 
> where there exists an environment variable named FOO that 
> points to some directory, and have the FileAppender figure 
> out where to place it.
> 
> 
> 
> 
> So, based on what we're trying to do, are there any other 
> suggestions to accomplish this dynamic log-file creation at 
> runtime, with other configuration data being read from a config file?
> 
> Thanks
> 
> --dw
> 
> 
> 

RE: Modifying logFilePath for FileAppender using a PropertyConfigurator

Posted by da...@l-3com.com.
Well, let's back up for a second.

Our problem, is that we need to have the log file specified dynamically
at runtime.  Our current mechanism, accomplishes this by specifying an
environment variable, having the application read the environment
variable, and then modifying the FileAppender's file parameter.

Our current strategy, is we call configureAndWatch() on the config file,
modifying the file parameter using code similar to below, and then
calling activateOptions() to make them work.  We have a problem, where
we do the following actions:

1.) File specifies the logging level to be DEBUG.  Logfile writes fine.
(expected behavior)
2.) File is changed to logging level of WARN.  Output to logfile stops.
(expected behavior)
3.) File is changed to logging level of DEBUG.  Output to logfile does
not resume. (unexpected behavior).

Whenever the log file is read, we get the following error:

	"log4cxx: File option not set for appender [file]."

Because we do not specify the log file in the file, but it is done at
runtime.



Ideally, log4cxx would allow us to specify the file as:

$FOO/log_files/logfile.txt

where there exists an environment variable named FOO that points to some
directory, and have the FileAppender figure out where to place it.




So, based on what we're trying to do, are there any other suggestions to
accomplish this dynamic log-file creation at runtime, with other
configuration data being read from a config file?

Thanks

--dw



RE: Modifying logFilePath for FileAppender using a PropertyConfigurator

Posted by Jean-Francois Bastien <jf...@cae.com>.
> I am trying to configure log4cxx to write its logging messages to a
log
> file. The path to the log file is not known until run-time. The way
that
> I was going to go about doing this is by creating a
> log4cxx::helpers::Properties object, setting the property and then
passing
> the Properties object to the PropertyConfigurator.
> 
> [snip]
> 
> Looking within the logstring.h file I found the LOG4CXX_STR macro
which can
> be added to line 55 to make the first error message go away (I think
because
> the macro converts my literal string to a UNICODE literal string). How
can I
> achieve the same effect for the data within the logFilePath variable
(whose
> value is only known at run-time)?

I do the following:
  const ::log4cxx::LogString wFileName(boost::lexical_cast<
::log4cxx::LogString>(wNonUnicodeString.c_str()));
  // ...
  ::log4cxx::AppenderPtr wAppender(new ::log4cxx::FileAppender(wLayout,
wFileName, wAppendToFile));
 
 
JF