PDA

View Full Version : Using Log4QT



gpuntoni
3rd November 2009, 09:33
Hello!
I'm new to Qt programming,
I would like to use Log4Qt as log framework.
I downloaded related source code, I compiled it as qt library in visual studio 2008.
Now I have a static library .lib to use in my QT project.
I'm not able to link this lib to new QT project.
Log4Qt usage instruction doesn't seem to work.
Someone can tell me te exact steps to use Log4QT?
There is another way to log in QT?

Can anyone help me?
Thank in advance!

Netheril
8th December 2010, 14:23
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
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"

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!");
}
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

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-lite/coding-standards.html#faq-27.5

johnpatrickfrancis
20th January 2016, 03:14
Thanks this was exactly what I was looking for.