diff --git a/README.md b/README.md index b059d38..e827fd5 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,10 @@ It's not recommended to use this logger in production, since it's relative slow. [Ololog configuration](https://github.com/xpl/ololog#configuration) and plus custom configuration: ```ts +/** + * Format date using https://github.com/lukeed/tinydate + */ +timeFormat: undefined as undefined | string, /** * Limit of context message. */ @@ -65,6 +69,10 @@ customLocatePosition: 'bottom' as 'bottom' | 'column' | 'context', customLocateColumnLimit: 30, ``` +## Development + +- ololog pipeline: stringify trim lines concat indent tag time locate join render returnValue + ## Resources ### Context Candidates diff --git a/package.json b/package.json index 426846a..a85b550 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "string.bullet": "^1.0.12", "string.ify": "^1.0.64", "term-size": "2.X", + "tinydate": "^1.3.0", "wrap-ansi": "7.X" }, "devDependencies": { diff --git a/src/index.spec.ts b/src/index.spec.ts index f3174c1..97402d9 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -1,5 +1,5 @@ import ansicolor from 'ansicolor'; -import { expect, it } from 'vitest'; +import { expect, it, vi } from 'vitest'; import { messageColumnWidth } from './message-column-width'; import { NestologOptions, nestologOptionsDefaults } from './nestolog-options'; @@ -8,9 +8,10 @@ import { NestoLogger } from './nestologger.service'; function createOutput( options: Partial< NestologOptions & { - message?: string; + message?: string | Error; context?: string; method: 'log' | 'warn' | 'error' | 'debug'; + trace?: string; } > = {}, ) { @@ -25,7 +26,9 @@ function createOutput( }, }); const method = options.method ?? 'log'; - logger[method](message, context); + if (logger[method].length === 2) logger[method](message, context); + else if (logger[method].length === 3) + logger[method](message, options.trace, context); return output; } @@ -86,3 +89,25 @@ it('debug', () => { const output = createOutput({ method: 'debug' }); expect(output).toMatch('DEBUG\t'); }); + +it('timeFormat', () => { + vi.useFakeTimers(); + + vi.setSystemTime(new Date(2000, 1, 1, 10, 42, 56)); + + const output = createOutput({ + timeFormat: '{HH}:{mm}:{ss}', + }); + expect(output).toContain('10:42:56'); + + vi.useRealTimers(); +}); + +it('error stack trace', () => { + const output = createOutput({ + method: 'error', + message: new Error('foo'), + }); + + console.log('output', output); +}); diff --git a/src/nestolog-options.ts b/src/nestolog-options.ts index 6209a1e..8401d27 100644 --- a/src/nestolog-options.ts +++ b/src/nestolog-options.ts @@ -11,6 +11,10 @@ export const nestologOptionsDefaults = { time: true, locate: true, tag: true, + /** + * Format date using https://github.com/lukeed/tinydate + */ + timeFormat: undefined as undefined | string, /** * Limit of context message. */ diff --git a/src/nestologger.service.ts b/src/nestologger.service.ts index f9d0287..650d564 100644 --- a/src/nestologger.service.ts +++ b/src/nestologger.service.ts @@ -2,6 +2,7 @@ import { Inject, Injectable, LoggerService, Optional } from '@nestjs/common'; import ansicolor from 'ansicolor'; import ololog from 'ololog'; import StackTracey from 'stacktracey'; +import tinydate from 'tinydate'; import wrapAnsi from 'wrap-ansi'; import { messageColumnWidth } from './message-column-width'; @@ -13,12 +14,9 @@ import { import { bullet, stringify } from './string'; import { Entry } from './types'; -// ololog pipeline: stringify trim lines concat indent tag time locate join render returnValue - @Injectable() export class NestoLogger implements LoggerService { - // eslint-disable-next-line @typescript-eslint/unbound-method - verbose = this.debug; + verbose = this.debug.bind(this); constructor( @Inject(NESTOLOG_OPTIONS) private readonly options: NestologOptions, @@ -31,6 +29,7 @@ export class NestoLogger implements LoggerService { logger = logger.configure(this.options); const width = this.options.messageColumnWidth || messageColumnWidth(this.options, logger); + if (width && width > 0) { logger = logger.configure({ locate: false, @@ -40,6 +39,19 @@ export class NestoLogger implements LoggerService { }, }); } + + if (this.options.timeFormat) { + const format = tinydate(this.options.timeFormat); + logger = logger.configure({ + time: { + yes: true, + print: (date: Date) => { + return ansicolor.darkGray(format(date)); + }, + }, + }); + } + return logger; }