@lbu/insight
Advanced tools
Comparing version 0.0.87 to 0.0.88
@@ -61,11 +61,2 @@ /** | ||
/** | ||
* Bind a context object to the logger functions and returns a new Logger | ||
* The context is always printed | ||
*/ | ||
export function bindLoggerContext<T extends LoggerContext>( | ||
logger: Logger, | ||
ctx: T, | ||
): Logger; | ||
/** | ||
* Format bytes, with up to 2 digits after the decimal point, in a more human readable way | ||
@@ -72,0 +63,0 @@ * Support up to a pebibyte |
import { newLogger } from "./src/logger/logger.js"; | ||
export { bytesToHumanReadable, printProcessMemoryUsage } from "./src/memory.js"; | ||
export { newLogger, bindLoggerContext } from "./src/logger/logger.js"; | ||
export { newLogger } from "./src/logger/logger.js"; | ||
@@ -6,0 +6,0 @@ export { |
{ | ||
"name": "@lbu/insight", | ||
"version": "0.0.87", | ||
"version": "0.0.88", | ||
"description": "Simple logger in NDJSON format", | ||
@@ -36,3 +36,3 @@ "main": "./index.js", | ||
}, | ||
"gitHead": "021df7496d95137e21351c5686674de431f7bb01" | ||
"gitHead": "f95775597389435d11e6ef713651a2d1d4816067" | ||
} |
@@ -8,2 +8,3 @@ import { writeNDJSON, writePretty } from "./writer.js"; | ||
export function newLogger(options) { | ||
const app = process.env.APP_NAME; | ||
const isProduction = | ||
@@ -17,13 +18,14 @@ options?.pretty === false || process.env.NODE_ENV === "production"; | ||
if (options?.ctx === undefined) { | ||
return { | ||
isProduction: () => isProduction, | ||
info: logFn.bind(undefined, stream, "info"), | ||
error: logFn.bind(undefined, stream, "error"), | ||
}; | ||
let context = options?.ctx ?? {}; | ||
if (isProduction) { | ||
if (app) { | ||
context.application = app; | ||
} | ||
context = JSON.stringify(context); | ||
} | ||
return { | ||
isProduction: () => isProduction, | ||
info: logFn.bind(undefined, stream, "info", options.ctx), | ||
error: logFn.bind(undefined, stream, "error", options.ctx), | ||
info: logFn.bind(undefined, stream, "info", context), | ||
error: logFn.bind(undefined, stream, "error", context), | ||
}; | ||
@@ -33,30 +35,9 @@ } | ||
/** | ||
* @param {Logger} logger | ||
* @param {LoggerContext} ctx | ||
* @returns {Logger} | ||
*/ | ||
export function bindLoggerContext(logger, ctx) { | ||
const isProd = logger.isProduction(); | ||
return { | ||
isProduction: () => isProd, | ||
info: logger.info.bind(undefined, ctx), | ||
error: logger.error.bind(undefined, ctx), | ||
}; | ||
} | ||
/** | ||
* Wrap provided writer function to be used in the Logger | ||
* | ||
* @param fn | ||
* @returns {log} | ||
*/ | ||
function wrapWriter(fn) { | ||
return function log(stream, level, context, message) { | ||
return (stream, level, context, message) => { | ||
const timestamp = new Date(); | ||
if (!message) { | ||
message = context; | ||
context = {}; | ||
} | ||
fn(stream, level, timestamp, context, message); | ||
fn(stream, level, timestamp, context, message ?? {}); | ||
}; | ||
} |
@@ -12,10 +12,6 @@ import { inspect } from "util"; | ||
stream.write( | ||
JSON.stringify({ | ||
level, | ||
...context, | ||
timestamp: timestamp.toISOString(), | ||
message: message, | ||
}), | ||
`{"level": "${level}", "timestamp": "${timestamp.toISOString()}", "context": ${context}, "message": ${JSON.stringify( | ||
message, | ||
)}}\n`, | ||
); | ||
stream.write("\n"); | ||
} | ||
@@ -31,5 +27,5 @@ | ||
export function writePretty(stream, level, timestamp, context, message) { | ||
stream.write(formatDate(timestamp)); | ||
stream.write(" "); | ||
stream.write(formatLevelAndType(level, context?.type)); | ||
stream.write( | ||
`${formatDate(timestamp)} ${formatLevelAndType(level, context?.type)}`, | ||
); | ||
@@ -48,4 +44,3 @@ if (message) { | ||
if (Object.keys(context).length > keyCount) { | ||
stream.write(formatMessagePretty(context)); | ||
stream.write(" "); | ||
stream.write(`${formatMessagePretty(context)} `); | ||
} | ||
@@ -52,0 +47,0 @@ stream.write(formatMessagePretty(message)); |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
17954
485
3