Skip to content

Commit

Permalink
feat: Locate position column
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Feb 18, 2021
1 parent 880cc0a commit 8186240
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class AppModule {}

```ts
/**
* Limit context to this number of chars.
* Limit of context message.
*/
contextLimit: 13,
/**
Expand All @@ -36,6 +36,21 @@ contextLimit: 13,
* If -1 disable
*/
messageColumnWidth: 0,
/**
* Alternative locate. Default ololog's locate add callee info to the last non-empty string
* Custom locate add callee info on next new line.
*/
customLocate: undefined as undefined | boolean | typeof customLocateDefault,
/**
* Place of callee info.
* 'bottom' - next on new line (default)
* 'column' - between tag and message columnized
*/
customLocatePosition: 'bottom' as 'bottom' | 'column',
/**
* Limit callee info length in case of customLocatePosition = 'column'
*/
customLocateColumnLimit: 30,
```

## Use as the main Nest Logger
Expand Down
44 changes: 34 additions & 10 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,49 @@ import ansicolor from 'ansicolor';
import expect from 'expect';

import { messageColumnWidth } from './message-column-width';
import { nestologOptionsDefaults } from './nestolog-options';
import { NestologOptions, nestologOptionsDefaults } from './nestolog-options';
import { NestoLogger } from './nestologger.service';

it('messageColumnWidth', () => {
const result = messageColumnWidth(nestologOptionsDefaults);
expect(result).not.toBeUndefined();
});

it('custom locate', () => {
function createOutput(
options: Partial<NestologOptions & { message?: string; context?: string }> = {},
) {
let output = '';
const message = options.message ?? 'Test Message';
const context = options.context ?? 'Context';
const logger = new NestoLogger({
...nestologOptionsDefaults,
customLocate: true,
...options,
render: (input: string) => {
output = ansicolor.strip(input);
},
});
logger.log('Test message', 'Contex');
logger.log(message, context);
return output;
}

it('messageColumnWidth', () => {
const result = messageColumnWidth(nestologOptionsDefaults);
expect(result).not.toBeUndefined();
});

it('custom locate bottom', () => {
const output = createOutput({
customLocate: true,
customLocatePosition: 'bottom',
});
const lines = output.split('\n');
expect(lines).toHaveLength(2);
expect(lines[0]!.indexOf('Test message')).toEqual(lines[1]?.indexOf('<anonymous>'));
expect(lines[0]!.indexOf('Test Message')).toEqual(
lines[1]?.indexOf('createOutput'),
);
});

it('custom locate column', () => {
const output = createOutput({
customLocate: true,
customLocatePosition: 'column',
});
const lines = output.split('\n');
expect(lines).toHaveLength(1);
expect(output).toContain('Context index.spec.ts');
});
33 changes: 29 additions & 4 deletions src/nestolog-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { Entry } from './types';

export const NESTOLOG_OPTIONS = Symbol('NESTOLOG_OPTIONS');

export type NestologOptions = typeof nestologOptionsDefaults &
Parameters<typeof ololog.configure>[0];

export const nestologOptionsDefaults = {
time: true,
locate: true,
tag: true,
/**
* Limit of context message.
*/
contextLimit: 13,
/**
* Word wrap width for message.
Expand All @@ -20,9 +26,23 @@ export const nestologOptionsDefaults = {
* Custom locate add callee info on next new line.
*/
customLocate: undefined as undefined | boolean | typeof customLocateDefault,
/**
* Place of callee info.
* 'bottom' - next on new line (default)
* 'column' - between tag and message columnized
*/
customLocatePosition: 'bottom' as 'bottom' | 'column',
/**
* Limit callee info length in case of customLocatePosition = 'column'
*/
customLocateColumnLimit: 30,
};

export function customLocateDefault({ calleeShort, fileName, line }: Entry): string {
export function customLocateDefault(
{ calleeShort, fileName, line }: Entry,
options: NestologOptions,
): string {
const { customLocatePosition, customLocateColumnLimit } = options;
let result = '';
if (calleeShort) {
result += calleeShort;
Expand All @@ -35,8 +55,13 @@ export function customLocateDefault({ calleeShort, fileName, line }: Entry): str
result += ':';
result += line;
}
if (
customLocatePosition === 'column' &&
result.length > customLocateColumnLimit &&
fileName &&
line
) {
result = `${fileName}:${line}`;
}
return result;
}

export type NestologOptions = typeof nestologOptionsDefaults &
Parameters<typeof ololog.configure>[0];
17 changes: 14 additions & 3 deletions src/nestologger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,33 @@ export class NestoLogger implements LoggerService {
}

private concatContext({ context, where }: { context?: string; where?: Entry }) {
const { contextLimit } = this.options;
const {
contextLimit,
customLocatePosition,
customLocateColumnLimit,
} = this.options;
const customLocate =
this.options.customLocate === true
? customLocateDefault
: this.options.customLocate;
return (lines: string[]) => {
if (where && customLocate) {
lines.push(ansicolor.darkGray(customLocate(where)));
let calleeInfo = customLocate(where, this.options);
if (customLocatePosition === 'column') {
calleeInfo = calleeInfo.padEnd(customLocateColumnLimit);
calleeInfo = stringify.limit(calleeInfo, customLocateColumnLimit);
lines = bullet(ansicolor.darkGray(calleeInfo) + ' ', lines);
} else {
lines.push(ansicolor.darkGray(calleeInfo));
}
}
if (context) {
if (contextLimit > 0) {
context = stringify
.limit(context, contextLimit)
.padEnd(contextLimit);
}
return bullet(ansicolor.yellow(context + ' '), lines);
lines = bullet(ansicolor.yellow(context + ' '), lines);
}
return lines;
};
Expand Down

0 comments on commit 8186240

Please sign in to comment.