@nestjs/common#LoggerService TypeScript Examples

The following examples show how to use @nestjs/common#LoggerService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: logger.ts    From MyAPI with MIT License 6 votes vote down vote up
export function createLogger (): LoggerService {
  const winstonOptions : WinstonModuleOptions = {
    transports: [
      new transports.Console({
        format: format.combine(
          format.timestamp(),
          utilities.format.nestLike(),
        ),
        level: 'debug'
      }),
      new transports.File({
        format: format.combine(
          format.timestamp(),
          utilities.format.nestLike(),
        ),
        filename: 'errors.log',
        level: 'error'
      }),
      new transports.File({
        format: format.combine(
          format.timestamp(),
          utilities.format.nestLike(),
        ),
        filename: 'warnings.log',
        level: 'warning'
      }),
      new transports.File({
        format: format.combine(
          format.timestamp(),
          utilities.format.nestLike(),
        ),
        filename: 'critical.log',
        level: 'crit'
      })
    ]
  }

  return WinstonModule.createLogger(winstonOptions)
}
Example #2
Source File: logger.ts    From nest-amqp with MIT License 6 votes vote down vote up
export class Logger implements LoggerService {
  private static instance: LoggerService = new NestLogger();

  constructor(private readonly context = '') {}

  public static overrideLogger(logger: LoggerService) {
    Logger.instance = logger;
  }

  public log(message: any, context?: string): void {
    Logger.instance.log(message, context || this.context);
  }

  public error(message: any, trace?: string, context?: string): void {
    Logger.instance.error(message, trace, context || this.context);
  }

  public warn(message: any, context?: string): void {
    Logger.instance.warn(message, context || this.context);
  }

  public debug(message: any, context?: string): void {
    Logger.instance.debug(message, context || this.context);
  }

  public verbose(message: any, context?: string): void {
    Logger.instance.verbose(message, context || this.context);
  }
}
Example #3
Source File: logger.mock.ts    From nest-amqp with MIT License 6 votes vote down vote up
/* istanbul ignore file */
/* eslint-disable @typescript-eslint/no-unused-vars */

export class LoggerMock implements LoggerService {
  public log(message: any, context?: string): void {}
  public error(message: any, trace?: string, context?: string): void {}
  public warn(message: any, context?: string): void {}
  public debug(message: any, context?: string): void {}
  public verbose(message: any, context?: string): void {}
}
Example #4
Source File: noop-logger.ts    From pebula-node with MIT License 6 votes vote down vote up
export class NoopLogger implements LoggerService {

  static readonly shared = new NoopLogger();

  log(message: any, context?: string): any { }
  error(message: any, trace?: string, context?: string): any { }
  warn(message: any, context?: string): any { }
  debug(message: any, context?: string): any { }
  verbose(message: any, context?: string): any { }
}
Example #5
Source File: LoggerService.ts    From pandaid with MIT License 6 votes vote down vote up
export class PinoLoggerService implements LoggerService {
  private logger: pino.Logger

  constructor(logger: pino.Logger) {
    this.logger = logger
  }

  log(message: string) {
    this.logger.info(message)
  }

  error(message: string, trace: string) {
    this.logger.error(message, {
      trace
    })
  }

  warn(message: string) {
    this.logger.warn(message)
  }

  debug(message: string) {
    this.logger.debug(message)
  }

  verbose(message: string) {
    this.logger.debug(message)
  }
}
Example #6
Source File: logger.service.ts    From mamori-i-japan-api with BSD 2-Clause "Simplified" License 5 votes vote down vote up
// We inject a transient instance of the Logger into our feature modules
// so that each one has its own custom context.

@Injectable({ scope: Scope.TRANSIENT })
// export class AppLogger extends Logger {}
export class AppLogger implements LoggerService {
  private context?: string
  private winstonLogger: Logger

  public setContext(context: string) {
    this.context = context
  }

  constructor() {
    //
    // As of winston@3, the default logging format is JSON.
    //
    this.winstonLogger = createLogger({
      transports: [new transports.Console()],
    })
  }

  log(message: any, context?: string) {
    return this.winstonLogger.info(message, { context: context || this.context })
  }

  error(message: any, trace?: string, context?: string): any {
    return this.winstonLogger.error(message, { trace, context: context || this.context })
  }

  warn(message: any, context?: string): any {
    return this.winstonLogger.warn(message, { context: context || this.context })
  }

  debug(message: any, context?: string): any {
    return this.winstonLogger.debug(message, { context: context || this.context })
  }

  verbose(message: any, context?: string): any {
    return this.winstonLogger.verbose(message, { context: context || this.context })
  }
}
Example #7
Source File: documentType.controller.ts    From MyAPI with MIT License 5 votes vote down vote up
constructor(
    private readonly userService: UserService,
    private readonly documentTypeService: DocumentTypeService,
    @Inject(Logger) private readonly logger: LoggerService
  ) { }
Example #8
Source File: role.controller.ts    From MyAPI with MIT License 5 votes vote down vote up
constructor(
    private readonly roleService: RoleService,
    @Inject(Logger) private readonly logger: LoggerService
  ) { }
Example #9
Source File: user.controller.ts    From MyAPI with MIT License 5 votes vote down vote up
constructor(
    private readonly userService: UserService,
    @Inject(Logger) private readonly logger: LoggerService
  ) { }
Example #10
Source File: logger.service.ts    From 42_checkIn with GNU General Public License v3.0 5 votes vote down vote up
@Injectable()
export class MyLogger implements LoggerService {
  constructor(private readonly configService: ConfigService) {}

  logger_info = dailyfile({
    root: './logs',
    allLogsFileName: 'info.42CheckIn',
    stackIndex: 1,
  });

  logger_log = dailyfile({
    root: './logs',
    allLogsFileName: 'log.42CheckIn',
    stackIndex: 1,
    level: 'trace',
  });

  logger_error = dailyfile({
    root: './logs',
    allLogsFileName: 'error.42CheckIn',
    stackIndex: 1,
  });

  logger_debug = dailyfile({
    root: './logs',
    allLogsFileName: 'debug.42CheckIn',
    stackIndex: 1,
    level: this.configService.get('log.debug') === true ? 'debug' : 'error',
  });

  log(...trace: any[]) {
    this.logger_log.trace(trace);
  }
  error(...trace: any[]) {
    this.logger_error.error(trace);
  }
  warn(...trace: any[]) {
    this.logger_log.warn(trace);
  }
  debug(...trace: any[]) {
    this.logger_debug.debug(trace);
  }
  info(...trace: any[]) {
    this.logger_info.info(trace);
  }
}
Example #11
Source File: service-bus-client.ts    From pebula-node with MIT License 5 votes vote down vote up
protected readonly sbLogger: LoggerService;
Example #12
Source File: configurator.ts    From pebula-node with MIT License 5 votes vote down vote up
private sbLogger: LoggerService = NoopLogger.shared;
Example #13
Source File: dynamoose-logger.provider.ts    From nestjs-dynamoose with MIT License 5 votes vote down vote up
constructor(private readonly logger: LoggerService) {}
Example #14
Source File: service-bus-server.ts    From pebula-node with MIT License 5 votes vote down vote up
protected sbLogger: LoggerService = NoopLogger.shared;
Example #15
Source File: logger.ts    From nest-keycloak-connect with MIT License 5 votes vote down vote up
/**
 * Wrapper for Nest Logger. Will be removed in 2.0.
 */
export class KeycloakLogger implements LoggerService {
  private logger = new Logger('Keycloak');
  private logLevels: LogLevel[];
  private readonly DEFAULT_LOG_LEVEL = 'log';

  constructor(providedLogLevels: LogLevel[]) {
    this.isLogLevelSet(providedLogLevels)
      ? (this.logLevels = providedLogLevels)
      : this.setDefaultLogLevel();
  }

  private setDefaultLogLevel = () => {
    this.logLevels = [this.DEFAULT_LOG_LEVEL];
    this.logger.verbose(
      'No LogLevel for KeycloakLogger provided; falling back to default: ' +
        this.DEFAULT_LOG_LEVEL,
    );
  };

  private isLogLevelSet = (logLevels: LogLevel[]) =>
    Array.isArray(logLevels) && logLevels.length;

  log(message: any, context?: string) {
    this.callWrapped('log', message, context);
  }

  error(message: any, trace?: string, context?: string) {
    this.callWrapped('error', message, context);
  }

  warn(message: any, context?: string) {
    this.callWrapped('warn', message, context);
  }

  debug?(message: any, context?: string) {
    this.callWrapped('debug', message, context);
  }

  verbose?(message: any, context?: string) {
    this.callWrapped('verbose', message, context);
  }

  private isLogLevelEnabled(level: LogLevel): boolean {
    return this.logLevels.includes(level);
  }

  private callWrapped(
    name: 'log' | 'warn' | 'debug' | 'verbose' | 'error',
    message: any,
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    context?: string,
  ) {
    if (!this.isLogLevelEnabled(name)) {
      return;
    }
    const func = this.logger[name];

    func && func.call(this.logger, message);
  }
}
Example #16
Source File: logger.spec.ts    From nest-amqp with MIT License 5 votes vote down vote up
describe('Logger', () => {
  let logger: Logger;
  let loggerMock: LoggerService;

  beforeEach(() => {
    logger = new Logger(getLoggerContext('test'));
    loggerMock = {
      log: jest.fn(),
      error: jest.fn(),
      warn: jest.fn(),
      debug: jest.fn(),
      verbose: jest.fn(),
    };
    Logger.overrideLogger(loggerMock);
  });

  it('should log log messages', () => {
    logger.log('message', 'context');

    expect(loggerMock.log).toBeCalled();
  });

  it('should log warn messages', () => {
    logger.warn('message');

    expect(loggerMock.warn).toBeCalled();
  });

  it('should log error messages', () => {
    logger.error('message');

    expect(loggerMock.error).toBeCalled();
  });

  it('should log debug messages', () => {
    logger.debug('message');

    expect(loggerMock.debug).toBeCalled();
  });

  it('should log verbose messages', () => {
    logger.verbose('message');

    expect(loggerMock.verbose).toBeCalled();
  });

  it('should log without context', () => {
    logger = new Logger();

    expect((logger as any).context).toBe('');
  });
});
Example #17
Source File: logger.ts    From nest-amqp with MIT License 5 votes vote down vote up
public static overrideLogger(logger: LoggerService) {
    Logger.instance = logger;
  }
Example #18
Source File: logger.ts    From nest-amqp with MIT License 5 votes vote down vote up
private static instance: LoggerService = new NestLogger();
Example #19
Source File: nest-application-context.d.ts    From nest-jaeger with MIT License 5 votes vote down vote up
useLogger(logger: LoggerService): void;