Since I restarted the blog more than 2 years ago I’ve never talked about QLogger in-depth. I linked to the repo and did a small introduction in the second entry (Logging with Qt) but that was all.
During this year QLogger has had some nice improvements going from some sort of threading to full multi-threading support. During the last month it went from close configuration to full. When once it was only possible to choose the log level, now you can configure a lot of different things, including the output type.
All of that while keeping the retro-compatibility from the old usage.
How to use of QLogger
The initialization of QLogger remains the same as it was before. It’s still follows the singleton pattern and same signature. The following line will create the instance if it doesn’t exist or it will retrieve the existing one:
QLoggerManager::getInstance();
After that you can choose to pause it so it doesn’t print any messages until you resume it:
const auto logger = QLoggerManager::getInstance();
logger->pause();
logger->resume();
Adding destinations (basic level)
QLogger works in a way where you add modules (at least one) and a file where you want to print. This is the simplest scenario and gives you the ability to add several modules that print in the same file by passing the list of modules. Or splitting the file printing a variable number of modules by adding destinations one by one.
const auto logger = QLoggerManager::getInstance();
logger->addDestination("myLog.log", "UI");
logger->addDestination("myOtherLog.log", {"Network", "DB"});
Finally, to change the log level of those modules, you can use the following:
const auto logger = QLoggerManager::getInstance();
const auto infoLogLevel = LogLevel::Debug;
logger->overwriteLogLevel(infoLogLevel);
Logging with QLogger
The easier way to log using QLogger is by using the predefined methods. They automatically log the message using the desired level from the following 7 ones:
- QLog_Trace
- QLog_Debug
- QLog_Info
- QLog_Warning
- QLog_Error
- QLog_Fatal
The usage would be something like:
QLog_Info("UI", QString("Executing the command: {%1}").arg(cmd));
If the choosen level (suffix _Info) is equal or higher than the configured level, it will print the message onto the destination.
New configuration for QLogger
Recently, a user named AphaseDev has introduced a new bunch of features in QLogger. That includes a whole level of configuration for the destinations as well as adding support for console logging using QLogger. The new configuration includes the following fields:
- Folder where the logs directory will be placed.
- Log mode: Now the user can define if the logs will be printed in the file, the console or both.
- File name suffix: It is possible to configure the log file suffix showing the date and time or a number.
- Log message format: The log message can include a wider amount of information and not only the module and the log message. It includes the thread that printed the message, the function who printed the message, and the file and line.
In addition to the configuration of the destination, it is now possible to configure the maximum size of the file. The default value is 1 MB but you can add any number in here. Removing the log files is also possible through the clearFileDestinationFolder method and optionally providing the oldest date to keep the files from.
It is also possible to overwrite the log mode (output redirected to console or log file) during execution time.
Versioning
So far I’ve kept QLogger as an “under development” repository. The problem of releasing versions of a library for Qt is that I would have to provide versions compiled with all the possible compilers and at least several Qt versions, what is something annoying.
So far nobody has requested that and my intention is to keep the repository free of of pre-compiled libraries. However at the same time of this post I’m starting to add a release tag to the QLogger repository. This will allow users to have an easier way to get the tested and ready-for-production versions of the library.
Since this is a library, I’m concerned about breaking changes so I’m going to use the QT_DEPRECATED_X attribute (equivalent to the deprecated attribute added in C++14) and add that flag in the configuration of the project. This will allow people to get at least warnings or errors in the best case.
The code can be found in here.
Feel free to comment and let me know what you would like to see in QLogger in the future.
Comments
Function like syntax is a pain to write. QsLogger does it better with qDebug styled logger
Hi Nikolai,
Feel free to open an issue explaining how you would like to see it if you think it can be improved! 🙂
https://github.com/francescmm/QLogger/issues
I checked your github and there’s already an issue for that. I meant logging with streaming operator instead of function calls. It’s just way more natural to not wrap the logged things into braces and instead << them