Skip to content

Commit

Permalink
feat: Add static method create
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Dec 8, 2024
1 parent a48a3f9 commit b7ce3a9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
10 changes: 7 additions & 3 deletions src/nestologger.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<NestologOptions>) {
return new NestoLogger({ ...nestologOptionsDefaults, ...options });
}

private createLogger(logger: ololog) {
Expand Down
4 changes: 3 additions & 1 deletion src/test-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export class UserModule {}
export class AppModule {}

const bootstrap = async (): Promise<void> => {
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));

Expand Down

0 comments on commit b7ce3a9

Please sign in to comment.