replaced some console.errors with logger
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const fs = require("fs").promises;
|
||||
const path = require("path");
|
||||
const logger = require("../../../utils/logger");
|
||||
|
||||
/**
|
||||
* TemplateManager handles loading, caching, and rendering email templates
|
||||
@@ -40,7 +41,7 @@ class TemplateManager {
|
||||
this.initializationPromise = (async () => {
|
||||
await this.loadEmailTemplates();
|
||||
this.initialized = true;
|
||||
console.log("Email Template Manager initialized successfully");
|
||||
logger.info("Email Template Manager initialized successfully");
|
||||
})();
|
||||
|
||||
return this.initializationPromise;
|
||||
@@ -118,22 +119,21 @@ class TemplateManager {
|
||||
const templateContent = await fs.readFile(templatePath, "utf-8");
|
||||
const templateName = path.basename(templateFile, ".html");
|
||||
this.templates.set(templateName, templateContent);
|
||||
console.log(`✓ Loaded template: ${templateName}`);
|
||||
logger.debug("Loaded template", { templateName });
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`✗ Failed to load template ${templateFile}:`,
|
||||
error.message
|
||||
);
|
||||
console.error(
|
||||
` Template path: ${path.join(templatesDir, templateFile)}`
|
||||
);
|
||||
logger.error("Failed to load template", {
|
||||
templateFile,
|
||||
templatePath: path.join(templatesDir, templateFile),
|
||||
error,
|
||||
});
|
||||
failedTemplates.push(templateFile);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Loaded ${this.templates.size} of ${templateFiles.length} email templates`
|
||||
);
|
||||
logger.info("Loaded email templates", {
|
||||
loaded: this.templates.size,
|
||||
total: templateFiles.length,
|
||||
});
|
||||
|
||||
// Check if critical templates are missing
|
||||
const missingCriticalTemplates = criticalTemplates.filter(
|
||||
@@ -152,17 +152,15 @@ class TemplateManager {
|
||||
|
||||
// Warn if non-critical templates failed
|
||||
if (failedTemplates.length > 0) {
|
||||
console.warn(
|
||||
`⚠️ Non-critical templates failed to load: ${failedTemplates.join(
|
||||
", "
|
||||
)}`
|
||||
);
|
||||
console.warn("These templates will use fallback versions");
|
||||
logger.warn("Non-critical templates failed to load, using fallback versions", {
|
||||
failedTemplates,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to load email templates:", error);
|
||||
console.error("Templates directory:", templatesDir);
|
||||
console.error("Error stack:", error.stack);
|
||||
logger.error("Failed to load email templates", {
|
||||
templatesDir,
|
||||
error,
|
||||
});
|
||||
throw error; // Re-throw to fail server startup
|
||||
}
|
||||
}
|
||||
@@ -176,22 +174,20 @@ class TemplateManager {
|
||||
async renderTemplate(templateName, variables = {}) {
|
||||
// Ensure service is initialized before rendering
|
||||
if (!this.initialized) {
|
||||
console.log(`Template manager not initialized yet, initializing now...`);
|
||||
logger.debug("Template manager not initialized yet, initializing now...");
|
||||
await this.initialize();
|
||||
}
|
||||
|
||||
let template = this.templates.get(templateName);
|
||||
|
||||
if (!template) {
|
||||
console.error(`Template not found: ${templateName}`);
|
||||
console.error(
|
||||
`Available templates: ${Array.from(this.templates.keys()).join(", ")}`
|
||||
);
|
||||
console.error(`Stack trace:`, new Error().stack);
|
||||
console.log(`Using fallback template for: ${templateName}`);
|
||||
logger.error("Template not found, using fallback", {
|
||||
templateName,
|
||||
availableTemplates: Array.from(this.templates.keys()),
|
||||
});
|
||||
template = this.getFallbackTemplate(templateName);
|
||||
} else {
|
||||
console.log(`✓ Template found: ${templateName}`);
|
||||
logger.debug("Template found", { templateName });
|
||||
}
|
||||
|
||||
let rendered = template;
|
||||
@@ -202,9 +198,11 @@ class TemplateManager {
|
||||
rendered = rendered.replace(regex, variables[key] || "");
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Error rendering template ${templateName}:`, error);
|
||||
console.error(`Stack trace:`, error.stack);
|
||||
console.error(`Variables provided:`, Object.keys(variables));
|
||||
logger.error("Error rendering template", {
|
||||
templateName,
|
||||
variableKeys: Object.keys(variables),
|
||||
error,
|
||||
});
|
||||
}
|
||||
|
||||
return rendered;
|
||||
|
||||
Reference in New Issue
Block a user