In the propertyconfig_test
main.cxx
file: (version log4cplus-1.1.0-rc2)
int main() { tcout << LOG4CPLUS_TEXT("Entering main()...") << endl; LogLog::getLogLog()->setInternalDebugging(true); Logger root = Logger::getRoot(); try { PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("log4cplus.properties")); Logger fileCat = Logger::getInstance(LOG4CPLUS_TEXT("filelogger")); LOG4CPLUS_WARN(root, LOG4CPLUS_TEXT("Testing....")); LOG4CPLUS_WARN(root, LOG4CPLUS_TEXT("Writing messages to log....")); for(int i=0; i<1; ++i) { // I change the loop limit from 10000 to 1 LOG4CPLUS_WARN(fileCat, LOG4CPLUS_TEXT("This is a WARNING...") << i); } } catch(...) { tcout << LOG4CPLUS_TEXT("Exception...") << endl; LOG4CPLUS_FATAL(root, LOG4CPLUS_TEXT("Exception occured...")); return -1; // *****note: I add this line ***** } tcout << LOG4CPLUS_TEXT("Exiting main()...") << endl; return 0; }
1) In current dir, if there is no file named 'log4cplus.properties', I find no exception thrown. The output is:
Entering main()... log4cplus:ERROR No appenders could be found for logger (root). log4cplus:ERROR Please initialize the log4cplus system properly. Exiting main()...
There is no exception thrown, because the output of "Exiting main()...".
2) 'log4cplus.properties' file is in current dir, but in the file there is no logger named 'filelogger', the output is:
Entering main()... log4cplus: Just opened file: output_${ENV_VAR}.log 04/16/12 05:14:48 [3079182592] WARN root %% - Testing.... [main.cxx:24] 04/16/12 05:14:48 [3079182592] WARN root %% - Writing messages to log.... [main.cxx:26] 04/16/12 05:14:48 [3079182592] WARN filelogger %% - This is a WARNING...0 [main.cxx:28] Exiting main()... log4cplus: Entering ConsoleAppender::close().. log4cplus: Destroying appender named [STDOUT]. log4cplus: Destroying appender named [R]. log4cplus: Destroying appender named [R].
There is also no exception thrown.
Question: In the above two conditions, why exception thrown? How do I check the above two conditions in my code?
Thanks a lot.
In 1.1.0-RC3, I have added an error message when the property file cannot be found. However there is still no exception. I an not sure if throwing exceptions is the right thing to do here.
I think just printing an error message is not enough, that means the application above itself should do an 'access' call to check the existence of the property file. All applications using log4cplus should do such job.
I agree that error handling could be better. The problem is that it is not clear to me how it should be improved. I am pretty sure not everyone would be happy if log4cplus were throwing exceptions for all errors all the time.
Diff:
Diff: