Logging Messages
Mach provides a built-in logger for recording application events, diagnostics, warnings, and errors.
Logger instances are managed by Mach and should normally be obtained through dependency injection rather than constructed directly.
Using the Logger
Section titled “Using the Logger”Inject mach::Logger into services, controllers, or middleware that need to write log messages.
For example:
class UserService {public: UserService(mach::Logger& logger) : m_logger(logger) {}
void createUser(const std::string& username) { // ...
m_logger.info("Created user '{}'.", username); }
private: mach::Logger& m_logger;};This lets application code use the logger without managing its lifetime or configuration.
Log Levels
Section titled “Log Levels”Mach provides four log levels for different kinds of messages:
| Level | Use for |
|---|---|
Debug |
Detailed diagnostic information useful during development and debugging. |
Info |
Normal application events and operations. |
Warning |
Unexpected situations that do not prevent the application from continuing. |
Error |
Failures that prevent an operation from completing successfully. |
Choose the level that best represents the severity and purpose of the message:
logger.debug("Looking up user {}.", userId);logger.info("User {} signed in.", userId);logger.warning("Login attempt failed for user {}.", userId);logger.error("Failed to load user {}.", userId);Messages below the application’s configured minimum log level are ignored.
Formatting Messages
Section titled “Formatting Messages”Logging methods support formatted messages:
logger.info( "Processed {} requests in {} ms.", requestCount, elapsedMs);Values are inserted into {} placeholders in the order they are provided.
Next Steps
Section titled “Next Steps”For the complete logging API, see Logger.