Logger Documentation¶
The useLogger composable provides a safe and consistent interface for logging to Sentry in the Peachfuzz Frontend. It wraps the Sentry SDK, automatically augments logs with relevant context (user, subscription, route, etc.), and ensures sensitive data is sanitized before being sent.
Overview¶
The logger is designed to be used throughout the application for both debugging and production monitoring. It fails silently if Sentry is disabled or if any error occurs during the logging process, ensuring that logging never crashes the application.
Configuration¶
The logger's activity is controlled by the following runtime configuration variables:
public.sentryEnabled: Must betruefor logs to be sent.public.sentryDsn: A valid Sentry DSN must be provided.
If either of these is missing or false, the logger will return early and perform no actions.
Automatic Attributes¶
Every log message automatically includes the following attributes if they are available:
user_id: The ID of the currently logged-in customer.subscription_id: The ID of the current subscription.url: The full URL of the current request, with the query string stripped and known sensitive dynamic route segments masked in the path (see below).route_name: The name of the current Nuxt route.route_path: The path of the current Nuxt route, with known sensitive dynamic segments masked (see below).locale: The current active locale.
Sensitive route params¶
Some routes carry secrets in their dynamic path segments (e.g. orderKey on checkout/pay-order/[orderId]/[orderKey] and checkout/thank-you/[orderId]/[orderKey]). These are masked to [FILTERED] in both url and route_path via the SENSITIVE_ROUTE_PARAMS list in useLogger.ts. Plain identifiers like orderId are left intact since they aren't secrets on their own.
If a new route introduces a param that grants access to a resource (a key, token, or similar), add its name to SENSITIVE_ROUTE_PARAMS.
Sanitization¶
The logger includes a beforeSendLog mechanism that sanitizes sensitive data. Any attributes matching the following keys will have their values replaced with [FILTERED]:
passwordnewPasswordemailphoneaddress1firstNamelastNamekeylogin
Usage Examples¶
To use the logger, first import and initialize it in your component or composable:
Basic Logging¶
You can log messages at different levels:
// Information logs
logger.info("User viewed the cart");
// Warning logs
logger.warn("Cart checkout initiated with empty items");
// Error logs
logger.error("Failed to update cart quantity");
Logging with Attributes¶
You can provide additional context by passing an optional attributes object:
logger.info("Product added to cart", {
productId: 123,
quantity: 2,
price: 49.99
});
try {
// some logic
} catch (error) {
logger.error("Checkout process failed", {
error: error.message,
cartTotal: 150.00
});
}
Log Levels¶
The following log levels are supported:
trace: For very fine-grained informational events.debug: For debugging information.info: For general informational messages.warn: For potentially harmful situations.error: For error events that might still allow the application to continue running.fatal: For very severe error events that will presumably lead the application to abort.
Best Practices¶
- Don't Log PII: While the logger has automatic sanitization for common keys, avoid passing sensitive personal identifiable information (PII) in custom attributes whenever possible.
- Use Appropriate Levels: Use
errorandfatalfor issues that need immediate attention, andinfoordebugfor general flow tracking. - Context is Key: Always include relevant IDs (like product IDs or transaction IDs) in the attributes to make debugging in Sentry easier.
- Avoid High-Frequency Logging: Be mindful of the volume of logs sent to Sentry, especially in high-traffic areas of the application.