Skip to content
Download

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.

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.

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.

Logging methods support formatted messages:

logger.info(
"Processed {} requests in {} ms.",
requestCount,
elapsedMs
);

Values are inserted into {} placeholders in the order they are provided.


For the complete logging API, see Logger.