diff --git a/README.md b/README.md index 84c80d4..a476f46 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ app.useLogger(app.get(NestoLogger)); It's not recommended to use this logger in production, since it's relative slow. -## Options +### Options [Ololog configuration](https://github.com/xpl/ololog#configuration) and plus custom configuration: @@ -69,6 +69,14 @@ customLocatePosition: 'bottom' as 'bottom' | 'column' | 'context', customLocateColumnLimit: 30, ``` +## Create instance by static method + +```ts +const app = await NestFactory.create(AppModule, { + logger: NestoLogger.create(), +}); +``` + ## Development - ololog pipeline: stringify trim lines concat indent tag time locate join render returnValue diff --git a/package.json b/package.json index 936d99e..234cd0b 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "homepage": "https://github.com/unlight/nestolog#readme", "scripts": { + "testapp": "ts-node src/test-app", "test": "npm run eslint && npm run tscheck && npm run test:r", "test:r": "vitest run src", "test:cov": "vitest run src --coverage", diff --git a/src/index.spec.ts b/src/index.spec.ts index daa04d4..0c85a2c 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -32,6 +32,12 @@ function createOutput( return output; } +it('create static instance', () => { + const logger = NestoLogger.create(); + + expect(logger).toBeInstanceOf(NestoLogger); +}); + it('log', () => { const output = createOutput({}); expect(output).toMatch('INFO\t'); diff --git a/src/nestologger.service.ts b/src/nestologger.service.ts index 1f72c65..de387fc 100644 --- a/src/nestologger.service.ts +++ b/src/nestologger.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable, LoggerService, Optional } from '@nestjs/common'; +import { Inject, Injectable, LoggerService } from '@nestjs/common'; import ansicolor from 'ansicolor'; import ololog from 'ololog'; import StackTracey from 'stacktracey'; @@ -18,12 +18,16 @@ import { Entry } from './types'; @Injectable() export class NestoLogger implements LoggerService { verbose = this.debug.bind(this); + private logger: typeof ololog; constructor( @Inject(MODULE_OPTIONS_TOKEN) private readonly options: NestologOptions, - @Inject('ololog') @Optional() private readonly logger = ololog, ) { - this.logger = this.createLogger(logger); + this.logger = this.createLogger(ololog); + } + + static create(options?: Partial) { + return new NestoLogger({ ...nestologOptionsDefaults, ...options }); } private createLogger(logger: ololog) { diff --git a/src/test-app.ts b/src/test-app.ts index 7203ef9..c3ca693 100644 --- a/src/test-app.ts +++ b/src/test-app.ts @@ -34,7 +34,9 @@ export class UserModule {} export class AppModule {} const bootstrap = async (): Promise => { - const app = await NestFactory.create(AppModule); + const app = await NestFactory.create(AppModule, { + logger: NestoLogger.create({ tag: false, time: false }), + }); const port = process.env.PORT ?? 3333; app.useLogger(app.get(NestoLogger));