@lbu/insight
Advanced tools
Comparing version 0.0.43 to 0.0.44
@@ -41,7 +41,2 @@ /** | ||
/** | ||
* Max-depth printed | ||
*/ | ||
depth?: number; | ||
/** | ||
* The stream to write the logs to | ||
@@ -89,27 +84,4 @@ */ | ||
/** | ||
* Standard log instance. | ||
* Comes with a depth of 4, prevents printing deeply nested objects | ||
* Standard log instance | ||
*/ | ||
export const log: Logger; | ||
/** | ||
* The LogParserContext enables you too analyze logs produced by this Logger | ||
*/ | ||
export interface LogParserContext { | ||
jsonProcessor?: (data: object) => void; | ||
textProcessor?: (data: string) => void; | ||
stream: NodeJS.ReadableStream; | ||
} | ||
/** | ||
* Create a new parser context | ||
*/ | ||
export function newLogParserContext( | ||
stream: NodeJS.ReadableStream, | ||
): LogParserContext; | ||
/** | ||
* Run the parser, splits the in stream onn lines and call either the jsonProcessor or | ||
* textProcessor with the value. The original value is written to the returned stream | ||
*/ | ||
export function executeLogParser(lpc: LogParserContext): NodeJS.ReadableStream; |
@@ -5,3 +5,2 @@ import { newLogger } from "./src/logger.js"; | ||
export { newLogger, bindLoggerContext } from "./src/logger.js"; | ||
export { newLogParserContext, executeLogParser } from "./src/parser.js"; | ||
@@ -11,4 +10,2 @@ /** | ||
*/ | ||
export const log = newLogger({ | ||
depth: 4, | ||
}); | ||
export const log = newLogger({}); |
{ | ||
"name": "@lbu/insight", | ||
"version": "0.0.43", | ||
"version": "0.0.44", | ||
"description": "Simple logger in NDJSON format", | ||
@@ -15,6 +15,2 @@ "main": "./index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"pump": "3.0.0", | ||
"split2": "3.1.1" | ||
}, | ||
"author": { | ||
@@ -35,3 +31,3 @@ "name": "Dirk de Visser", | ||
}, | ||
"gitHead": "f3b1d86fb7237f6bdc30f3499e0a383a0e0724da" | ||
"gitHead": "7b7f5bb4365346c8e3098c0eb8a8b43ad306635f" | ||
} |
@@ -52,3 +52,2 @@ # @lbu/insight | ||
- Various utilities to get insight in the running process | ||
- Parser to process production logs in an external process | ||
@@ -55,0 +54,0 @@ **@lbu/stdlib**: |
@@ -11,3 +11,2 @@ import { writeNDJSON, writePretty } from "./writer.js"; | ||
const stream = options?.stream ?? process.stdout; | ||
const depth = options?.depth ?? 3; | ||
@@ -21,4 +20,4 @@ const logFn = isProduction | ||
isProduction: () => isProduction, | ||
info: logFn.bind(undefined, stream, depth, "info"), | ||
error: logFn.bind(undefined, stream, depth, "error"), | ||
info: logFn.bind(undefined, stream, "info"), | ||
error: logFn.bind(undefined, stream, "error"), | ||
}; | ||
@@ -28,4 +27,4 @@ } else { | ||
isProduction: () => isProduction, | ||
info: logFn.bind(undefined, stream, depth, "info", options.ctx), | ||
error: logFn.bind(undefined, stream, depth, "error", options.ctx), | ||
info: logFn.bind(undefined, stream, "info", options.ctx), | ||
error: logFn.bind(undefined, stream, "error", options.ctx), | ||
}; | ||
@@ -56,3 +55,3 @@ } | ||
function wrapWriter(fn) { | ||
return function log(stream, depth, level, context, message) { | ||
return function log(stream, level, context, message) { | ||
const timestamp = new Date(); | ||
@@ -63,4 +62,4 @@ if (!message) { | ||
} | ||
fn(stream, depth, level, timestamp, context, message); | ||
fn(stream, level, timestamp, context, message); | ||
}; | ||
} |
@@ -5,3 +5,2 @@ import { inspect } from "util"; | ||
* @param stream | ||
* @param depth | ||
* @param level | ||
@@ -12,3 +11,3 @@ * @param timestamp | ||
*/ | ||
export function writeNDJSON(stream, depth, level, timestamp, context, message) { | ||
export function writeNDJSON(stream, level, timestamp, context, message) { | ||
stream.write( | ||
@@ -19,3 +18,3 @@ JSON.stringify({ | ||
timestamp: timestamp.toISOString(), | ||
message: formatMessage(depth, message), | ||
message: message, | ||
}), | ||
@@ -28,3 +27,2 @@ ); | ||
* @param stream | ||
* @param depth | ||
* @param level | ||
@@ -35,3 +33,3 @@ * @param timestamp | ||
*/ | ||
export function writePretty(stream, depth, level, timestamp, context, message) { | ||
export function writePretty(stream, level, timestamp, context, message) { | ||
stream.write(formatDate(timestamp)); | ||
@@ -44,5 +42,3 @@ stream.write(" "); | ||
if (Array.isArray(message)) { | ||
stream.write( | ||
message.map((it) => formatMessagePretty(depth - 2, it)).join(", "), | ||
); | ||
stream.write(message.map((it) => formatMessagePretty(it)).join(", ")); | ||
} else { | ||
@@ -56,6 +52,6 @@ let keyCount = 0; | ||
if (Object.keys(context).length > keyCount) { | ||
stream.write(formatMessagePretty(depth - 1, context)); | ||
stream.write(formatMessagePretty(context)); | ||
stream.write(" "); | ||
} | ||
stream.write(formatMessagePretty(depth - 1, message)); | ||
stream.write(formatMessagePretty(message)); | ||
} | ||
@@ -68,7 +64,6 @@ } | ||
/** | ||
* @param {number} depth | ||
* @param {*} value | ||
* @returns {string} | ||
*/ | ||
function formatMessagePretty(depth, value) { | ||
function formatMessagePretty(value) { | ||
if ( | ||
@@ -83,3 +78,3 @@ typeof value === "boolean" || | ||
colors: true, | ||
depth, | ||
depth: null, | ||
}); | ||
@@ -90,61 +85,2 @@ } | ||
/** | ||
* @param {number} availableDepth | ||
* @param {*} message | ||
* @returns {*} | ||
*/ | ||
function formatMessage(availableDepth, message) { | ||
if (message === null) { | ||
return null; | ||
} | ||
if (message === undefined) { | ||
return undefined; | ||
} | ||
const type = typeof message; | ||
if (type === "string" || type === "boolean" || type === "number") { | ||
return message; | ||
} | ||
if (type === "bigint" || type === "symbol") { | ||
return message.toString(); | ||
} else if (type === "function") { | ||
return formatMessage(availableDepth, { | ||
name: message.name || "fn", | ||
length: message.length || 0, | ||
}); | ||
} | ||
if (availableDepth === 0) { | ||
if (Array.isArray(message)) { | ||
return `[...]`; | ||
} else { | ||
return `{...}`; | ||
} | ||
} | ||
if (Array.isArray(message)) { | ||
let result = Array(message.length); | ||
for (let i = 0; i < message.length; ++i) { | ||
result[i] = formatMessage(availableDepth - 1, message[i]); | ||
} | ||
return result; | ||
} | ||
// Handle classes & objects | ||
const keys = | ||
typeof message === "object" && | ||
message.constructor === Object && | ||
Object.prototype.toString.call(message) === "[object Object]" | ||
? Object.keys(message) | ||
: Object.getOwnPropertyNames(message); | ||
const result = {}; | ||
for (const key of keys) { | ||
result[key] = formatMessage(availableDepth - 1, message[key]); | ||
} | ||
return result; | ||
} | ||
/** | ||
* @param {Date} date | ||
@@ -151,0 +87,0 @@ * @returns {string} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
12224
8
275
113
- Removedpump@3.0.0
- Removedsplit2@3.1.1
- Removedend-of-stream@1.4.4(transitive)
- Removedinherits@2.0.4(transitive)
- Removedonce@1.4.0(transitive)
- Removedpump@3.0.0(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsplit2@3.1.1(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwrappy@1.0.2(transitive)