Skip to content

Commit

Permalink
Update logger.ts (#668)
Browse files Browse the repository at this point in the history
Created a separate utility function for colorizing log levels and
timestamps. Added comments and documentation to make the code more
understandable. Used template strings for better readability in the
"console.log" statement. Improved the way the log level is fetched by
creating a dedicated getLogLevel function.

Co-authored-by: Himanshu Dixit <[email protected]>
Co-authored-by: Viraj <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2024
1 parent 368f798 commit b23b111
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions js/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const levels = {
warn: 'WARN',
info: 'INFO',
debug: 'DEBUG'
};
} as const;

const colors = {
red: (str: string) => `\x1b[31m${str}\x1b[0m`,
Expand All @@ -15,7 +15,13 @@ const colors = {
gray: (str: string) => `\x1b[90m${str}\x1b[0m`
};

const colorize = (level: string, timestamp: string) => {
/**
* Colorize log level and timestamp.
* @param {string} level - The log level.
* @param {string} timestamp - The timestamp.
* @returns {Object} - Object containing colored level and timestamp.
*/
const colorize = (level: keyof typeof levels, timestamp: string): { level: string; timestamp: string } => {
switch (level) {
case 'error':
return { level: colors.red(levels[level]), timestamp: colors.gray(timestamp) };
Expand All @@ -30,18 +36,30 @@ const colorize = (level: string, timestamp: string) => {
}
};

/**
* Get the current log level from environment variables.
* @returns {string} - The current log level.
*/
const getLogLevel = (): keyof typeof levels => {
const envLevel = getEnvVariable("COMPOSIO_DEBUG", "0");
return envLevel === "1" ? 'debug' : 'info';
};

/**
* Logger utility to log messages with different levels.
*/
const logger = {
level: getEnvVariable("COMPOSIO_DEBUG", "0") === "1" ? 'debug' : 'info',
log: (level: string, message: string, meta?: any) => {
const timestamp = new Date().toLocaleTimeString();
level: getLogLevel(),
log: (level: keyof typeof levels, message: string, meta?: any): void => {
const timestamp = new Date().toLocaleString();
const { level: coloredLevel, timestamp: coloredTimestamp } = colorize(level, timestamp);
const metaInfo = meta ? ` - ${JSON.stringify(meta)}` : '';
console.log(`[${coloredLevel}] ${coloredTimestamp} ${message}${metaInfo}`);
},
error: (message: string, meta?: any) => logger.log('error', message, meta),
warn: (message: string, meta?: any) => logger.log('warn', message, meta),
info: (message: string, meta?: any) => logger.log('info', message, meta),
debug: (message: string, meta?: any) => logger.log('debug', message, meta)
error: (message: string, meta?: any): void => logger.log('error', message, meta),
warn: (message: string, meta?: any): void => logger.log('warn', message, meta),
info: (message: string, meta?: any): void => logger.log('info', message, meta),
debug: (message: string, meta?: any): void => logger.log('debug', message, meta)
};

export default logger;

0 comments on commit b23b111

Please sign in to comment.