lambda-log
Advanced tools
Comparing version 4.0.0-beta.4 to 4.0.0-beta.5
@@ -25,3 +25,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Types = exports.LogMessage = exports.LambdaLog = void 0; | ||
exports.Types = exports.formatters = exports.LogMessage = exports.LambdaLog = void 0; | ||
const LambdaLog_js_1 = __importDefault(require("./LambdaLog.js")); | ||
@@ -31,4 +31,6 @@ exports.LambdaLog = LambdaLog_js_1.default; | ||
exports.LogMessage = LogMessage_js_1.default; | ||
const formatters = __importStar(require("./formatters/index.js")); | ||
exports.formatters = formatters; | ||
const Types = __importStar(require("./typings.js")); | ||
exports.Types = Types; | ||
exports.default = new LambdaLog_js_1.default(); |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,5 +25,6 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defaultOptions = void 0; | ||
exports.formatters = exports.defaultOptions = void 0; | ||
const events_1 = require("events"); | ||
const LogMessage_js_1 = __importDefault(require("./LogMessage.js")); | ||
const logFormatters = __importStar(require("./formatters/index.js")); | ||
const utils_js_1 = require("./utils.js"); | ||
@@ -44,4 +64,6 @@ const levels = [{ | ||
messageKey: 'msg', | ||
tagsKey: '__tags' | ||
tagsKey: '__tags', | ||
onFormat: logFormatters.json() | ||
}; | ||
exports.formatters = logFormatters; | ||
class LambdaLog extends events_1.EventEmitter { | ||
@@ -60,3 +82,2 @@ /** | ||
* @readonly | ||
* @type {LambdaLog} | ||
*/ | ||
@@ -66,3 +87,2 @@ this.LambdaLog = LambdaLog; | ||
* Access to the uninstantiated LogMessage class. | ||
* @type {LogMessage} | ||
*/ | ||
@@ -96,3 +116,2 @@ this.LogMessage = LogMessage_js_1.default; | ||
* Returns the console object to use for logging. | ||
* @readonly | ||
* @private | ||
@@ -112,7 +131,7 @@ * @returns {ConsoleObject} The configured console object or `console` if none is provided. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {object} [meta={}] Optional meta data to attach to the log. | ||
* @param {object|string|number} [meta={}] Optional meta data to attach to the log. | ||
* @param {string[]} [tags=[]] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
log(level, msg, meta = {}, tags = []) { | ||
_log(level, msg, meta, tags) { | ||
const lvl = this.getLevel(level); | ||
@@ -122,5 +141,2 @@ if (!lvl) { | ||
} | ||
// Check if we can log this level | ||
if (lvl.idx > this.maxLevelIdx) | ||
return false; | ||
// Generate the log message instance | ||
@@ -133,13 +149,16 @@ const message = new this.LogMessage({ | ||
}, this.options); | ||
const consoleObj = this.console; | ||
// Log the message to the console | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
consoleObj[lvl.method](message.toString()); | ||
/** | ||
* The log event is emitted (using EventEmitter) for every log generated. This allows for custom integrations, such as logging to a thrid-party service. | ||
* This event is emitted with the [LogMessage](#logmessage) instance for the log. You may control events using all the methods of EventEmitter. | ||
* @event LambdaLog#log | ||
* @type {LogMessage} | ||
*/ | ||
this.emit('log', message); | ||
// Check if we can log this level | ||
if (lvl.idx <= this.maxLevelIdx) { | ||
const consoleObj = this.console; | ||
// Log the message to the console | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
consoleObj[lvl.method](message.toString()); | ||
/** | ||
* The log event is emitted (using EventEmitter) for every log generated. This allows for custom integrations, such as logging to a thrid-party service. | ||
* This event is emitted with the [LogMessage](#logmessage) instance for the log. You may control events using all the methods of EventEmitter. | ||
* @event LambdaLog#log | ||
* @type {LogMessage} | ||
*/ | ||
this.emit('log', message); | ||
} | ||
return message; | ||
@@ -151,8 +170,8 @@ } | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
trace(msg, meta, tags) { | ||
return this.log('trace', msg, meta, tags); | ||
return this._log('trace', msg, meta, tags); | ||
} | ||
@@ -163,8 +182,8 @@ /** | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
debug(msg, meta, tags) { | ||
return this.log('debug', msg, meta, tags); | ||
return this._log('debug', msg, meta, tags); | ||
} | ||
@@ -175,19 +194,30 @@ /** | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
info(msg, meta, tags) { | ||
return this.log('info', msg, meta, tags); | ||
return this._log('info', msg, meta, tags); | ||
} | ||
/** | ||
* Alias for `info`. | ||
* @template T The type of the message to log. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
log(msg, meta, tags) { | ||
return this._log('info', msg, meta, tags); | ||
} | ||
/** | ||
* Logs a message at the `warn` log level. | ||
* @template T The type of the message to log. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
warn(msg, meta, tags) { | ||
return this.log('warn', msg, meta, tags); | ||
return this._log('warn', msg, meta, tags); | ||
} | ||
@@ -198,8 +228,8 @@ /** | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
error(msg, meta, tags) { | ||
return this.log('error', msg, meta, tags); | ||
return this._log('error', msg, meta, tags); | ||
} | ||
@@ -210,8 +240,8 @@ /** | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
fatal(msg, meta, tags) { | ||
return this.log('fatal', msg, meta, tags); | ||
return this._log('fatal', msg, meta, tags); | ||
} | ||
@@ -221,3 +251,2 @@ /** | ||
* wrap them in an if statement. The log level will be `error`. | ||
* @since 1.4.0 | ||
* @template T The type of the message to log. | ||
@@ -233,11 +262,10 @@ * @param {*} test Value to test for a falsy value. | ||
return false; | ||
return this.log('error', msg, meta, tags); | ||
return this._log('error', msg, meta, tags); | ||
} | ||
/** | ||
* Generates a log message with the result or error provided by a promise. Useful for debugging and testing. | ||
* @since 2.3.0 | ||
* @param {Promise<*>} promise Promise to log the results of. | ||
* @param {object} [meta={}] Optional meta data to attach to the log. | ||
* @param {string[]} [tags=[]] Additional tags to append to this log. | ||
* @returns {Promise<LogMessage | false>} A Promise that resolves with the log message. | ||
* @returns {Promise<LogMessage>} A Promise that resolves with the log message. | ||
*/ | ||
@@ -248,6 +276,6 @@ async result(promise, meta, tags) { | ||
.then(value => { | ||
resolve(this.log('info', value, meta, tags)); | ||
resolve(this._log('info', value, meta, tags)); | ||
}) | ||
.catch(err => { | ||
resolve(this.log('error', err, meta, tags)); | ||
resolve(this._log('error', err, meta, tags)); | ||
}); | ||
@@ -276,3 +304,2 @@ }); | ||
* Returns the index of the configured maximum log level. | ||
* @readonly | ||
* @private | ||
@@ -288,1 +315,3 @@ * @returns {number} The index of the configured maximum log level. | ||
exports.default = LambdaLog; | ||
LambdaLog.defaultOptions = exports.defaultOptions; | ||
LambdaLog.formatters = exports.formatters; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const utils_js_1 = require("./utils.js"); | ||
const json_js_1 = __importDefault(require("./formatters/json.js")); | ||
/** | ||
@@ -150,3 +151,3 @@ * The LogMessage class is a private/internal class that is used for generating log messages. All log methods return an instance of LogMessage allowing for a chainable api. | ||
/** | ||
* The full log object. This is the object used in logMessage.toJSON() and when the log is written to the console. | ||
* The log represented as an object that is useful for stringifying or pulling data from. | ||
* @returns {GenericRecord} The compiled log object. | ||
@@ -188,12 +189,2 @@ */ | ||
/** | ||
* Returns the compiled log object converted into JSON. This method utilizes `options.replacer` for the replacer function. It also uses | ||
* [fast-safe-stringify](https://www.npmjs.com/package/fast-safe-stringify) to prevent circular reference issues. | ||
* @param {boolean} format Whether to format the log object with line breaks and indentation. | ||
* @returns {string} The JSON string. | ||
*/ | ||
toJSON(format) { | ||
var _a; | ||
return (0, fast_safe_stringify_1.default)(this.value, (_a = this.__opts.replacer) !== null && _a !== void 0 ? _a : undefined, format ? 2 : 0); | ||
} | ||
/** | ||
* Converts the log to a string using a specific/custom formatter. | ||
@@ -208,3 +199,3 @@ * @returns {string} The formatted log as a string. | ||
* @protected | ||
* @param {Formatter} [formatter] The formatter to use or custom formatter function. | ||
* @param {FormatPlugin} [formatter] The formatter to use or custom formatter function. | ||
* @returns {string} The formatted log as a string. | ||
@@ -214,21 +205,6 @@ */ | ||
const { __opts } = this; | ||
if (typeof formatter === 'function') { | ||
return formatter.call(this, this, __opts, fast_safe_stringify_1.default); | ||
if (!formatter || typeof formatter !== 'function') { | ||
formatter = (0, json_js_1.default)(); | ||
} | ||
switch (formatter) { | ||
// Clean Formatter | ||
case 'clean': | ||
return [ | ||
`${this.level.toUpperCase()}\t${this.msg}`, | ||
this.tags.length ? `\t├→ ${this.tags.join(', ')}` : null, | ||
Object.keys(this.meta).length ? `\t└→ ${(0, fast_safe_stringify_1.default)(this.meta, undefined, 4).replace(/\n/g, '\n\t┊ ')}` : null | ||
].filter(v => Boolean(v)).join('\n'); | ||
// Minimal Formatter | ||
case 'minimal': | ||
return `${this.level.toUpperCase()}\t${this.msg}`; | ||
// JSON Formatter (default) | ||
case 'json': | ||
default: | ||
return this.toJSON(__opts.dev); | ||
} | ||
return formatter.call(this, this, __opts, fast_safe_stringify_1.default); | ||
} | ||
@@ -235,0 +211,0 @@ /** |
import LambdaLog from './LambdaLog.js'; | ||
import LogMessage from './LogMessage.js'; | ||
import * as formatters from './formatters/index.js'; | ||
import * as Types from './typings.js'; | ||
export default new LambdaLog(); | ||
export { LambdaLog, LogMessage, Types }; | ||
export { LambdaLog, LogMessage, formatters, Types }; |
import { EventEmitter } from 'events'; | ||
import LogMessage from './LogMessage.js'; | ||
import * as logFormatters from './formatters/index.js'; | ||
import { toBool } from './utils.js'; | ||
@@ -37,4 +38,6 @@ const levels = [{ | ||
messageKey: 'msg', | ||
tagsKey: '__tags' | ||
tagsKey: '__tags', | ||
onFormat: logFormatters.json() | ||
}; | ||
export const formatters = logFormatters; | ||
export default class LambdaLog extends EventEmitter { | ||
@@ -53,3 +56,2 @@ /** | ||
* @readonly | ||
* @type {LambdaLog} | ||
*/ | ||
@@ -59,3 +61,2 @@ this.LambdaLog = LambdaLog; | ||
* Access to the uninstantiated LogMessage class. | ||
* @type {LogMessage} | ||
*/ | ||
@@ -89,3 +90,2 @@ this.LogMessage = LogMessage; | ||
* Returns the console object to use for logging. | ||
* @readonly | ||
* @private | ||
@@ -105,7 +105,7 @@ * @returns {ConsoleObject} The configured console object or `console` if none is provided. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {object} [meta={}] Optional meta data to attach to the log. | ||
* @param {object|string|number} [meta={}] Optional meta data to attach to the log. | ||
* @param {string[]} [tags=[]] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
log(level, msg, meta = {}, tags = []) { | ||
_log(level, msg, meta, tags) { | ||
const lvl = this.getLevel(level); | ||
@@ -115,5 +115,2 @@ if (!lvl) { | ||
} | ||
// Check if we can log this level | ||
if (lvl.idx > this.maxLevelIdx) | ||
return false; | ||
// Generate the log message instance | ||
@@ -126,13 +123,16 @@ const message = new this.LogMessage({ | ||
}, this.options); | ||
const consoleObj = this.console; | ||
// Log the message to the console | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
consoleObj[lvl.method](message.toString()); | ||
/** | ||
* The log event is emitted (using EventEmitter) for every log generated. This allows for custom integrations, such as logging to a thrid-party service. | ||
* This event is emitted with the [LogMessage](#logmessage) instance for the log. You may control events using all the methods of EventEmitter. | ||
* @event LambdaLog#log | ||
* @type {LogMessage} | ||
*/ | ||
this.emit('log', message); | ||
// Check if we can log this level | ||
if (lvl.idx <= this.maxLevelIdx) { | ||
const consoleObj = this.console; | ||
// Log the message to the console | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
consoleObj[lvl.method](message.toString()); | ||
/** | ||
* The log event is emitted (using EventEmitter) for every log generated. This allows for custom integrations, such as logging to a thrid-party service. | ||
* This event is emitted with the [LogMessage](#logmessage) instance for the log. You may control events using all the methods of EventEmitter. | ||
* @event LambdaLog#log | ||
* @type {LogMessage} | ||
*/ | ||
this.emit('log', message); | ||
} | ||
return message; | ||
@@ -144,8 +144,8 @@ } | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
trace(msg, meta, tags) { | ||
return this.log('trace', msg, meta, tags); | ||
return this._log('trace', msg, meta, tags); | ||
} | ||
@@ -156,8 +156,8 @@ /** | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
debug(msg, meta, tags) { | ||
return this.log('debug', msg, meta, tags); | ||
return this._log('debug', msg, meta, tags); | ||
} | ||
@@ -168,19 +168,30 @@ /** | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
info(msg, meta, tags) { | ||
return this.log('info', msg, meta, tags); | ||
return this._log('info', msg, meta, tags); | ||
} | ||
/** | ||
* Alias for `info`. | ||
* @template T The type of the message to log. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
log(msg, meta, tags) { | ||
return this._log('info', msg, meta, tags); | ||
} | ||
/** | ||
* Logs a message at the `warn` log level. | ||
* @template T The type of the message to log. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
warn(msg, meta, tags) { | ||
return this.log('warn', msg, meta, tags); | ||
return this._log('warn', msg, meta, tags); | ||
} | ||
@@ -191,8 +202,8 @@ /** | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
error(msg, meta, tags) { | ||
return this.log('error', msg, meta, tags); | ||
return this._log('error', msg, meta, tags); | ||
} | ||
@@ -203,8 +214,8 @@ /** | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
fatal(msg, meta, tags) { | ||
return this.log('fatal', msg, meta, tags); | ||
return this._log('fatal', msg, meta, tags); | ||
} | ||
@@ -214,3 +225,2 @@ /** | ||
* wrap them in an if statement. The log level will be `error`. | ||
* @since 1.4.0 | ||
* @template T The type of the message to log. | ||
@@ -226,11 +236,10 @@ * @param {*} test Value to test for a falsy value. | ||
return false; | ||
return this.log('error', msg, meta, tags); | ||
return this._log('error', msg, meta, tags); | ||
} | ||
/** | ||
* Generates a log message with the result or error provided by a promise. Useful for debugging and testing. | ||
* @since 2.3.0 | ||
* @param {Promise<*>} promise Promise to log the results of. | ||
* @param {object} [meta={}] Optional meta data to attach to the log. | ||
* @param {string[]} [tags=[]] Additional tags to append to this log. | ||
* @returns {Promise<LogMessage | false>} A Promise that resolves with the log message. | ||
* @returns {Promise<LogMessage>} A Promise that resolves with the log message. | ||
*/ | ||
@@ -241,6 +250,6 @@ async result(promise, meta, tags) { | ||
.then(value => { | ||
resolve(this.log('info', value, meta, tags)); | ||
resolve(this._log('info', value, meta, tags)); | ||
}) | ||
.catch(err => { | ||
resolve(this.log('error', err, meta, tags)); | ||
resolve(this._log('error', err, meta, tags)); | ||
}); | ||
@@ -269,3 +278,2 @@ }); | ||
* Returns the index of the configured maximum log level. | ||
* @readonly | ||
* @private | ||
@@ -280,1 +288,3 @@ * @returns {number} The index of the configured maximum log level. | ||
} | ||
LambdaLog.defaultOptions = defaultOptions; | ||
LambdaLog.formatters = formatters; |
import stringify from 'fast-safe-stringify'; | ||
import { isError, stubError } from './utils.js'; | ||
import jsonFormatter from './formatters/json.js'; | ||
/** | ||
@@ -144,3 +145,3 @@ * The LogMessage class is a private/internal class that is used for generating log messages. All log methods return an instance of LogMessage allowing for a chainable api. | ||
/** | ||
* The full log object. This is the object used in logMessage.toJSON() and when the log is written to the console. | ||
* The log represented as an object that is useful for stringifying or pulling data from. | ||
* @returns {GenericRecord} The compiled log object. | ||
@@ -182,12 +183,2 @@ */ | ||
/** | ||
* Returns the compiled log object converted into JSON. This method utilizes `options.replacer` for the replacer function. It also uses | ||
* [fast-safe-stringify](https://www.npmjs.com/package/fast-safe-stringify) to prevent circular reference issues. | ||
* @param {boolean} format Whether to format the log object with line breaks and indentation. | ||
* @returns {string} The JSON string. | ||
*/ | ||
toJSON(format) { | ||
var _a; | ||
return stringify(this.value, (_a = this.__opts.replacer) !== null && _a !== void 0 ? _a : undefined, format ? 2 : 0); | ||
} | ||
/** | ||
* Converts the log to a string using a specific/custom formatter. | ||
@@ -202,3 +193,3 @@ * @returns {string} The formatted log as a string. | ||
* @protected | ||
* @param {Formatter} [formatter] The formatter to use or custom formatter function. | ||
* @param {FormatPlugin} [formatter] The formatter to use or custom formatter function. | ||
* @returns {string} The formatted log as a string. | ||
@@ -208,21 +199,6 @@ */ | ||
const { __opts } = this; | ||
if (typeof formatter === 'function') { | ||
return formatter.call(this, this, __opts, stringify); | ||
if (!formatter || typeof formatter !== 'function') { | ||
formatter = jsonFormatter(); | ||
} | ||
switch (formatter) { | ||
// Clean Formatter | ||
case 'clean': | ||
return [ | ||
`${this.level.toUpperCase()}\t${this.msg}`, | ||
this.tags.length ? `\t├→ ${this.tags.join(', ')}` : null, | ||
Object.keys(this.meta).length ? `\t└→ ${stringify(this.meta, undefined, 4).replace(/\n/g, '\n\t┊ ')}` : null | ||
].filter(v => Boolean(v)).join('\n'); | ||
// Minimal Formatter | ||
case 'minimal': | ||
return `${this.level.toUpperCase()}\t${this.msg}`; | ||
// JSON Formatter (default) | ||
case 'json': | ||
default: | ||
return this.toJSON(__opts.dev); | ||
} | ||
return formatter.call(this, this, __opts, stringify); | ||
} | ||
@@ -229,0 +205,0 @@ /** |
@@ -23,4 +23,12 @@ /// <reference types="node" /> | ||
} | ||
declare module "formatters/json" { | ||
import { FormatPlugin } from "typings"; | ||
/** | ||
* JSON formatter for log messages. | ||
* @returns {FormatPlugin} The JSON formatter function. | ||
*/ | ||
export default function jsonFormatter(): FormatPlugin; | ||
} | ||
declare module "LogMessage" { | ||
import { LambdaLogOptions, Message, LogObject, Tag, GenericRecord, Formatter, StubbedError, Empty } from "typings"; | ||
import { LambdaLogOptions, Message, LogObject, Tag, GenericRecord, StubbedError, Empty, FormatPlugin } from "typings"; | ||
export interface ILogMessage { | ||
@@ -40,3 +48,3 @@ readonly __opts: LambdaLogOptions; | ||
set meta(obj: GenericRecord); | ||
get tags(): Tag[]; | ||
get tags(): string[]; | ||
set tags(tags: Tag[]); | ||
@@ -46,3 +54,2 @@ get value(): GenericRecord; | ||
get throw(): void; | ||
toJSON(format: boolean): string; | ||
} | ||
@@ -54,3 +61,3 @@ /** | ||
*/ | ||
export default class LogMessage implements ILogMessage { | ||
export default class LogMessage<MT extends Message = Message> implements ILogMessage { | ||
readonly __opts: LambdaLogOptions; | ||
@@ -68,3 +75,3 @@ __level: string; | ||
*/ | ||
constructor(log: LogObject, opts: LambdaLogOptions); | ||
constructor(log: LogObject<MT>, opts: LambdaLogOptions); | ||
/** | ||
@@ -94,3 +101,3 @@ * String log level of the message. | ||
*/ | ||
set message(msg: string); | ||
set message(msg: Message); | ||
/** | ||
@@ -110,3 +117,3 @@ * The fully compiled metadata object for the log. Includes global and dynamic metadata. | ||
*/ | ||
get tags(): Tag[]; | ||
get tags(): string[]; | ||
/** | ||
@@ -118,3 +125,3 @@ * Appends additional tags to this log message. | ||
/** | ||
* The full log object. This is the object used in logMessage.toJSON() and when the log is written to the console. | ||
* The log represented as an object that is useful for stringifying or pulling data from. | ||
* @returns {GenericRecord} The compiled log object. | ||
@@ -135,9 +142,2 @@ */ | ||
/** | ||
* Returns the compiled log object converted into JSON. This method utilizes `options.replacer` for the replacer function. It also uses | ||
* [fast-safe-stringify](https://www.npmjs.com/package/fast-safe-stringify) to prevent circular reference issues. | ||
* @param {boolean} format Whether to format the log object with line breaks and indentation. | ||
* @returns {string} The JSON string. | ||
*/ | ||
toJSON(format?: boolean): string; | ||
/** | ||
* Converts the log to a string using a specific/custom formatter. | ||
@@ -150,6 +150,6 @@ * @returns {string} The formatted log as a string. | ||
* @protected | ||
* @param {Formatter} [formatter] The formatter to use or custom formatter function. | ||
* @param {FormatPlugin} [formatter] The formatter to use or custom formatter function. | ||
* @returns {string} The formatted log as a string. | ||
*/ | ||
protected formatMessage(formatter?: Formatter): string; | ||
protected formatMessage(formatter?: FormatPlugin): string; | ||
/** | ||
@@ -179,3 +179,4 @@ * Takes a log message, parses it, and sets any corresponding properties on the log. | ||
export type GenericRecord<K extends string | number = string | number, V = unknown> = Record<K, V>; | ||
export type Message<T = string | number | Error> = T; | ||
export type Message = string | number | Error; | ||
export type Metadata = GenericRecord | string | number | null | undefined; | ||
export type Empty = false | null | undefined; | ||
@@ -189,10 +190,20 @@ type TagFnObject = { | ||
type StringifyType = typeof stringify; | ||
export type Formatter = 'json' | 'clean' | 'minimal' | ((ctx: LogMessage, options: LambdaLogOptions, stringify: StringifyType) => string); | ||
export type LogObject = { | ||
export type LogObject<T extends Message = Message> = { | ||
level: string; | ||
msg: Message; | ||
msg: T; | ||
meta?: Metadata; | ||
tags?: Tag[]; | ||
}; | ||
export type LogLevels = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'; | ||
export type ParsePlugin = (msg: Message, options: LambdaLogOptions) => { | ||
msg: string; | ||
meta?: GenericRecord; | ||
error?: Error; | ||
tags?: Tag[]; | ||
} | Empty; | ||
export type CompilePlugin = (level?: string, msg?: Message, meta?: GenericRecord, tags?: Tag[], options?: LambdaLogOptions) => GenericRecord; | ||
export type FormatPlugin = { | ||
(ctx: LogMessage, options: LambdaLogOptions, stringify: StringifyType): string; | ||
_cfg?: Record<string, unknown>; | ||
}; | ||
export type LogLevels = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'; | ||
export type LambdaLogOptions = { | ||
@@ -211,10 +222,5 @@ [key: string]: any; | ||
tagsKey?: string | false; | ||
onParse?: (msg: Message, options: LambdaLogOptions) => { | ||
msg: string; | ||
meta?: GenericRecord; | ||
error?: Error; | ||
tags?: Tag[]; | ||
} | Empty; | ||
onCompile?: (level?: string, msg?: Message, meta?: GenericRecord, tags?: Tag[], options?: LambdaLogOptions) => GenericRecord; | ||
onFormat?: Formatter; | ||
onParse?: ParsePlugin; | ||
onCompile?: CompilePlugin; | ||
onFormat?: FormatPlugin; | ||
}; | ||
@@ -235,6 +241,44 @@ export interface ConsoleObject extends Console { | ||
} | ||
declare module "formatters/full" { | ||
import { FormatPlugin } from "typings"; | ||
import { InspectOptions } from 'util'; | ||
type FullFormatterCfg = { | ||
includeTimestamp?: boolean; | ||
formatTimestamp?: (timestamp: Date) => string; | ||
includeTags?: boolean; | ||
includeMeta?: boolean; | ||
separator?: string; | ||
inspectOptions?: InspectOptions; | ||
}; | ||
/** | ||
* Full formatter for log messages. | ||
* @param {object} cfg Configuration object for the formatter. | ||
* @returns {FormatPlugin} The full formatter function. | ||
*/ | ||
export default function fullFormatter(cfg?: FullFormatterCfg): FormatPlugin; | ||
} | ||
declare module "formatters/minimal" { | ||
import { FormatPlugin } from "typings"; | ||
type MinimalFormatterCfg = { | ||
includeTimestamp?: boolean; | ||
formatTimestamp?: (timestamp: Date) => string; | ||
separator?: string; | ||
}; | ||
/** | ||
* Minimal formatter for log messages. | ||
* @param {object} cfg Configuration object for the formatter. | ||
* @returns {FormatPlugin} The minimal formatter function. | ||
*/ | ||
export default function minimalFormatter(cfg?: MinimalFormatterCfg): FormatPlugin; | ||
} | ||
declare module "formatters/index" { | ||
export { default as json } from "formatters/json"; | ||
export { default as full } from "formatters/full"; | ||
export { default as minimal } from "formatters/minimal"; | ||
} | ||
declare module "LambdaLog" { | ||
import { EventEmitter } from 'events'; | ||
import { LambdaLogOptions, Message, GenericRecord, LogLevels, Tag } from "typings"; | ||
import { LambdaLogOptions, Message, Metadata, LogLevels, Tag } from "typings"; | ||
import LogMessage from "LogMessage"; | ||
import * as logFormatters from "formatters/index"; | ||
/** | ||
@@ -244,7 +288,9 @@ * Default options for the LambdaLog class. Stored globally so that it can be modified by the user. | ||
export const defaultOptions: LambdaLogOptions; | ||
export const formatters: typeof logFormatters; | ||
export default class LambdaLog extends EventEmitter { | ||
static defaultOptions: LambdaLogOptions; | ||
static formatters: typeof logFormatters; | ||
/** | ||
* Access to the uninstantiated LambdaLog class. | ||
* @readonly | ||
* @type {LambdaLog} | ||
*/ | ||
@@ -254,3 +300,2 @@ readonly LambdaLog: typeof LambdaLog; | ||
* Access to the uninstantiated LogMessage class. | ||
* @type {LogMessage} | ||
*/ | ||
@@ -260,3 +305,2 @@ LogMessage: typeof LogMessage; | ||
* The options object for this instance of LambdaLog. | ||
* @type {LambdaLogOptions} | ||
*/ | ||
@@ -274,3 +318,2 @@ options: LambdaLogOptions; | ||
* Returns the console object to use for logging. | ||
* @readonly | ||
* @private | ||
@@ -295,7 +338,7 @@ * @returns {ConsoleObject} The configured console object or `console` if none is provided. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {object} [meta={}] Optional meta data to attach to the log. | ||
* @param {object|string|number} [meta={}] Optional meta data to attach to the log. | ||
* @param {string[]} [tags=[]] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
log<T extends Message>(level: LogLevels, msg: T, meta?: GenericRecord, tags?: Tag[]): false | LogMessage; | ||
_log<T extends Message = Message>(level: LogLevels, msg: T, meta?: Metadata, tags?: Tag[]): LogMessage; | ||
/** | ||
@@ -305,7 +348,7 @@ * Logs a message at the `trace` log level. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
trace<T extends Message>(msg: T, meta?: GenericRecord, tags?: Tag[]): false | LogMessage; | ||
trace<T extends Message = Message>(msg: T, meta?: Metadata, tags?: Tag[]): LogMessage; | ||
/** | ||
@@ -315,7 +358,7 @@ * Logs a message at the `debug` log level. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
debug<T extends Message>(msg: T, meta?: GenericRecord, tags?: Tag[]): false | LogMessage; | ||
debug<T extends Message = Message>(msg: T, meta?: Metadata, tags?: Tag[]): LogMessage; | ||
/** | ||
@@ -325,16 +368,25 @@ * Logs a message at the `info` log level. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
info<T extends Message>(msg: T, meta?: GenericRecord, tags?: Tag[]): false | LogMessage; | ||
info<T extends Message = Message>(msg: T, meta?: Metadata, tags?: Tag[]): LogMessage; | ||
/** | ||
* Alias for `info`. | ||
* @template T The type of the message to log. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
log<T extends Message = Message>(msg: T, meta?: Metadata, tags?: Tag[]): LogMessage; | ||
/** | ||
* Logs a message at the `warn` log level. | ||
* @template T The type of the message to log. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
warn<T extends Message>(msg: T, meta?: GenericRecord, tags?: Tag[]): false | LogMessage; | ||
warn<T extends Message = Message>(msg: T, meta?: Metadata, tags?: Tag[]): LogMessage; | ||
/** | ||
@@ -344,7 +396,7 @@ * Logs a message at the `error` log level. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
error<T extends Message>(msg: T, meta?: GenericRecord, tags?: Tag[]): false | LogMessage; | ||
error<T extends Message = Message>(msg: T, meta?: Metadata, tags?: Tag[]): LogMessage; | ||
/** | ||
@@ -354,11 +406,10 @@ * Logs a message at the `error` log level. | ||
* @param {T} msg Message to log. Can be any type, but string or `Error` is reccommended. | ||
* @param {GenericRecord} [meta] Optional meta data to attach to the log. | ||
* @param {Metadata} [meta] Optional meta data to attach to the log. | ||
* @param {Tag[]} [tags] Additional tags to append to this log. | ||
* @returns {LogMessage|false} Returns instance of LogMessage or `false` if the level of the log exceeds to the maximum set log level. | ||
* @returns {LogMessage} Returns instance of LogMessage. | ||
*/ | ||
fatal<T extends Message>(msg: T, meta?: GenericRecord, tags?: Tag[]): false | LogMessage; | ||
fatal<T extends Message = Message>(msg: T, meta?: Metadata, tags?: Tag[]): LogMessage; | ||
/** | ||
* Generates a log message if `test` is a falsy value. If `test` is truthy, the log message is skipped and returns `false`. Allows creating log messages without the need to | ||
* wrap them in an if statement. The log level will be `error`. | ||
* @since 1.4.0 | ||
* @template T The type of the message to log. | ||
@@ -371,12 +422,11 @@ * @param {*} test Value to test for a falsy value. | ||
*/ | ||
assert<T extends Message>(test: unknown, msg: T, meta?: GenericRecord, tags?: Tag[]): false | LogMessage; | ||
assert<T extends Message = Message>(test: unknown, msg: T, meta?: Metadata, tags?: Tag[]): LogMessage | false; | ||
/** | ||
* Generates a log message with the result or error provided by a promise. Useful for debugging and testing. | ||
* @since 2.3.0 | ||
* @param {Promise<*>} promise Promise to log the results of. | ||
* @param {object} [meta={}] Optional meta data to attach to the log. | ||
* @param {string[]} [tags=[]] Additional tags to append to this log. | ||
* @returns {Promise<LogMessage | false>} A Promise that resolves with the log message. | ||
* @returns {Promise<LogMessage>} A Promise that resolves with the log message. | ||
*/ | ||
result(promise: Promise<unknown>, meta?: GenericRecord, tags?: Tag[]): Promise<false | LogMessage>; | ||
result(promise: Promise<unknown>, meta?: Metadata, tags?: Tag[]): Promise<LogMessage>; | ||
/** | ||
@@ -391,3 +441,2 @@ * Validates and gets the configuration for the provided log level. | ||
* Returns the index of the configured maximum log level. | ||
* @readonly | ||
* @private | ||
@@ -402,6 +451,7 @@ * @returns {number} The index of the configured maximum log level. | ||
import LogMessage from "LogMessage"; | ||
import * as formatters from "formatters/index"; | ||
import * as Types from "typings"; | ||
const _default: LambdaLog; | ||
export default _default; | ||
export { LambdaLog, LogMessage, Types }; | ||
export { LambdaLog, LogMessage, formatters, Types }; | ||
} | ||
@@ -408,0 +458,0 @@ declare module "index" { |
{ | ||
"name": "lambda-log", | ||
"version": "4.0.0-beta.4", | ||
"version": "4.0.0-beta.5", | ||
"description": "Lightweight logging library for any Node 12+ applications", | ||
@@ -74,5 +74,5 @@ "main": "./index.js", | ||
"@semantic-release/git": "^10.0.1", | ||
"@types/jest": "^27.0.2", | ||
"@typescript-eslint/eslint-plugin": "^5.4.0", | ||
"@typescript-eslint/parser": "^5.4.0", | ||
"@types/jest": "^27.0.3", | ||
"@typescript-eslint/eslint-plugin": "^5.6.0", | ||
"@typescript-eslint/parser": "^5.6.0", | ||
"cross-env": "^7.0.3", | ||
@@ -82,3 +82,3 @@ "eslint": "^7.23.0", | ||
"eslint-config-xo-typescript": "^0.45.2", | ||
"eslint-plugin-jsdoc": "^37.0.3", | ||
"eslint-plugin-jsdoc": "^37.2.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
@@ -88,14 +88,14 @@ "expect-more-jest": "^5.4.0", | ||
"is-ci": "^3.0.1", | ||
"jest": "^27.3.1", | ||
"jest": "^27.4.3", | ||
"jest-ts-webcompat-resolver": "^1.0.0", | ||
"rimraf": "^3.0.2", | ||
"semantic-release": "^18.0.0", | ||
"ts-jest": "^27.0.7", | ||
"semantic-release": "^18.0.1", | ||
"ts-jest": "^27.1.1", | ||
"ts-node": "^10.4.0", | ||
"typescript": "^4.5.2" | ||
"typescript": "^4.5.3" | ||
}, | ||
"dependencies": { | ||
"@types/node": "^16.11.9", | ||
"@types/node": "^16.11.12", | ||
"fast-safe-stringify": "^2.1.1" | ||
} | ||
} |
87305
27
1830
Updated@types/node@^16.11.12