Menu

#133 property config file exception problem

v1.2.x
open
5
2015-02-11
2012-04-16
No

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.

Discussion

  • Václav Haisman

    Václav Haisman - 2012-04-16
    • assigned_to: nobody --> wilx
    • status: open --> open-accepted
     
  • Václav Haisman

    Václav Haisman - 2012-04-16

    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.

     
  • guang11cheng

    guang11cheng - 2012-04-26

    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.

     
  • Václav Haisman

    Václav Haisman - 2012-04-26

    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.

     
  • Václav Haisman

    Václav Haisman - 2013-05-27
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,31 +1,34 @@
    -In the 'propertyconfig\_test' main.cxx file: \(version log4cplus-1.1.0-rc2\)
    +In the `propertyconfig_test` `main.cxx` file: (version log4cplus-1.1.0-rc2)
    
    +
    +~~~~{.c}
     int
    -main\(\)
    -\{
    -tcout &lt;&lt; LOG4CPLUS\_TEXT\("Entering main\(\)..."\) &lt;&lt; endl;
    -LogLog::getLogLog\(\)-&gt;setInternalDebugging\(true\);
    -Logger root = Logger::getRoot\(\);
    -try \{
    -PropertyConfigurator::doConfigure\(LOG4CPLUS\_TEXT\("log4cplus.properties"\)\);
    -Logger fileCat = Logger::getInstance\(LOG4CPLUS\_TEXT\("filelogger"\)\);
    +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("Testing...."));
    
    -   LOG4CPLUS\_WARN\(root, LOG4CPLUS\_TEXT\("Writing messages to log...."\)\);
    -for\(int i=0; i&lt;1; ++i\) \{ // I change the loop limit from 10000 to 1
    -       LOG4CPLUS\_WARN\(fileCat, LOG4CPLUS\_TEXT\("This is a WARNING..."\) &lt;&lt; i\);
    -\}
    -\}
    -catch\(...\) \{
    -tcout &lt;&lt; LOG4CPLUS\_TEXT\("Exception..."\) &lt;&lt; endl;
    -   LOG4CPLUS\_FATAL\(root, LOG4CPLUS\_TEXT\("Exception occured..."\)\);
    -return -1; // \*\*\*\*\*note: I add this line \*\*\*\*\*
    -\}
    +            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 &lt;&lt; LOG4CPLUS\_TEXT\("Exiting main\(\)..."\) &lt;&lt; endl;
    -return 0;
    -\}
    +    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:
    
     
  • Václav Haisman

    Václav Haisman - 2013-05-27
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -30,26 +30,31 @@
     }
     ~~~~
    
    -1, In current dir, if there is no file named 'log4cplus.properties', I find no exception thrown. The output is:
    +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\).
    +~~~~
    +Entering main()...
    +log4cplus:ERROR No appenders could be found for logger (root).
     log4cplus:ERROR Please initialize the log4cplus system properly.
    -Exiting main\(\)...
    +Exiting main()...
    +~~~~
    
    -There is no exception thrown, because the output of "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\].
    +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.
    
     
  • Václav Haisman

    Václav Haisman - 2013-05-27
    • labels: --> error handling, properties, v1.1.0
    • Group: v1.1.x --> v1.2.x
     

Log in to post a comment.