@openfn/logger
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -1,7 +0,9 @@ | ||
declare type LogLevel = 'debug' | 'info' | 'default' | 'none'; | ||
declare type LogEmitter = typeof console & { | ||
type SanitizePolicies = 'remove' | 'obfuscate' | 'summarize' | 'none'; | ||
type LogLevel = 'debug' | 'info' | 'default' | 'none'; | ||
type LogEmitter = typeof console & { | ||
success: typeof console.log; | ||
always: typeof console.log; | ||
}; | ||
declare type LogOptions = { | ||
type LogOptions = { | ||
level?: LogLevel; | ||
@@ -14,9 +16,10 @@ logger?: LogEmitter; | ||
sanitizePaths?: string[]; | ||
sanitiseState?: boolean; | ||
sanitizeState?: boolean; | ||
detectState?: boolean; | ||
json?: boolean; | ||
sanitize?: SanitizePolicies; | ||
}; | ||
declare type LogFns = 'debug' | 'info' | 'log' | 'warn' | 'error' | 'success' | 'always'; | ||
declare type JSONLog = { | ||
type LogFns = 'debug' | 'info' | 'log' | 'warn' | 'error' | 'success' | 'always'; | ||
type JSONLog = { | ||
message: Array<string | object | any>; | ||
@@ -27,3 +30,3 @@ level: LogFns; | ||
}; | ||
declare type StringLog = [LogFns | 'confirm' | 'print', ...any]; | ||
type StringLog = [LogFns | 'confirm' | 'print', ...any]; | ||
interface Logger extends Console { | ||
@@ -46,3 +49,3 @@ constructor(name: string): Logger; | ||
declare type ParsedLog = { | ||
type ParsedLog = { | ||
level: string; | ||
@@ -54,3 +57,3 @@ namespace?: string; | ||
}; | ||
declare type MockLogger<T> = Logger & { | ||
type MockLogger<T> = Logger & { | ||
_last: T; | ||
@@ -57,0 +60,0 @@ _history: T[]; |
@@ -17,3 +17,39 @@ // src/logger.ts | ||
import stringify from "fast-safe-stringify"; | ||
// src/util/duration.ts | ||
var duration_default = (timeInMs) => { | ||
if (timeInMs < 1e3) { | ||
return `${timeInMs}ms`; | ||
} | ||
const seconds = timeInMs / 1e3; | ||
if (seconds < 60) { | ||
return `${seconds}s`; | ||
} | ||
const minutes = seconds / 60; | ||
return `${Math.floor(minutes)}m ${seconds % 60}s`; | ||
}; | ||
// src/util/is-valid-log-level.ts | ||
var is_valid_log_level_default = (v) => /^(none|debug|info|default)$/i.test(v); | ||
// src/util/index.ts | ||
var isObject = (thing) => { | ||
if (Array.isArray(thing) || thing === null || thing instanceof Error) { | ||
return false; | ||
} | ||
if (typeof thing === "object") { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
var isArray = Array.isArray; | ||
// src/sanitize.ts | ||
var SECRET = "****"; | ||
var scrubbers = { | ||
remove, | ||
obfuscate, | ||
summarize, | ||
none: (item) => item | ||
}; | ||
var sanitize = (item, options = {}) => { | ||
@@ -24,3 +60,5 @@ const maybeStringify = (o) => options.stringify === false ? o : stringify(o, void 0, 2); | ||
} | ||
if (Array.isArray(item) || isNaN(item) && item && typeof item !== "string") { | ||
if (options.policy?.match(/^(remove|obfuscate|summarize)$/)) { | ||
return scrubbers[options.policy](item); | ||
} else if (Array.isArray(item) || isNaN(item) && item && typeof item !== "string") { | ||
const obj = item; | ||
@@ -42,16 +80,36 @@ if (obj && obj.configuration) { | ||
}; | ||
var sanitize_default = sanitize; | ||
// src/util/duration.ts | ||
var duration_default = (timeInMs) => { | ||
if (timeInMs < 1e3) { | ||
return `${timeInMs}ms`; | ||
function remove(logItem) { | ||
if (isObject(logItem) || isArray(logItem)) { | ||
return null; | ||
} | ||
const seconds = timeInMs / 1e3; | ||
if (seconds < 60) { | ||
return `${seconds}s`; | ||
return logItem; | ||
} | ||
function summarize(logItem) { | ||
if (isArray(logItem)) { | ||
if (logItem.length) { | ||
return `(array with ${logItem.length} items)`; | ||
} else { | ||
return "(empty array)"; | ||
} | ||
} | ||
const minutes = seconds / 60; | ||
return `${Math.floor(minutes)}m ${seconds % 60}s`; | ||
}; | ||
if (isObject(logItem)) { | ||
const keys = Object.keys(logItem); | ||
if (keys.length) { | ||
return `(object with keys ${keys.sort().join(", ")})`; | ||
} else { | ||
return "(empty object)"; | ||
} | ||
} | ||
return logItem; | ||
} | ||
function obfuscate(logItem) { | ||
if (isArray(logItem)) { | ||
return "[array]"; | ||
} | ||
if (isObject(logItem)) { | ||
return "[object]"; | ||
} | ||
return logItem; | ||
} | ||
var sanitize_default = sanitize; | ||
@@ -66,8 +124,12 @@ // src/options.ts | ||
level: "default", | ||
// TODO support an array of emitters here | ||
logger: defaultEmitter, | ||
// I guess? | ||
hideNamespace: false, | ||
hideIcons: false, | ||
// Not implemented | ||
wrap: false, | ||
showTimestamps: false, | ||
sanitiseState: false, | ||
sanitizeState: false, | ||
sanitize: "none", | ||
detectState: false, | ||
@@ -101,5 +163,6 @@ sanitizePaths: ["configuration"], | ||
[WARN]: 2, | ||
[ERROR]: 2, | ||
[SUCCESS]: 2, | ||
[NONE]: 9 | ||
[NONE]: 9, | ||
[ERROR]: 100 | ||
// errors ALWAYS log | ||
}; | ||
@@ -136,6 +199,15 @@ var styleLevel = (level) => { | ||
const logJSON = (level, ...args) => { | ||
const message = args.map( | ||
(o) => sanitize_default(o, { | ||
stringify: false, | ||
policy: options.sanitize | ||
}) | ||
); | ||
if (message.length === 1 && message[0] === null) { | ||
return; | ||
} | ||
const output = { | ||
level, | ||
name, | ||
message: args.map((o) => sanitize_default(o, { stringify: false })), | ||
message, | ||
time: Date.now() | ||
@@ -148,12 +220,22 @@ }; | ||
if (emitter.hasOwnProperty(level)) { | ||
const output = []; | ||
if (name && !opts.hideNamespace) { | ||
output.push(c.blue(`[${name}]`)); | ||
const cleanedArgs = args.map( | ||
(o) => sanitize_default(o, { | ||
stringify: true, | ||
sanitizePaths: [], | ||
policy: options.sanitize | ||
}) | ||
); | ||
if (cleanedArgs.length === 1 && cleanedArgs[0] === null) { | ||
return; | ||
} | ||
if (!opts.hideIcons) { | ||
output.push(styleLevel(level)); | ||
if (cleanedArgs.length) { | ||
const output = []; | ||
if (name && !opts.hideNamespace) { | ||
output.push(c.blue(`[${name}]`)); | ||
} | ||
if (!opts.hideIcons) { | ||
output.push(styleLevel(level)); | ||
} | ||
emitter[level](...output.concat(cleanedArgs)); | ||
} | ||
output.push(...args); | ||
const cleaned = output.map((o) => sanitize_default(o, options)); | ||
emitter[level](...cleaned); | ||
} | ||
@@ -181,5 +263,5 @@ }; | ||
delete timers[name2]; | ||
return duration_default(new Date().getTime() - startTime); | ||
return duration_default((/* @__PURE__ */ new Date()).getTime() - startTime); | ||
} | ||
timers[name2] = new Date().getTime(); | ||
timers[name2] = (/* @__PURE__ */ new Date()).getTime(); | ||
}; | ||
@@ -198,6 +280,10 @@ const wrap = (level) => (...args) => log(level, ...args); | ||
print, | ||
// possible convenience APIs | ||
force: () => { | ||
}, | ||
// force the next lines to log (even if silent) | ||
unforce: () => { | ||
}, | ||
// restore silent default | ||
// print a line break | ||
break: () => { | ||
@@ -208,3 +294,5 @@ console.log(); | ||
}, | ||
// set the indent level | ||
options: opts | ||
// debug and testing | ||
}; | ||
@@ -283,2 +371,3 @@ return logger; | ||
level, | ||
// Chop out the square brackets from the namespace, it's a style thing and annoying in tests | ||
namespace: namespace.substring(1, namespace.length - 1), | ||
@@ -308,5 +397,2 @@ icon, | ||
// src/util/is-valid-log-level.ts | ||
var is_valid_log_level_default = (v) => /^(none|debug|info|default)$/i.test(v); | ||
// src/index.ts | ||
@@ -313,0 +399,0 @@ var defaultLogger = logger_default(); |
{ | ||
"name": "@openfn/logger", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "Cross-package logging utility", | ||
@@ -32,4 +32,4 @@ "module": "dist/index.js", | ||
"tslib": "^2.4.0", | ||
"tsup": "^6.2.3", | ||
"typescript": "^4.8.3" | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.1.6" | ||
}, | ||
@@ -36,0 +36,0 @@ "files": [ |
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
15063
447