Skip to content

Commit

Permalink
fix functionally changing options and added file/line/column to log
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Oct 1, 2021
1 parent 912d0b5 commit 99b88e6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'use strict';

import {
CallSite,
CompleteLoggerOptions,
LogContent,
LoggerOptions,
Expand All @@ -19,6 +20,7 @@ import {
import merge from '@eartharoid/deep-merge';
import defaults from './defaults';
import { inspect } from 'util';
import { relative } from 'path';
import * as transports from './transports';

module.exports = class Logger {
Expand Down Expand Up @@ -53,6 +55,11 @@ module.exports = class Logger {
}

public log(namespace: string | null, level: LogLevel, ...content: LogContent) {
const _prepareStackTrace = Error.prepareStackTrace; // eslint-disable-line no-underscore-dangle
Error.prepareStackTrace = (_, stack) => stack;
const stack = <Array<CallSite> | undefined>new Error().stack;
// const stack = (<> new Error().stack).slice(2);
Error.prepareStackTrace = _prepareStackTrace;
const strings = content.map((c: unknown) => typeof c === 'string'
? c
: c instanceof Error
Expand All @@ -64,13 +71,15 @@ module.exports = class Logger {
for (const transport of this._options.transports) {
if (level.number >= this.levels.indexOf(transport.level)) {
transport.write({
column: stack ? stack[0]?.getColumnNumber() : null,
content: strings.join(' '),
file: stack ? stack[0]?.getFileName() ? relative(process.cwd(), <string>stack[0]?.getFileName()) : null : null,
level,
line: stack ? stack[0]?.getLineNumber() : null,
namespace,
timestamp: new Date()
});
}

}
}

Expand All @@ -79,7 +88,7 @@ module.exports = class Logger {
}

set options(options) {
this._options = options;
this._options = merge(this._options, options);
this._init();
}
};
Expand Down
3 changes: 3 additions & 0 deletions src/transports/console/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export default class ConsoleTransport extends Transport {
.replace(/{+ ?LEVEL ?}+/gm, log.level.name.toUpperCase())
.replace(/{+ ?namespace ?}+/gm, log.namespace?.toLowerCase() ?? 'global')
.replace(/{+ ?NAMESPACE ?}+/gm, log.namespace?.toUpperCase() ?? 'GLOBAL')
.replace(/{+ ?file ?}+/gmi, log.file ?? 'unknown')
.replace(/{+ ?line ?}+/gmi, String(log.line) ?? 'unknown')
.replace(/{+ ?column ?}+/gmi, String(log.column) ?? 'unknown')
.replace(/{+ ?content ?}+/gmi, log.content)
.replace(/{+ ?timestamp ?}+/gmi, typeof this.options.timestamp === 'function'
? this.options.timestamp(log.timestamp)
Expand Down
3 changes: 3 additions & 0 deletions src/transports/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export default class FileTransport extends Transport {
.replace(/{+ ?LEVEL ?}+/gm, log.level.name.toUpperCase())
.replace(/{+ ?namespace ?}+/gm, log.namespace?.toLowerCase() ?? 'global')
.replace(/{+ ?NAMESPACE ?}+/gm, log.namespace?.toUpperCase() ?? 'GLOBAL')
.replace(/{+ ?file ?}+/gmi, log.file ?? 'unknown')
.replace(/{+ ?line ?}+/gmi, String(log.line) ?? 'unknown')
.replace(/{+ ?column ?}+/gmi, String(log.column) ?? 'unknown')
.replace(/{+ ?content ?}+/gmi, log.content)
.replace(/{+ ?timestamp ?}+/gmi, typeof this.options.timestamp === 'function'
? this.options.timestamp(log.timestamp)
Expand Down
19 changes: 19 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import Transport from './transport';

export interface CallSite {
getThis(): unknown | undefined;
getTypeName(): string | null;
getFunction(): (...args: unknown[]) => unknown | undefined;
getFunctionName(): string | null;
getMethodName(): string | undefined;
getFileName(): string | null;
getLineNumber(): number | null;
getColumnNumber(): number | null;
getEvalOrigin(): string | undefined;
isToplevel(): boolean;
isEval(): boolean;
isNative(): boolean;
isConstructor(): boolean;
}

export type LogLevelType = 'debug' | 'info' | 'warn' | 'error';

export interface LogLevel {
Expand Down Expand Up @@ -27,8 +43,11 @@ export interface CompleteLoggerOptions {
export type LogContent = Array<unknown>;

export interface Log {
column: number | null,
content: string,
file: string | null,
level: LogLevel,
line: number | null,
namespace: string | null,
timestamp: Date
}
Expand Down
2 changes: 1 addition & 1 deletion test/test2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { FileTransport } = require('../dist/transports');
const log = new Logger({
transports: [
new Logger.transports.ConsoleTransport({
format: '[{timestamp}] [{level}]: {content}',
format: '[{timestamp}] [{level}] ({file}:{line}:{column}): {content}',
level: 'debug',
timestamp: 'HH:mm:ss'
}),
Expand Down

0 comments on commit 99b88e6

Please sign in to comment.