@php-wasm/logger
Advanced tools
Comparing version 0.7.19 to 0.7.20
208
index.js
@@ -1,4 +0,4 @@ | ||
const p = function() { | ||
var r; | ||
return typeof process < "u" && ((r = process.release) == null ? void 0 : r.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : ( | ||
const b = function() { | ||
var e; | ||
return typeof process < "u" && ((e = process.release) == null ? void 0 : e.name) === "node" ? "NODE" : typeof window < "u" ? "WEB" : ( | ||
// @ts-ignore | ||
@@ -9,4 +9,4 @@ typeof WorkerGlobalScope < "u" && // @ts-ignore | ||
}(); | ||
if (p === "NODE") { | ||
let r = function(t) { | ||
if (b === "NODE") { | ||
let e = function(t) { | ||
return new Promise(function(s, o) { | ||
@@ -17,3 +17,3 @@ t.onload = t.onerror = function(n) { | ||
}); | ||
}, e = function() { | ||
}, r = function() { | ||
const t = new Uint8Array([1, 2, 3, 4]), o = new File([t], "test").stream(); | ||
@@ -38,7 +38,7 @@ try { | ||
const s = new FileReader(); | ||
return s.readAsArrayBuffer(this), r(s); | ||
return s.readAsArrayBuffer(this), e(s); | ||
}), typeof Blob.prototype.text > "u" && (Blob.prototype.text = function() { | ||
const s = new FileReader(); | ||
return s.readAsText(this), r(s); | ||
}), (typeof Blob.prototype.stream > "u" || !e()) && (Blob.prototype.stream = function() { | ||
return s.readAsText(this), e(s); | ||
}), (typeof Blob.prototype.stream > "u" || !r()) && (Blob.prototype.stream = function() { | ||
let t = 0; | ||
@@ -55,6 +55,6 @@ const s = this; | ||
t + n.byteLength | ||
).arrayBuffer(), f = new Uint8Array(a); | ||
new Uint8Array(n.buffer).set(f); | ||
const c = f.byteLength; | ||
o.byobRequest.respond(c), t += c, t >= s.size && o.close(); | ||
).arrayBuffer(), u = new Uint8Array(a); | ||
new Uint8Array(n.buffer).set(u); | ||
const m = u.byteLength; | ||
o.byobRequest.respond(m), t += m, t >= s.size && o.close(); | ||
} | ||
@@ -64,4 +64,4 @@ }); | ||
} | ||
if (p === "NODE" && typeof CustomEvent > "u") { | ||
class r extends Event { | ||
if (b === "NODE" && typeof CustomEvent > "u") { | ||
class e extends Event { | ||
constructor(t, s = {}) { | ||
@@ -73,45 +73,43 @@ super(t, s), this.detail = s.detail; | ||
} | ||
globalThis.CustomEvent = r; | ||
globalThis.CustomEvent = e; | ||
} | ||
const w = (r, ...e) => { | ||
switch (r.severity) { | ||
const E = (e, ...r) => { | ||
switch (typeof e.message == "string" ? e.message = l(e.message) : e.message.message && typeof e.message.message == "string" && (e.message.message = l(e.message.message)), e.severity) { | ||
case "Debug": | ||
console.debug(r.message, ...e); | ||
console.debug(e.message, ...r); | ||
break; | ||
case "Info": | ||
console.info(r.message, ...e); | ||
console.info(e.message, ...r); | ||
break; | ||
case "Warn": | ||
console.warn(r.message, ...e); | ||
console.warn(e.message, ...r); | ||
break; | ||
case "Error": | ||
console.error(r.message, ...e); | ||
console.error(e.message, ...r); | ||
break; | ||
case "Fatal": | ||
console.error(r.message, ...e); | ||
console.error(e.message, ...r); | ||
break; | ||
default: | ||
console.log(r.message, ...e); | ||
console.log(e.message, ...r); | ||
} | ||
}, E = (r, ...e) => [ | ||
typeof r == "object" ? JSON.stringify(r) : r, | ||
...e.map((t) => JSON.stringify(t)) | ||
].join(" "), h = [], u = (r) => { | ||
h.push(r); | ||
}, b = (r) => { | ||
if (r.raw === !0) | ||
u(r.message); | ||
}, v = (e) => e instanceof Error ? [e.message, e.stack].join(` | ||
`) : JSON.stringify(e, null, 2), w = [], g = (e) => { | ||
w.push(e); | ||
}, d = (e) => { | ||
if (e.raw === !0) | ||
g(e.message); | ||
else { | ||
const e = L( | ||
typeof r.message == "object" ? E(r.message) : r.message, | ||
r.severity ?? "Info", | ||
r.prefix ?? "JavaScript" | ||
const r = T( | ||
typeof e.message == "object" ? v(e.message) : e.message, | ||
e.severity ?? "Info", | ||
e.prefix ?? "JavaScript" | ||
); | ||
u(e); | ||
g(r); | ||
} | ||
}; | ||
class v extends EventTarget { | ||
class L extends EventTarget { | ||
// constructor | ||
constructor(e = []) { | ||
super(), this.handlers = e, this.fatalErrorEvent = "playground-fatal-error"; | ||
constructor(r = []) { | ||
super(), this.handlers = r, this.fatalErrorEvent = "playground-fatal-error"; | ||
} | ||
@@ -123,3 +121,3 @@ /** | ||
getLogs() { | ||
return this.handlers.includes(b) ? [...h] : (this.error(`Logs aren't stored because the logToMemory handler isn't registered. | ||
return this.handlers.includes(d) ? [...w] : (this.error(`Logs aren't stored because the logToMemory handler isn't registered. | ||
If you're using a custom logger instance, make sure to register logToMemory handler. | ||
@@ -136,5 +134,5 @@ `), []); | ||
*/ | ||
logMessage(e, ...t) { | ||
logMessage(r, ...t) { | ||
for (const s of this.handlers) | ||
s(e, ...t); | ||
s(r, ...t); | ||
} | ||
@@ -147,6 +145,6 @@ /** | ||
*/ | ||
log(e, ...t) { | ||
log(r, ...t) { | ||
this.logMessage( | ||
{ | ||
message: e, | ||
message: r, | ||
severity: void 0, | ||
@@ -165,6 +163,6 @@ prefix: "JavaScript", | ||
*/ | ||
debug(e, ...t) { | ||
debug(r, ...t) { | ||
this.logMessage( | ||
{ | ||
message: e, | ||
message: r, | ||
severity: "Debug", | ||
@@ -183,6 +181,6 @@ prefix: "JavaScript", | ||
*/ | ||
info(e, ...t) { | ||
info(r, ...t) { | ||
this.logMessage( | ||
{ | ||
message: e, | ||
message: r, | ||
severity: "Info", | ||
@@ -201,6 +199,6 @@ prefix: "JavaScript", | ||
*/ | ||
warn(e, ...t) { | ||
warn(r, ...t) { | ||
this.logMessage( | ||
{ | ||
message: e, | ||
message: r, | ||
severity: "Warn", | ||
@@ -219,6 +217,6 @@ prefix: "JavaScript", | ||
*/ | ||
error(e, ...t) { | ||
error(r, ...t) { | ||
this.logMessage( | ||
{ | ||
message: e, | ||
message: r, | ||
severity: "Error", | ||
@@ -232,3 +230,10 @@ prefix: "JavaScript", | ||
} | ||
const B = new v([b, w]), L = (r, e, t) => { | ||
const M = () => { | ||
try { | ||
if (process.env.NODE_ENV === "test") | ||
return [d]; | ||
} catch { | ||
} | ||
return [d, E]; | ||
}, k = new L(M()), l = (e) => e.replace(/\t/g, ""), T = (e, r, t) => { | ||
const s = /* @__PURE__ */ new Date(), o = new Intl.DateTimeFormat("en-GB", { | ||
@@ -246,27 +251,27 @@ year: "numeric", | ||
timeZoneName: "short" | ||
}).format(s); | ||
return `[${o + " " + n}] ${t} ${e}: ${r}`; | ||
}, O = (r, e) => { | ||
r.addEventListener(r.fatalErrorEvent, e); | ||
}).format(s), i = o + " " + n; | ||
return e = l(e), `[${i}] ${t} ${r}: ${e}`; | ||
}, P = (e, r) => { | ||
e.addEventListener(e.fatalErrorEvent, r); | ||
}; | ||
let l = 0; | ||
const g = "/wordpress/wp-content/debug.log", M = async (r) => await r.fileExists(g) ? await r.readFileAsText(g) : "", P = (r, e) => { | ||
e.addEventListener("request.end", async () => { | ||
const t = await M(e); | ||
if (t.length > l) { | ||
const s = t.substring(l); | ||
r.logMessage({ | ||
let f = 0; | ||
const p = "/wordpress/wp-content/debug.log", x = async (e) => await e.fileExists(p) ? await e.readFileAsText(p) : "", D = (e, r) => { | ||
r.addEventListener("request.end", async () => { | ||
const t = await x(r); | ||
if (t.length > f) { | ||
const s = t.substring(f); | ||
e.logMessage({ | ||
message: s, | ||
raw: !0 | ||
}), l = t.length; | ||
}), f = t.length; | ||
} | ||
}), e.addEventListener("request.error", (t) => { | ||
t = t, t.error && (r.logMessage({ | ||
}), r.addEventListener("request.error", (t) => { | ||
t = t, t.error && (e.logMessage({ | ||
message: `${t.error.message} ${t.error.stack}`, | ||
severity: "Fatal", | ||
prefix: t.source === "request" ? "PHP" : "WASM Crash" | ||
}), r.dispatchEvent( | ||
new CustomEvent(r.fatalErrorEvent, { | ||
}), e.dispatchEvent( | ||
new CustomEvent(e.fatalErrorEvent, { | ||
detail: { | ||
logs: r.getLogs(), | ||
logs: e.getLogs(), | ||
source: t.source | ||
@@ -277,12 +282,12 @@ } | ||
}); | ||
}, T = (r, e) => { | ||
r.logMessage({ | ||
message: `${e.message} in ${e.filename} on line ${e.lineno}:${e.colno}`, | ||
}, B = (e, r) => { | ||
e.logMessage({ | ||
message: `${r.message} in ${r.filename} on line ${r.lineno}:${r.colno}`, | ||
severity: "Error" | ||
}); | ||
}, m = (r, e) => { | ||
if (!(e != null && e.reason)) | ||
}, y = (e, r) => { | ||
if (!(r != null && r.reason)) | ||
return; | ||
const t = (e == null ? void 0 : e.reason.stack) ?? e.reason; | ||
r.logMessage({ | ||
const t = (r == null ? void 0 : r.reason.stack) ?? r.reason; | ||
e.logMessage({ | ||
message: t, | ||
@@ -292,28 +297,28 @@ severity: "Error" | ||
}; | ||
let d = 0; | ||
const x = (r) => { | ||
navigator.serviceWorker.addEventListener("message", (e) => { | ||
let c = 0; | ||
const O = (e) => { | ||
navigator.serviceWorker.addEventListener("message", (r) => { | ||
var t, s, o; | ||
((t = e.data) == null ? void 0 : t.numberOfOpenPlaygroundTabs) !== void 0 && d !== ((s = e.data) == null ? void 0 : s.numberOfOpenPlaygroundTabs) && (d = (o = e.data) == null ? void 0 : o.numberOfOpenPlaygroundTabs, r.debug( | ||
`Number of open Playground tabs is: ${d}` | ||
((t = r.data) == null ? void 0 : t.numberOfOpenPlaygroundTabs) !== void 0 && c !== ((s = r.data) == null ? void 0 : s.numberOfOpenPlaygroundTabs) && (c = (o = r.data) == null ? void 0 : o.numberOfOpenPlaygroundTabs, e.debug( | ||
`Number of open Playground tabs is: ${c}` | ||
)); | ||
}); | ||
}; | ||
let y = !1; | ||
const S = (r) => { | ||
y || (x(r), !(typeof window > "u") && (window.addEventListener( | ||
let h = !1; | ||
const S = (e) => { | ||
h || (O(e), !(typeof window > "u") && (window.addEventListener( | ||
"error", | ||
(e) => T(r, e) | ||
(r) => B(e, r) | ||
), window.addEventListener( | ||
"unhandledrejection", | ||
(e) => m(r, e) | ||
(r) => y(e, r) | ||
), window.addEventListener( | ||
"rejectionhandled", | ||
(e) => m(r, e) | ||
), y = !0)); | ||
}, k = (r) => { | ||
r.addEventListener("activate", () => { | ||
r.clients.matchAll().then((e) => { | ||
(r) => y(e, r) | ||
), h = !0)); | ||
}, F = (e) => { | ||
e.addEventListener("activate", () => { | ||
e.clients.matchAll().then((r) => { | ||
const t = { | ||
numberOfOpenPlaygroundTabs: e.filter( | ||
numberOfOpenPlaygroundTabs: r.filter( | ||
// Only count top-level frames to get the number of tabs. | ||
@@ -323,3 +328,3 @@ (s) => s.frameType === "top-level" | ||
}; | ||
for (const s of e) | ||
for (const s of r) | ||
s.postMessage(t); | ||
@@ -330,9 +335,10 @@ }); | ||
export { | ||
v as Logger, | ||
O as addCrashListener, | ||
P as collectPhpLogs, | ||
L as Logger, | ||
P as addCrashListener, | ||
D as collectPhpLogs, | ||
S as collectWindowErrors, | ||
L as formatLogEntry, | ||
B as logger, | ||
k as reportServiceWorkerMetrics | ||
T as formatLogEntry, | ||
k as logger, | ||
l as prepareLogMessage, | ||
F as reportServiceWorkerMetrics | ||
}; |
@@ -76,2 +76,3 @@ export type Log = { | ||
export declare const logger: Logger; | ||
export declare const prepareLogMessage: (message: string) => string; | ||
export declare const formatLogEntry: (message: string, severity: LogSeverity, prefix: string) => string; | ||
@@ -78,0 +79,0 @@ /** |
{ | ||
"name": "@php-wasm/logger", | ||
"version": "0.7.19", | ||
"version": "0.7.20", | ||
"description": "A logger for PHP-wasm clients like Playground and WP-now.", | ||
@@ -30,3 +30,3 @@ "repository": { | ||
}, | ||
"gitHead": "687ec237085853ff5c808814c28be349146a5d1a" | ||
"gitHead": "5915ef756c88da8dcb665f9f0e49ddc0c0b10d50" | ||
} |
Sorry, the diff of this file is not supported yet
18669
483