@openfn/logger
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -15,5 +15,7 @@ declare type LogLevel = 'debug' | 'info' | 'default' | 'none'; | ||
detectState?: boolean; | ||
json?: boolean; | ||
}; | ||
declare type LogFns = 'debug' | 'info' | 'log' | 'warn' | 'error' | 'success'; | ||
declare type StringLog = [LogFns | 'confirm' | 'print', ...any]; | ||
interface Logger extends Console { | ||
@@ -35,8 +37,7 @@ constructor(name: string): Logger; | ||
declare type LogMessage = [LogFns | 'confirm' | 'print', ...any[]]; | ||
declare type MockLogger = Logger & { | ||
_last: LogMessage; | ||
_history: any[]; | ||
declare type MockLogger<T> = Logger & { | ||
_last: T; | ||
_history: T[]; | ||
_reset: () => void; | ||
_parse: (m: LogMessage) => { | ||
_parse: (m: StringLog) => { | ||
level: string; | ||
@@ -49,3 +50,3 @@ namespace?: string; | ||
}; | ||
declare const mockLogger: (name?: string, opts?: LogOptions) => MockLogger; | ||
declare const mockLogger: <T = [LogFns | "confirm" | "print", ...any[]]>(name?: string, opts?: LogOptions) => MockLogger<T>; | ||
@@ -52,0 +53,0 @@ declare const _default$1: (v: string) => boolean; |
// src/logger.ts | ||
import c from "chalk"; | ||
import iconfirm from "@inquirer/confirm"; | ||
import stringify2 from "fast-safe-stringify"; | ||
@@ -14,5 +15,10 @@ // src/symbols.ts | ||
// src/sanitize.ts | ||
import stringify from "fast-safe-stringify"; | ||
var SECRET = "****"; | ||
var sanitize = (item, _options = {}) => { | ||
if (typeof item !== "string") { | ||
var sanitize = (item, options = {}) => { | ||
const maybeStringify = (o) => options.stringify === false ? o : stringify(o); | ||
if (item instanceof Error) { | ||
return item.toString(); | ||
} | ||
if (Array.isArray(item) || isNaN(item) && item && typeof item !== "string") { | ||
const obj = item; | ||
@@ -24,8 +30,9 @@ if (obj && obj.configuration) { | ||
} | ||
const cleaned = { | ||
const cleaned = maybeStringify({ | ||
...obj, | ||
configuration | ||
}; | ||
}); | ||
return cleaned; | ||
} | ||
return maybeStringify(obj); | ||
} | ||
@@ -63,3 +70,4 @@ return item; | ||
detectState: false, | ||
sanitizePaths: ["configuration"] | ||
sanitizePaths: ["configuration"], | ||
json: false | ||
}; | ||
@@ -111,19 +119,32 @@ var parseOptions = (opts = {}) => { | ||
const log = (level, ...args) => { | ||
if (opts.level === NONE) | ||
return; | ||
const output = []; | ||
if (name && !opts.hideNamespace) { | ||
output.push(c.blue(`[${name}]`)); | ||
} | ||
if (!opts.hideIcons) { | ||
output.push(styleLevel(level)); | ||
} | ||
output.push(...args); | ||
if (priority[level] >= minLevel) { | ||
if (emitter.hasOwnProperty(level)) { | ||
const cleaned = output.map((o) => sanitize_default(o, options)); | ||
emitter[level](...cleaned); | ||
if (options.json) { | ||
logJSON(level, ...args); | ||
} else { | ||
logString(level, ...args); | ||
} | ||
} | ||
}; | ||
const logJSON = (level, ...args) => { | ||
const output = { | ||
level, | ||
name, | ||
message: args.map((o) => sanitize_default(o, { stringify: false })) | ||
}; | ||
emitter[level](stringify2(output)); | ||
}; | ||
const logString = (level, ...args) => { | ||
if (emitter.hasOwnProperty(level)) { | ||
const output = []; | ||
if (name && !opts.hideNamespace) { | ||
output.push(c.blue(`[${name}]`)); | ||
} | ||
if (!opts.hideIcons) { | ||
output.push(styleLevel(level)); | ||
} | ||
output.push(...args); | ||
const cleaned = output.map((o) => sanitize_default(o, options)); | ||
emitter[level](...cleaned); | ||
} | ||
}; | ||
const print = (...args) => { | ||
@@ -183,3 +204,7 @@ if (opts.level !== NONE) { | ||
logger[level] = (...out) => { | ||
history.push([level, ...out]); | ||
if (opts.json) { | ||
history.push(out[0]); | ||
} else { | ||
history.push([level, ...out]); | ||
} | ||
}; | ||
@@ -186,0 +211,0 @@ }); |
{ | ||
"name": "@openfn/logger", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Cross-package logging utility", | ||
@@ -24,2 +24,3 @@ "module": "dist/index.js", | ||
"chalk": "^5.1.2", | ||
"fast-safe-stringify": "^2.1.1", | ||
"figures": "^5.0.0" | ||
@@ -26,0 +27,0 @@ }, |
@@ -25,2 +25,9 @@ ## @openfn/logger | ||
## JSON Output | ||
Set the `json` object to a truthy value to output all logs as JSON objects of the following structure: | ||
``` | ||
{ level: 'info', name: 'CLI', message: ['Loaded adaptor'] } | ||
``` | ||
## Usage | ||
@@ -27,0 +34,0 @@ |
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
11954
320
78
4
+ Addedfast-safe-stringify@^2.1.1
+ Addedfast-safe-stringify@2.1.1(transitive)