Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logger.ts #668

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading