Centralized Application Logger
This document describes the centralized logging service for the ChainAlign backend application.
Overview
This service provides a singleton-like interface for logging across the application. It is designed to integrate with Google Cloud Logging in a production environment while providing a simple, readable console fallback for local development or when Google Cloud credentials are not available.
It must be explicitly initialized at application startup by calling initializeLogger.
Functions
initializeLogger()
Initializes the application logger. This function should be called once at application startup. It attempts to configure a connection to Google Cloud Logging using the credentials specified in the GOOGLE_APPLICATION_CREDENTIALS environment variable.
If the environment variable is not set, or if the client fails to initialize for any reason, it gracefully falls back to using the standard console for all log outputs. This ensures that logging is always available, even in a local development environment.
Note: This function uses console for its own logs during the initialization process, as the main logger is not yet available.
log(message, severity, metadata)
Logs a message with a specified severity and optional metadata.
message(string): The message to log.severity(string, optional): The severity level (e.g., 'INFO', 'ERROR', 'WARNING', 'DEBUG'). Defaults to 'INFO'.metadata(object, optional): Additional structured data to associate with the log entry.
error(message, metadata)
Logs a message with ERROR severity.
message(string): The error message to log.metadata(object, optional): Additional structured data to associate with the log entry.
warn(message, metadata)
Logs a message with WARNING severity.
message(string): The warning message to log.metadata(object, optional): Additional structured data to associate with the log entry.
info(message, metadata)
Logs a message with INFO severity.
message(string): The informational message to log.metadata(object, optional): Additional structured data to associate with the log entry.
debug(message, metadata)
Logs a message with DEBUG severity.
message(string): The debug message to log.metadata(object, optional): Additional structured data to associate with the log entry.