Hi gpuntoni, perhaps is a little bit late for the answer, but I guess would be useful for new readers that stop by this forum. I don't use log4Qt as a library in my Qt projects, I just include the *.pri file that comes in the log4Qt source folder, but I'll explain how I did it in case that will help you.
1. Download log4Qt from: http://log4qt.sourceforge.net/
2. In your project add the line: include(/locationOfYourLog4QtFolder/src/log4qt/log4qt.pri). Look at the line 12 of my .pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2010-12-08T08:17:29
#
#-------------------------------------------------
QT += core gui
TARGET = testLog4Qt
TEMPLATE = app
include(/locationOfYourLog4QtFolder/src/log4qt/log4qt.pri)
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
#-------------------------------------------------
#
# Project created by QtCreator 2010-12-08T08:17:29
#
#-------------------------------------------------
QT += core gui
TARGET = testLog4Qt
TEMPLATE = app
include(/locationOfYourLog4QtFolder/src/log4qt/log4qt.pri)
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
To copy to clipboard, switch view to plain text mode
That will include in your project all the source files necessary to work with Log4Qt.
3. Just add your log4Qt code anywhere in your code:
#include "log4qt/consoleappender.h"
#include "log4qt/logger.h"
#include "log4qt/ttcclayout.h"
#include "log4qt/logmanager.h"
{
Log4Qt::LogManager::rootLogger();
Log4Qt::TTCCLayout *p_layout = new Log4Qt::TTCCLayout();
p_layout->activateOptions();
// Create an appender
Log4Qt::ConsoleAppender *p_appender = new Log4Qt::ConsoleAppender(p_layout, Log4Qt::ConsoleAppender::STDOUT_TARGET);
p_appender->activateOptions();
// Set appender on root logger
Log4Qt::Logger::rootLogger()->addAppender(p_appender);
Log4Qt
::Logger::logger(QLatin1String("My Logger"))->info
("Hello World!");
}
#include "log4qt/consoleappender.h"
#include "log4qt/logger.h"
#include "log4qt/ttcclayout.h"
#include "log4qt/logmanager.h"
Dialog::Dialog( QWidget *parent ) : QDialog( parent )
{
Log4Qt::LogManager::rootLogger();
Log4Qt::TTCCLayout *p_layout = new Log4Qt::TTCCLayout();
p_layout->setName(QLatin1String("My Layout"));
p_layout->activateOptions();
// Create an appender
Log4Qt::ConsoleAppender *p_appender = new Log4Qt::ConsoleAppender(p_layout, Log4Qt::ConsoleAppender::STDOUT_TARGET);
p_appender->setName(QLatin1String("My Appender"));
p_appender->activateOptions();
// Set appender on root logger
Log4Qt::Logger::rootLogger()->addAppender(p_appender);
Log4Qt::Logger::logger(QLatin1String("My Logger"))->info("Hello World!");
}
To copy to clipboard, switch view to plain text mode
4. Now compile your project and it's done. You will see in your Applicatin Output (Alt+3):
Starting /home/user/Documents/Qt_Projects/testLog4Qt/testLog4Qt-build-desktop/testLog4Qt...
99 [] INFO My Logger - Hello World!
/home/user/Documents/Qt_Projects/testLog4Qt/testLog4Qt-build-desktop/testLog4Qt exited with code 0
Starting /home/user/Documents/Qt_Projects/testLog4Qt/testLog4Qt-build-desktop/testLog4Qt...
99 [] INFO My Logger - Hello World!
/home/user/Documents/Qt_Projects/testLog4Qt/testLog4Qt-build-desktop/testLog4Qt exited with code 0
To copy to clipboard, switch view to plain text mode
Note: If you wish you can use the using namespace Log4Qt, but I'm more the kind of guy that use Log4Qt:: or std::, look at: http://www.parashift.com/c++-faq-lit....html#faq-27.5
Bookmarks