Skip to content

Commit

Permalink
feat: Format date using https://github.com/lukeed/tinydate
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Apr 2, 2022
1 parent fe525b4 commit 77e9710
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
31 changes: 28 additions & 3 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
}
> = {},
) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
});
4 changes: 4 additions & 0 deletions src/nestolog-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
20 changes: 16 additions & 4 deletions src/nestologger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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;
}

Expand Down

0 comments on commit 77e9710

Please sign in to comment.