@matrixai/logger
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -7,4 +7,5 @@ import type { LogFormatter } from './types'; | ||
declare const msg: unique symbol; | ||
declare const trace: unique symbol; | ||
declare function format(strings: TemplateStringsArray, ...values: Array<any>): LogFormatter; | ||
declare const formatter: LogFormatter; | ||
export { level, key, keys, date, msg, format, formatter }; | ||
export { level, key, keys, date, msg, trace, format, formatter }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formatter = exports.format = exports.msg = exports.date = exports.keys = exports.key = exports.level = void 0; | ||
exports.formatter = exports.format = exports.trace = exports.msg = exports.date = exports.keys = exports.key = exports.level = void 0; | ||
const types_1 = require("./types"); | ||
@@ -15,2 +15,4 @@ const level = Symbol('level'); | ||
exports.msg = msg; | ||
const trace = Symbol('trace'); | ||
exports.trace = trace; | ||
function format(strings, ...values) { | ||
@@ -42,2 +44,17 @@ return (record) => { | ||
} | ||
else if (value === trace) { | ||
const errorStack = new Error().stack; | ||
if (errorStack != null) { | ||
const splitErrorStack = errorStack.split('\n'); | ||
const position = splitErrorStack.findIndex((value) => /Logger\.log/.test(value)) ?? | ||
0; | ||
const formattedStack = splitErrorStack | ||
.splice(position + 2) | ||
.join('\n'); | ||
result += '\n' + formattedStack; | ||
} | ||
else { | ||
result += ''; | ||
} | ||
} | ||
else { | ||
@@ -44,0 +61,0 @@ result += value.toString(); |
@@ -6,3 +6,3 @@ import type { LogRecord, LogFormatter } from './types'; | ||
setFormatter(formatter: LogFormatter): void; | ||
handle(record: LogRecord): void; | ||
handle(record: LogRecord, format?: LogFormatter): void; | ||
format(record: LogRecord): string; | ||
@@ -9,0 +9,0 @@ abstract emit(output: string): void; |
@@ -34,4 +34,4 @@ "use strict"; | ||
} | ||
handle(record) { | ||
const output = this.format(record); | ||
handle(record, format) { | ||
const output = format != null ? format(record) : this.format(record); | ||
this.emit(output); | ||
@@ -38,0 +38,0 @@ } |
@@ -9,3 +9,3 @@ "use strict"; | ||
emit(output) { | ||
console.log(output); | ||
console.error(output); | ||
} | ||
@@ -12,0 +12,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import type { ToString, LogRecord } from './types'; | ||
import type { ToString, LogRecord, LogFormatter } from './types'; | ||
import type Handler from './Handler'; | ||
@@ -22,10 +22,10 @@ import { LogLevel } from './types'; | ||
hasHandlers(): boolean; | ||
debug(data: ToString): void; | ||
info(data: ToString): void; | ||
warn(data: ToString): void; | ||
error(data: ToString): void; | ||
protected log(msg: string, level: LogLevel): void; | ||
debug(data: ToString, format?: LogFormatter): void; | ||
info(data: ToString, format?: LogFormatter): void; | ||
warn(data: ToString, format?: LogFormatter): void; | ||
error(data: ToString, format?: LogFormatter): void; | ||
protected log(msg: string, level: LogLevel, format?: LogFormatter): void; | ||
protected makeRecord(msg: string, level: LogLevel): LogRecord; | ||
protected callHandlers(record: LogRecord): void; | ||
protected callHandlers(record: LogRecord, format?: LogFormatter): void; | ||
} | ||
export default Logger; |
@@ -59,18 +59,18 @@ "use strict"; | ||
} | ||
debug(data) { | ||
this.log(data.toString(), types_1.LogLevel.DEBUG); | ||
debug(data, format) { | ||
this.log(data.toString(), types_1.LogLevel.DEBUG, format); | ||
} | ||
info(data) { | ||
this.log(data.toString(), types_1.LogLevel.INFO); | ||
info(data, format) { | ||
this.log(data.toString(), types_1.LogLevel.INFO, format); | ||
} | ||
warn(data) { | ||
this.log(data.toString(), types_1.LogLevel.WARN); | ||
warn(data, format) { | ||
this.log(data.toString(), types_1.LogLevel.WARN, format); | ||
} | ||
error(data) { | ||
this.log(data.toString(), types_1.LogLevel.ERROR); | ||
error(data, format) { | ||
this.log(data.toString(), types_1.LogLevel.ERROR, format); | ||
} | ||
log(msg, level) { | ||
log(msg, level, format) { | ||
const record = this.makeRecord(msg, level); | ||
if (level >= this.getEffectiveLevel()) { | ||
this.callHandlers(record); | ||
this.callHandlers(record, format); | ||
} | ||
@@ -87,8 +87,8 @@ } | ||
} | ||
callHandlers(record) { | ||
callHandlers(record, format) { | ||
for (const handler of this.handlers) { | ||
handler.handle(record); | ||
handler.handle(record, format); | ||
} | ||
if (this.parent) { | ||
this.parent.callHandlers(record); | ||
this.parent.callHandlers(record, format); | ||
} | ||
@@ -95,0 +95,0 @@ } |
{ | ||
"name": "@matrixai/logger", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"author": "Scott Morris", | ||
@@ -19,3 +19,4 @@ "description": "Python-like JavaScript Logger", | ||
"lint": "eslint '{src,tests}/**/*.{js,ts}'", | ||
"lintfix": "eslint '{src,tests}/**/*.{js,ts}' --fix" | ||
"lintfix": "eslint '{src,tests}/**/*.{js,ts}' --fix", | ||
"docs": "rm -r ./docs || true; typedoc --gitRevision master --tsconfig ./tsconfig.build.json --out ./docs src" | ||
}, | ||
@@ -22,0 +23,0 @@ "devDependencies": { |
@@ -32,2 +32,10 @@ # js-logger | ||
### Docs Generation | ||
```sh | ||
npm run docs | ||
``` | ||
See the docs at: https://matrixai.github.io/js-logger/ | ||
### Publishing | ||
@@ -34,0 +42,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
35176
400
50