@php-wasm/logger 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './logger';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class i{constructor(e){this.LOG_PREFIX="Playground",this.windowConnected=!1,this.lastPHPLogLength=0,this.errorLogPath="/wordpress/wp-content/debug.log",e&&(this.errorLogPath=e)}async getRequestPhpErrorLog(e){return await e.fileExists(this.errorLogPath)?await e.readFileAsText(this.errorLogPath):""}logWindowError(e){this.log(`${e.message} in ${e.filename} on line ${e.lineno}:${e.colno}`,"fatal")}logUnhandledRejection(e){this.log(`${e.reason.stack}`,"fatal")}addWindowErrorListener(){this.windowConnected||typeof window>"u"||(window.addEventListener("error",this.logWindowError.bind(this)),window.addEventListener("unhandledrejection",this.logUnhandledRejection.bind(this)),window.addEventListener("rejectionhandled",this.logUnhandledRejection.bind(this)),this.windowConnected=!0)}addPlaygroundRequestEndListener(e){e.addEventListener("request.end",async()=>{const t=await this.getRequestPhpErrorLog(e);t.length>this.lastPHPLogLength&&(this.logRaw(t.substring(this.lastPHPLogLength)),this.lastPHPLogLength=t.length)})}formatLogDate(e){const t=new Intl.DateTimeFormat("en-GB",{year:"numeric",month:"short",day:"2-digit",timeZone:"UTC"}).format(e).replace(/ /g,"-"),n=new Intl.DateTimeFormat("en-GB",{hour:"2-digit",minute:"2-digit",second:"2-digit",hour12:!1,timeZone:"UTC",timeZoneName:"short"}).format(e);return t+" "+n}formatMessage(e,t){return`[${this.formatLogDate(new Date)}] ${this.LOG_PREFIX} ${t}: ${e}`}log(e,t){t===void 0&&(t="info");const n=this.formatMessage(e,t);this.logRaw(n)}logRaw(e){console.debug(e)}}const r=new i;function s(o){o.addWindowErrorListener()}function a(o,e){o.addPlaygroundRequestEndListener(e)}exports.Logger=i;exports.collectPhpLogs=a;exports.collectWindowErrors=s;exports.logger=r;
package/index.mjs ADDED
@@ -0,0 +1,115 @@
1
+ class i {
2
+ constructor(t) {
3
+ this.LOG_PREFIX = "Playground", this.windowConnected = !1, this.lastPHPLogLength = 0, this.errorLogPath = "/wordpress/wp-content/debug.log", t && (this.errorLogPath = t);
4
+ }
5
+ /**
6
+ * Read the WordPress debug.log file and return its content.
7
+ *
8
+ * @param UniversalPHP playground instance
9
+ * @returns string The content of the debug.log file
10
+ */
11
+ async getRequestPhpErrorLog(t) {
12
+ return await t.fileExists(this.errorLogPath) ? await t.readFileAsText(this.errorLogPath) : "";
13
+ }
14
+ /**
15
+ * Log Windows errors.
16
+ *
17
+ * @param ErrorEvent event
18
+ */
19
+ logWindowError(t) {
20
+ this.log(
21
+ `${t.message} in ${t.filename} on line ${t.lineno}:${t.colno}`,
22
+ "fatal"
23
+ );
24
+ }
25
+ /**
26
+ * Log unhandled promise rejections.
27
+ *
28
+ * @param PromiseRejectionEvent event
29
+ */
30
+ logUnhandledRejection(t) {
31
+ this.log(`${t.reason.stack}`, "fatal");
32
+ }
33
+ /**
34
+ * Register a listener for the window error events and log the data.
35
+ */
36
+ addWindowErrorListener() {
37
+ this.windowConnected || typeof window > "u" || (window.addEventListener("error", this.logWindowError.bind(this)), window.addEventListener(
38
+ "unhandledrejection",
39
+ this.logUnhandledRejection.bind(this)
40
+ ), window.addEventListener(
41
+ "rejectionhandled",
42
+ this.logUnhandledRejection.bind(this)
43
+ ), this.windowConnected = !0);
44
+ }
45
+ /**
46
+ * Register a listener for the request.end event and log the data.
47
+ * @param UniversalPHP playground instance
48
+ */
49
+ addPlaygroundRequestEndListener(t) {
50
+ t.addEventListener("request.end", async () => {
51
+ const e = await this.getRequestPhpErrorLog(t);
52
+ e.length > this.lastPHPLogLength && (this.logRaw(e.substring(this.lastPHPLogLength)), this.lastPHPLogLength = e.length);
53
+ });
54
+ }
55
+ /**
56
+ * Get UTC date in the PHP log format https://github.com/php/php-src/blob/master/main/main.c#L849
57
+ *
58
+ * @param date
59
+ * @returns string
60
+ */
61
+ formatLogDate(t) {
62
+ const e = new Intl.DateTimeFormat("en-GB", {
63
+ year: "numeric",
64
+ month: "short",
65
+ day: "2-digit",
66
+ timeZone: "UTC"
67
+ }).format(t).replace(/ /g, "-"), n = new Intl.DateTimeFormat("en-GB", {
68
+ hour: "2-digit",
69
+ minute: "2-digit",
70
+ second: "2-digit",
71
+ hour12: !1,
72
+ timeZone: "UTC",
73
+ timeZoneName: "short"
74
+ }).format(t);
75
+ return e + " " + n;
76
+ }
77
+ /**
78
+ * Format log message and severity and log it.
79
+ * @param string message
80
+ * @param LogSeverity severity
81
+ */
82
+ formatMessage(t, e) {
83
+ return `[${this.formatLogDate(/* @__PURE__ */ new Date())}] ${this.LOG_PREFIX} ${e}: ${t}`;
84
+ }
85
+ /**
86
+ * Log message with severity and timestamp.
87
+ * @param string message
88
+ * @param LogSeverity severity
89
+ */
90
+ log(t, e) {
91
+ e === void 0 && (e = "info");
92
+ const n = this.formatMessage(t, e);
93
+ this.logRaw(n);
94
+ }
95
+ /**
96
+ * Log message without severity and timestamp.
97
+ * @param string log
98
+ */
99
+ logRaw(t) {
100
+ console.debug(t);
101
+ }
102
+ }
103
+ const r = new i();
104
+ function s(o) {
105
+ o.addWindowErrorListener();
106
+ }
107
+ function a(o, t) {
108
+ o.addPlaygroundRequestEndListener(t);
109
+ }
110
+ export {
111
+ i as Logger,
112
+ a as collectPhpLogs,
113
+ s as collectWindowErrors,
114
+ r as logger
115
+ };
package/logger.d.ts ADDED
@@ -0,0 +1,91 @@
1
+ import { UniversalPHP } from '../../universal/src/index.ts/src/lib/universal-php';
2
+ /**
3
+ * Log severity levels.
4
+ */
5
+ export type LogSeverity = 'debug' | 'info' | 'warn' | 'error' | 'fatal';
6
+ /**
7
+ * A logger for Playground.
8
+ */
9
+ export declare class Logger {
10
+ private readonly LOG_PREFIX;
11
+ /**
12
+ * Whether the window events are connected.
13
+ */
14
+ private windowConnected;
15
+ /**
16
+ * The length of the last PHP log.
17
+ */
18
+ private lastPHPLogLength;
19
+ /**
20
+ * The path to the error log file.
21
+ */
22
+ private errorLogPath;
23
+ constructor(errorLogPath?: string);
24
+ /**
25
+ * Read the WordPress debug.log file and return its content.
26
+ *
27
+ * @param UniversalPHP playground instance
28
+ * @returns string The content of the debug.log file
29
+ */
30
+ private getRequestPhpErrorLog;
31
+ /**
32
+ * Log Windows errors.
33
+ *
34
+ * @param ErrorEvent event
35
+ */
36
+ private logWindowError;
37
+ /**
38
+ * Log unhandled promise rejections.
39
+ *
40
+ * @param PromiseRejectionEvent event
41
+ */
42
+ private logUnhandledRejection;
43
+ /**
44
+ * Register a listener for the window error events and log the data.
45
+ */
46
+ addWindowErrorListener(): void;
47
+ /**
48
+ * Register a listener for the request.end event and log the data.
49
+ * @param UniversalPHP playground instance
50
+ */
51
+ addPlaygroundRequestEndListener(playground: UniversalPHP): void;
52
+ /**
53
+ * Get UTC date in the PHP log format https://github.com/php/php-src/blob/master/main/main.c#L849
54
+ *
55
+ * @param date
56
+ * @returns string
57
+ */
58
+ private formatLogDate;
59
+ /**
60
+ * Format log message and severity and log it.
61
+ * @param string message
62
+ * @param LogSeverity severity
63
+ */
64
+ formatMessage(message: string, severity: LogSeverity): string;
65
+ /**
66
+ * Log message with severity and timestamp.
67
+ * @param string message
68
+ * @param LogSeverity severity
69
+ */
70
+ log(message: string, severity?: LogSeverity): void;
71
+ /**
72
+ * Log message without severity and timestamp.
73
+ * @param string log
74
+ */
75
+ logRaw(log: string): void;
76
+ }
77
+ /**
78
+ * The logger instance.
79
+ */
80
+ export declare const logger: Logger;
81
+ /**
82
+ * Collect errors from JavaScript window events like error and log them.
83
+ * @param loggerInstance The logger instance
84
+ */
85
+ export declare function collectWindowErrors(loggerInstance: Logger): void;
86
+ /**
87
+ * Collect PHP logs from the error_log file and log them.
88
+ * @param UniversalPHP playground instance
89
+ * @param loggerInstance The logger instance
90
+ */
91
+ export declare function collectPhpLogs(loggerInstance: Logger, playground: UniversalPHP): void;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@php-wasm/logger",
3
+ "version": "0.6.4",
4
+ "description": "A logger for PHP-wasm clients like Playground and WP-now.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/WordPress/wordpress-playground"
8
+ },
9
+ "homepage": "https://developer.wordpress.org/playground",
10
+ "author": "The WordPress contributors",
11
+ "typedoc": {
12
+ "entryPoint": "./src/index.ts",
13
+ "readmeFile": "./README.md",
14
+ "displayName": "@php-wasm/logger",
15
+ "tsconfig": "./tsconfig.lib.json"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public",
19
+ "directory": "../../../dist/packages/php-wasm/logger"
20
+ },
21
+ "license": "GPL-2.0-or-later",
22
+ "main": "index.cjs",
23
+ "types": "index.d.ts",
24
+ "engines": {
25
+ "node": ">=18.18.2",
26
+ "npm": ">=8.11.0"
27
+ },
28
+ "gitHead": "8b74852e9701f5083b367f9a294f34200230ce79"
29
+ }