Introduction Into Logging With Loguru - CodeProject
Introduction Into Logging With Loguru - CodeProject
This time, I would like to give a short introduction into a nice little library I just encountered. It’s called loguru and it’s a lightweight,
Thread-Safe, logging library with an impressive well written documentation and human-readable output.
To use it, all you need to do is to add a single header and source file to compile with. Unfortunately, that’s, in my opinion, also a
drawback. Yes, it’s easier to install and usable in training, but it can’t be exchanged by whoever is operating the system after
deployment. In such cases, where the operator wants to define how and what to use for logging, a generic logging interface (facade
pattern), such as slf4cxx which is similar to its Java pendant slf4j, would be preferable.
Loguru is supporting a various features such as callbacks for logging and fatal errors, verbosity levels, assertions and aborts, and
stack traces in case of aborts. It even supports {fmt}. Everyone who ever was used to java/spring log outputs will recognize its
similarities.
#include <thread>
#include "loguru.hpp"
#include "loguru.cpp"
void complex()
{
//LOG_SCOPE_F is indenting the following logs
LOG_SCOPE_F(INFO, "Preparing complex calculation");
https://www.codeproject.com/Articles/1279816/Introduction-into-Logging-with-Loguru?display=Print 1/3
25/04/2019 Introduction into Logging with Loguru - CodeProject
//Additionally, if we need we can also put the logs into a defined file
loguru::add_file("important.log", loguru::Truncate, loguru::Verbosity_INFO);
crashingFunction(-1);
return 0;
}
As you can see, it’s pretty straight forward to use. We are not only logging several messages on INFO verbosity level, but also a
message in a named thread called “complex lambda”. If we wouldn’t have defined the thread name with
loguru::set_thread_name(“complex lambda”), loguru would state the name of a thread with a hex id. The main
thread gets its name by calling loguru::init(…). Because our small tool is crashing, loguru is printing us a stack trace which
in my opinion is not as helpful as expected, but with ERROR_CONTEXT, we get a little better output.
https://www.codeproject.com/Articles/1279816/Introduction-into-Logging-with-Loguru?display=Print 2/3
25/04/2019 Introduction into Logging with Loguru - CodeProject
That’s it for now with this post. We have now a short introduction into a, until now, rather unknown, but promising, logging library.
Loguru is not only capable of producing Thread-Safe and human readable logging messages but also provides a very simple and
handy interface to use.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Passionate C++ developer, mechanical engineer, Head of Software Development at KISSsoft AG and host of https://thoughs-on-
cpp.com
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2019 by thoughts-on-cpp
Web02 | 2.8.190424.1 | Last Updated 14 Mar 2019 Everything else Copyright © CodeProject, 1999-2019
https://www.codeproject.com/Articles/1279816/Introduction-into-Logging-with-Loguru?display=Print 3/3