Skip to content

Instantly share code, notes, and snippets.

@sumskyi
Last active May 8, 2025 17:24
Show Gist options
  • Save sumskyi/27426b84a525645d1367cf92a05c3645 to your computer and use it in GitHub Desktop.
Save sumskyi/27426b84a525645d1367cf92a05c3645 to your computer and use it in GitHub Desktop.
import { environment } from '../../environments/environment';
export class Logger {
private static readonly LOG_PREFIX = '[NoteTaking]';
private static readonly COLORS = {
debug: 'color: blue',
info: 'color: green',
warn: 'color: yellow',
error: 'color: red',
fatal: 'color: darkred',
};
// Style for the prefix (e.g., [NoteTaking] DEBUG:)
private static readonly PREFIX_STYLE = 'color: gray; font-weight: bold';
private static createLogger(
method: (...args: unknown[]) => void,
prefix: string,
level: keyof typeof Logger.COLORS
): (...args: unknown[]) => void {
return (...args: unknown[]) => {
const formatString = `%c${prefix}: %c${args.join(' ')}`;
const prefixStyle = Logger.PREFIX_STYLE;
const messageStyle = Logger.COLORS[level];
// Log the message with styling
method(formatString, prefixStyle, messageStyle);
// Capture and log the stack trace to include the location where the logger was called from
console.trace();
};
}
// Debug logging (disabled in production)
static debug: (...args: unknown[]) => void = (() => {
if (!environment.production) {
return Logger.createLogger(console.debug, `${Logger.LOG_PREFIX} DEBUG`, 'debug');
}
return () => {};
})();
// Info logging
static info: (...args: unknown[]) => void = Logger.createLogger(
console.info,
`${Logger.LOG_PREFIX} INFO`,
'info'
);
// Warn logging
static warn: (...args: unknown[]) => void = Logger.createLogger(
console.warn,
`${Logger.LOG_PREFIX} WARN`,
'warn'
);
// Error logging
static error: (...args: unknown[]) => void = Logger.createLogger(
console.error,
`${Logger.LOG_PREFIX} ERROR`,
'error'
);
// Fatal logging (maps to error)
static fatal: (...args: unknown[]) => void = Logger.createLogger(
console.error,
`${Logger.LOG_PREFIX} FATAL`,
'fatal'
);
}
export const NOTE = Logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment