Initialization procedure

The package is initialised in two stages. The first stage takes place during static initialization. The second stage takes place when the LogManager singleton is created.

During static initialisation the InitialisationHelper singleton is created . On construction it captures the program startup time, reads the required values from the system environment and registers the package types with the Qt type system.

The LogManager singleton is created on first use. The creation is usually triggered by the request for a Logger object. The call to Logger::logger() is passed through to LogManager::logger(). On creation the LogManager creates a Hierarchy object as logger repository.

After the singleton is created the logging of the package is configured to its default by a call to LogManager::configureLogLogger(). The logger logLogger() is configured to be not additive. Messages with the level Level::ERROR_INT and Level::FATAL_INT are written to stderr using a ConsoleAppender. The remaining messages are written to stdout using a second ConsoleAppender. The level is read from the system environment or application settings using InitialisationHelper::setting() with the key Debug. If a level value is found, but it is not a valid Level string, Level::DEBUG_INT is used. If no level string is found Level::ERROR_INT is used.

Once the logging is configured the package is initialised by a call to LogManager::startup(). The function will test for the setting DefaultInitOverride in the system environment and application settings using InitialisationHelper::setting(). If the value is present and set to anything else then false, the initialisation is aborted.
The system environment and application settings are tested for the setting Configuration. If it is found and it is a valid path to a file, the package is configured with the file using PropertyConfigurator::doConfigure(). If the setting Configuration is not available and a QCoreApplication object is present, the application settings are tested for a group Log4Qt/Properties. If the group exists, the package is configured with the setting using the PropertyConfiguratordoConfigure(). If neither a configuration file nor configuration settings could be found, the current working directory is searched for the file "". If it is found, the package is configured with the file using PropertyConfigurator::doConfigure().

The following example shows how to use application settings to initialise the package.

 # file: myapplication.h

 #include qapplication.h

 class MyApplication : public QApplication

     void setupLog4Qt();
 # file: myapplication.cpp

 #include myapplication.h

     // Set Application data to allow Log4Qt initialisation to read the
     // correct values

     // Log first message, which initialises Log4Qt
     Log4Qt::Logger::logger("MyApplication")->info("Hello World");


 void MyApplication::setupLog4Qt()
     QSettings s;

     // Set logging level for Log4Qt to TRACE
     s.setValue("Debug", "TRACE");

     // Configure logging to log to the file C:/myapp.log using the level TRACE
     s.setValue("log4j.appender.A1", "org.apache.log4j.FileAppender");
     s.setValue("log4j.appender.A1.file", "C:/myapp.log");
     s.setValue("log4j.appender.A1.layout", "org.apache.log4j.TTCCLayout");
     s.setValue("log4j.appender.A1.layout.DateFormat", "ISO8601");
     s.setValue("log4j.rootLogger", "TRACE, A1");

     // Settings will become active on next application startup

Generated on Sun Mar 1 16:40:45 2009 for Log4Qt by  doxygen 1.5.6