Skip to content

Commit 99b88e6

Browse files
committed
fix functionally changing options and added file/line/column to log
1 parent 912d0b5 commit 99b88e6

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
import {
12+
CallSite,
1213
CompleteLoggerOptions,
1314
LogContent,
1415
LoggerOptions,
@@ -19,6 +20,7 @@ import {
1920
import merge from '@eartharoid/deep-merge';
2021
import defaults from './defaults';
2122
import { inspect } from 'util';
23+
import { relative } from 'path';
2224
import * as transports from './transports';
2325

2426
module.exports = class Logger {
@@ -53,6 +55,11 @@ module.exports = class Logger {
5355
}
5456

5557
public log(namespace: string | null, level: LogLevel, ...content: LogContent) {
58+
const _prepareStackTrace = Error.prepareStackTrace; // eslint-disable-line no-underscore-dangle
59+
Error.prepareStackTrace = (_, stack) => stack;
60+
const stack = <Array<CallSite> | undefined>new Error().stack;
61+
// const stack = (<> new Error().stack).slice(2);
62+
Error.prepareStackTrace = _prepareStackTrace;
5663
const strings = content.map((c: unknown) => typeof c === 'string'
5764
? c
5865
: c instanceof Error
@@ -64,13 +71,15 @@ module.exports = class Logger {
6471
for (const transport of this._options.transports) {
6572
if (level.number >= this.levels.indexOf(transport.level)) {
6673
transport.write({
74+
column: stack ? stack[0]?.getColumnNumber() : null,
6775
content: strings.join(' '),
76+
file: stack ? stack[0]?.getFileName() ? relative(process.cwd(), <string>stack[0]?.getFileName()) : null : null,
6877
level,
78+
line: stack ? stack[0]?.getLineNumber() : null,
6979
namespace,
7080
timestamp: new Date()
7181
});
7282
}
73-
7483
}
7584
}
7685

@@ -79,7 +88,7 @@ module.exports = class Logger {
7988
}
8089

8190
set options(options) {
82-
this._options = options;
91+
this._options = merge(this._options, options);
8392
this._init();
8493
}
8594
};

src/transports/console/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export default class ConsoleTransport extends Transport {
2929
.replace(/{+ ?LEVEL ?}+/gm, log.level.name.toUpperCase())
3030
.replace(/{+ ?namespace ?}+/gm, log.namespace?.toLowerCase() ?? 'global')
3131
.replace(/{+ ?NAMESPACE ?}+/gm, log.namespace?.toUpperCase() ?? 'GLOBAL')
32+
.replace(/{+ ?file ?}+/gmi, log.file ?? 'unknown')
33+
.replace(/{+ ?line ?}+/gmi, String(log.line) ?? 'unknown')
34+
.replace(/{+ ?column ?}+/gmi, String(log.column) ?? 'unknown')
3235
.replace(/{+ ?content ?}+/gmi, log.content)
3336
.replace(/{+ ?timestamp ?}+/gmi, typeof this.options.timestamp === 'function'
3437
? this.options.timestamp(log.timestamp)

src/transports/file/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export default class FileTransport extends Transport {
7272
.replace(/{+ ?LEVEL ?}+/gm, log.level.name.toUpperCase())
7373
.replace(/{+ ?namespace ?}+/gm, log.namespace?.toLowerCase() ?? 'global')
7474
.replace(/{+ ?NAMESPACE ?}+/gm, log.namespace?.toUpperCase() ?? 'GLOBAL')
75+
.replace(/{+ ?file ?}+/gmi, log.file ?? 'unknown')
76+
.replace(/{+ ?line ?}+/gmi, String(log.line) ?? 'unknown')
77+
.replace(/{+ ?column ?}+/gmi, String(log.column) ?? 'unknown')
7578
.replace(/{+ ?content ?}+/gmi, log.content)
7679
.replace(/{+ ?timestamp ?}+/gmi, typeof this.options.timestamp === 'function'
7780
? this.options.timestamp(log.timestamp)

src/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import Transport from './transport';
22

3+
export interface CallSite {
4+
getThis(): unknown | undefined;
5+
getTypeName(): string | null;
6+
getFunction(): (...args: unknown[]) => unknown | undefined;
7+
getFunctionName(): string | null;
8+
getMethodName(): string | undefined;
9+
getFileName(): string | null;
10+
getLineNumber(): number | null;
11+
getColumnNumber(): number | null;
12+
getEvalOrigin(): string | undefined;
13+
isToplevel(): boolean;
14+
isEval(): boolean;
15+
isNative(): boolean;
16+
isConstructor(): boolean;
17+
}
18+
319
export type LogLevelType = 'debug' | 'info' | 'warn' | 'error';
420

521
export interface LogLevel {
@@ -27,8 +43,11 @@ export interface CompleteLoggerOptions {
2743
export type LogContent = Array<unknown>;
2844

2945
export interface Log {
46+
column: number | null,
3047
content: string,
48+
file: string | null,
3149
level: LogLevel,
50+
line: number | null,
3251
namespace: string | null,
3352
timestamp: Date
3453
}

test/test2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { FileTransport } = require('../dist/transports');
44
const log = new Logger({
55
transports: [
66
new Logger.transports.ConsoleTransport({
7-
format: '[{timestamp}] [{level}]: {content}',
7+
format: '[{timestamp}] [{level}] ({file}:{line}:{column}): {content}',
88
level: 'debug',
99
timestamp: 'HH:mm:ss'
1010
}),

0 commit comments

Comments
 (0)