phase 1-6 implemented

This commit is contained in:
dimitar committed 2026-03-10 04:14:03 +01:00
1 parent 22c274bb83
commit 2c4927478a
73 files changed
+9702 -2354

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
/**
* Centralized logging utility for FitAI Admin App
* Uses Pino for structured logging with appropriate log levels
*/
import pino from "pino";
/**
* Create logger instance based on environment
* - Development: Pretty-printed logs to console
* - Production: JSON logs for aggregation/monitoring
*/
const logger = pino({
level:
process.env.LOG_LEVEL ||
(process.env.NODE_ENV === "production" ? "info" : "debug"),
formatters: {
level: (label) => {
return { level: label };
},
},
...(process.env.NODE_ENV !== "production" && {
transport: {
target: "pino-pretty",
options: {
colorize: true,
translateTime: "HH:MM:ss Z",
ignore: "pid,hostname",
},
},
}),
});
/**
* Log levels:
* - debug: Detailed information for debugging (only in dev)
* - info: General informational messages
* - warn: Warning messages for potentially harmful situations
* - error: Error messages for failures that don't stop the app
* - fatal: Critical errors that may cause the app to crash
*/
export { logger };
/**
* Convenience functions for common logging patterns
*/
export const log = {
/**
* Log debug information (only in development)
*/
debug: (message: string, data?: Record<string, unknown>) => {
logger.debug(data || {}, message);
},
/**
* Log general information
*/
info: (message: string, data?: Record<string, unknown>) => {
logger.info(data || {}, message);
},
/**
* Log warnings
*/
warn: (message: string, data?: Record<string, unknown>) => {
logger.warn(data || {}, message);
},
/**
* Log errors with optional error object
*/
error: (message: string, error?: unknown, data?: Record<string, unknown>) => {
const errorData: Record<string, unknown> = {
...(data || {}),
};
if (error instanceof Error) {
errorData.error = {
name: error.name,
message: error.message,
stack: error.stack,
};
} else if (error && typeof error === "object") {
errorData.error = error;
}
logger.error(errorData, message);
},
/**
* Log fatal errors (should be rare)
*/
fatal: (message: string, error?: unknown, data?: Record<string, unknown>) => {
const errorData: Record<string, unknown> = {
...(data || {}),
};
if (error instanceof Error) {
errorData.error = {
name: error.name,
message: error.message,
stack: error.stack,
};
} else if (error && typeof error === "object") {
errorData.error = error;
}
logger.fatal(errorData, message);
},
/**
* Log API requests (use in middleware or API routes)
*/
apiRequest: (
method: string,
path: string,
data?: Record<string, unknown>,
) => {
logger.info({ method, path, ...(data || {}) }, "API request");
},
/**
* Log API responses
*/
apiResponse: (
method: string,
path: string,
status: number,
duration?: number,
data?: Record<string, unknown>,
) => {
logger.info(
{ method, path, status, duration, ...(data || {}) },
"API response",
);
},
/**
* Log database operations
*/
database: (
operation: string,
table?: string,
data?: Record<string, unknown>,
) => {
logger.debug({ operation, table, ...(data || {}) }, "Database operation");
},
/**
* Log authentication events
*/
auth: (event: string, userId?: string, data?: Record<string, unknown>) => {
logger.info({ event, userId, ...(data || {}) }, "Authentication event");
},
};
export default log;