Skip to content

Instantly share code, notes, and snippets.

@EndyKaufman
Last active November 3, 2021 15:02
Show Gist options
  • Save EndyKaufman/ff84d84c2caa601aa8a305ac996bf347 to your computer and use it in GitHub Desktop.
Save EndyKaufman/ff84d84c2caa601aa8a305ac996bf347 to your computer and use it in GitHub Desktop.
CoreLogger.ts
import { ConsoleLogger } from '@nestjs/common';
export class CoreLogger extends ConsoleLogger {
public static excludeContexts: string[] = [];
public static addExcludeContexts(excludeContext: string) {
CoreLogger.excludeContexts.push(excludeContext);
}
/**
* Write a 'log' level log.
*/
log(message: any, ...optionalParams: any[]) {
if (!CoreLogger.excludeContexts.includes(optionalParams[0])) {
super.log(message, ...optionalParams);
}
}
/**
* Write an 'error' level log.
*/
error(message: any, ...optionalParams: any[]) {
if (!CoreLogger.excludeContexts.includes(optionalParams[1]) && !CoreLogger.excludeContexts.find((ctx) => String(message).includes(ctx))) {
super.error(message, ...optionalParams);
}
}
/**
* Write a 'warn' level log.
*/
warn(message: any, ...optionalParams: any[]) {
if (!CoreLogger.excludeContexts.includes(optionalParams[0])) {
super.warn(message, ...optionalParams);
}
}
/**
* Write a 'debug' level log.
*/
debug(message: any, ...optionalParams: any[]) {
if (!CoreLogger.excludeContexts.includes(optionalParams[0])) {
super.debug(message, ...optionalParams);
}
}
/**
* Write a 'verbose' level log.
*/
verbose(message: any, ...optionalParams: any[]) {
if (!CoreLogger.excludeContexts.includes(optionalParams[0])) {
super.verbose(message, ...optionalParams);
}
}
}
import { CoreLogger } from './CoreLogger.ts';
CoreLogger.addExcludeContexts(FeatureService.name);
CoreLogger.addExcludeContexts(AuthGraphqlGuard.name);
CoreLogger.addExcludeContexts(UnauthorizedException.name);
CoreLogger.addExcludeContexts('Unauthorized');
// or from process.env
// (process.env.LOGGER_EXCLUDE_CONTEXTS! || '').split(',').forEach((contextName) => CoreLogger.addExcludeContexts(contextName));
async function bootstrap() {
const app = await NestFactory.create(AppModule, { logger: new CoreLogger() });
const port = process.env.PORT || 5000;
await app.listen(port, () => {
logger.log('Listening at http://localhost:' + port);
});
}
bootstrap()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment