Header-only C++23 structured logging library using the logfmt format.
Just copy the include/logfmtxx.hpp
in your project.
Or with Shipp, add it to your dependencies:
{
"name": "myproject",
"version": "0.1.0",
"dependencies": [
{
"name": "logfmtxx",
"url": "https://github.com/linkdd/logfmtxx.git",
"version": "v0.1.0"
}
]
}
First, include the relevant headers:
#include <iostream> // if you wish to print logs with std::cout or std::cerr
#include <logfmtxx.hpp>
Then create a logger:
auto logger = logfmtxx::logger{
[](const std::string& message) {
std::cout << message << std::endl;
}
};
Or with global fields:
auto logger = logfmtxx::logger{
[](const std::string& message) {
std::cout << message << std::endl;
},
logfmtxx::field{"foo", 42},
logfmtxx::field{"bar", 3.14}
};
Then, call the log()
method:
logger.log(logfmtxx::level::info, "hello world");
The result should be:
time="2001-01-01T00:00:00Z" level=info message="hello world" foo=42 bar=3.14000
You can add extra fields as well:
logger.log(
logfmtxx::level::error,
"internal server error",
logfmtxx::field{"http.method", "GET"},
logfmtxx::field{"http.path", "/"},
logfmtxx::field{"http.status", 500}
);
The result should be:
time="2001-01-01T00:00:00Z" level=error message="internal server error" foo=42 bar=3.14000 http.method="GET" http.path="/" http.status=500
This software is released under the terms of the BSD 0-clause License.