Property: level
=
fcf::NTest::LL_DEF
Type: fcf::NTest::ELogLevel
Class: fcf::NTest::Logger::OutputTarget
Package: fcfTest
File:
test.hpp
( link_to_the_declaration_in_the_file )
Available from version: 1.2.10
The logging level threshold for this specific output target.
The level property defines the minimum severity threshold for log messages to be processed by this particular fcf::NTest::Logger::OutputTarget.
When a log event is triggered, the fcf::NTest::Logger compares the severity of the message against the level of each target. A message is only written to the target if its level is greater than or equal to the threshold set in this property.
For example, if the level is set to "err", the target will only receive messages with "err" or "ftl" severity, while "inf" or "dbg" messages will be ignored for this target.
If the property value is equal to fcf::NTest::LL_DEF, the logging level set in the logger object (via the fcf::NTest::Logger::level method) is used.
Example: Setting a Specific Log Level for an Output Target
Demonstrating how to configure a dedicated output target that only captures error and fatal messages, while the default target continues to capture all logs.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
#include <fstream>
FCF_TEST_DEFINE("LevelDemo", "Logger", "TargetLevelTest") {
// 1. Trigger various log levels
fcf::NTest::log() << "This is an info message (should not appear in error log)" << std::endl;
fcf::NTest::err() << "This is an error message (should appear in error log)" << std::endl;
fcf::NTest::ftl() << "This is a fatal message (should appear in error log)" << std::endl;
FCF_TEST(true);
}
int main(int a_argc, char* a_argv[]) {
fcf::NTest::Logger& logger = fcf::NTest::logger();
// 2. Create a specialized target for errors only
fcf::NTest::Logger::OutputTarget errorFileTarget;
errorFileTarget.name = "error-only-file";
static std::ofstream errorStream("errors.log");
errorFileTarget.stream = &errorStream;
errorFileTarget.format = "default";
// 3. Set the threshold level to LL_ERR
// Only messages with level LL_ERR or LL_FTL will be written to this file
errorFileTarget.level = fcf::NTest::LL_ERR;
// 4. Add the target to the logger
logger.appendTarget(errorFileTarget);
// 5. Run the tests
bool error = false;
fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error);
return error ? 1 : 0;
}
Output:
Performing the test: "LevelDemo" -> "Logger" -> "TargetLevelTest" ...
> This is an info message (should not appear in error log)
> This is an error message (should appear in error log)
> This is a fatal message (should appear in error log)
[SUCCESS] Test completed successfully (0.000`032`944 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`032`944 sec
If you check the generated file:
cat errors.log
errors.log file:
> This is an error message (should appear in error log)
> This is a fatal message (should appear in error log)
The "info" message was ignored by the file target because its level was lower than the threshold set via level.