cordova-plugin-unvired-logger 0.0.6 → 0.0.8
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/package.json +1 -1
- package/plugin.xml +1 -1
- package/src/browser/Logger.js +134 -179
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cordova-plugin-unvired-logger",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "A logger plugin for Electron, Android, Browser, and iOS that appends logs to log.txt files organized by user ID.",
|
|
5
5
|
"cordova": {
|
|
6
6
|
"id": "cordova-plugin-unvired-logger",
|
package/plugin.xml
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<plugin id="cordova-plugin-unvired-logger" version="0.0.
|
|
2
|
+
<plugin id="cordova-plugin-unvired-logger" version="0.0.8"
|
|
3
3
|
xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
|
4
4
|
xmlns:android="http://schemas.android.com/apk/res/android">
|
|
5
5
|
|
package/src/browser/Logger.js
CHANGED
|
@@ -15,180 +15,170 @@ let defaultLogLevel = LogLevel.Info;
|
|
|
15
15
|
const getStorageKey = (userId, fileName) => `unvired_logger_${userId}_${fileName}`;
|
|
16
16
|
const getLogLevelKey = (userId) => `unvired_logger_${userId}_level`;
|
|
17
17
|
|
|
18
|
-
function
|
|
19
|
-
|
|
18
|
+
module.exports.logDebug = async function (successCallback, errorCallback, args) {
|
|
19
|
+
await logWitLevel(successCallback, errorCallback, args);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
function
|
|
23
|
-
|
|
22
|
+
module.exports.logError = async function (successCallback, errorCallback, args) {
|
|
23
|
+
await logWitLevel(successCallback, errorCallback, args);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
function
|
|
27
|
-
|
|
26
|
+
module.exports.logInfo = async function (successCallback, errorCallback, args) {
|
|
27
|
+
await logWitLevel(successCallback, errorCallback, args);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
resolve();
|
|
39
|
-
});
|
|
30
|
+
module.exports.setLogLevel = function(successCallback, errorCallback, level) {
|
|
31
|
+
// If level is an array (from Cordova), extract the first element
|
|
32
|
+
if (Array.isArray(level)) {
|
|
33
|
+
defaultLogLevel = level[0];
|
|
34
|
+
} else {
|
|
35
|
+
defaultLogLevel = level;
|
|
36
|
+
}
|
|
37
|
+
successCallback("")
|
|
40
38
|
}
|
|
41
39
|
|
|
42
|
-
function
|
|
43
|
-
|
|
44
|
-
resolve(defaultLogLevel);
|
|
45
|
-
});
|
|
40
|
+
module.exports.getLogLevel = function(successCallback, errorCallback) {
|
|
41
|
+
successCallback(defaultLogLevel)
|
|
46
42
|
}
|
|
47
43
|
|
|
48
|
-
function
|
|
44
|
+
module.exports.getLogFileURL = function(successCallback, errorCallback, args) {
|
|
49
45
|
// In browser, we return a data URL or storage reference
|
|
50
|
-
|
|
46
|
+
successCallback(`browser://${getStorageKey(args[0].userId, LOG_FILE_NAME)}`);
|
|
51
47
|
}
|
|
52
48
|
|
|
53
|
-
function
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
});
|
|
49
|
+
module.exports.getLogFileContent = async function (successCallback, errorCallback, args) {
|
|
50
|
+
try {
|
|
51
|
+
const userId = args[0].userId;
|
|
52
|
+
const storageKey = getStorageKey(userId, LOG_FILE_NAME);
|
|
53
|
+
const content = localStorage.getItem(storageKey) || '';
|
|
54
|
+
successCallback(content);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
errorCallback(new Error(`Failed to read log file: ${err.message}`));
|
|
57
|
+
}
|
|
64
58
|
}
|
|
65
59
|
|
|
66
|
-
function
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
});
|
|
60
|
+
module.exports.clearLogFile = async function (successCallback, errorCallback, args) {
|
|
61
|
+
try {
|
|
62
|
+
const userId = args[0].userId;
|
|
63
|
+
const storageKey = getStorageKey(userId, LOG_FILE_NAME);
|
|
64
|
+
localStorage.removeItem(storageKey);
|
|
65
|
+
successCallback();
|
|
66
|
+
} catch (err) {
|
|
67
|
+
errorCallback(new Error(`Failed to clear log file: ${err.message}`));
|
|
68
|
+
}
|
|
77
69
|
}
|
|
78
70
|
|
|
79
|
-
function
|
|
80
|
-
|
|
71
|
+
module.exports.getBackupLogFileURL = function (successCallback, errorCallback, args) {
|
|
72
|
+
successCallback(`browser://${getStorageKey(args[0].userId, BACKUP_LOG_FILE_NAME)}`);
|
|
81
73
|
}
|
|
82
74
|
|
|
83
|
-
function
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
});
|
|
75
|
+
module.exports.getBackupLogFileContent = async function(successCallback, errorCallback, args) {
|
|
76
|
+
try {
|
|
77
|
+
const userId = args[0].userId;
|
|
78
|
+
const storageKey = getStorageKey(userId, BACKUP_LOG_FILE_NAME);
|
|
79
|
+
const content = localStorage.getItem(storageKey) || '';
|
|
80
|
+
successCallback(content);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
errorCallback(new Error(`Failed to read backup log file: ${err.message}`));
|
|
83
|
+
}
|
|
94
84
|
}
|
|
95
85
|
|
|
96
|
-
function
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
});
|
|
86
|
+
module.exports.copyLogToBackup = async function(successCallback, errorCallback, args) {
|
|
87
|
+
try {
|
|
88
|
+
const userId = args[0].userId;
|
|
89
|
+
const logStorageKey = getStorageKey(userId, LOG_FILE_NAME);
|
|
90
|
+
const backupStorageKey = getStorageKey(userId, BACKUP_LOG_FILE_NAME);
|
|
91
|
+
const logContent = localStorage.getItem(logStorageKey) || '';
|
|
92
|
+
localStorage.setItem(backupStorageKey, logContent);
|
|
93
|
+
successCallback();
|
|
94
|
+
} catch (err) {
|
|
95
|
+
errorCallback(new Error(`Failed to copy log file to backup: ${err.message}`));
|
|
96
|
+
}
|
|
109
97
|
}
|
|
110
98
|
|
|
111
|
-
function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (!args || !args[0]) {
|
|
115
|
-
return reject(new Error("Invalid arguments provided"));
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const { userId, level, sourceClass, sourceMethod, message } = args[0];
|
|
119
|
-
|
|
120
|
-
if (!userId) return reject(new Error("userId is required"));
|
|
121
|
-
if (!level) return reject(new Error("level is required"));
|
|
122
|
-
if (!message) return reject(new Error("message is required"));
|
|
123
|
-
|
|
124
|
-
// Normalize log levels for comparison
|
|
125
|
-
const normalizedDefaultLogLevel = (defaultLogLevel || '').toLowerCase();
|
|
126
|
-
const normalizedLevel = (level || '').toLowerCase();
|
|
127
|
-
|
|
128
|
-
// Log level filtering using normalized values
|
|
129
|
-
if (
|
|
130
|
-
(normalizedDefaultLogLevel === LogLevel.Error && (normalizedLevel === LogLevel.Debug || normalizedLevel === LogLevel.Info)) ||
|
|
131
|
-
(normalizedDefaultLogLevel === LogLevel.Info && normalizedLevel === LogLevel.Debug)
|
|
132
|
-
) {
|
|
133
|
-
return resolve();
|
|
134
|
-
}
|
|
99
|
+
module.exports.loggerWithLevel = async function(successCallback, errorCallback, args) {
|
|
100
|
+
await logWitLevel(successCallback, errorCallback, args)
|
|
101
|
+
}
|
|
135
102
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const dateUtc = new Date(currentDate.toUTCString());
|
|
146
|
-
const utcFormatter = new Intl.DateTimeFormat('en-US', {
|
|
147
|
-
day: '2-digit', month: '2-digit', year: 'numeric',
|
|
148
|
-
hour: '2-digit', minute: '2-digit', second: '2-digit', timeZone: 'UTC'
|
|
149
|
-
});
|
|
150
|
-
const utcDateString = utcFormatter.format(dateUtc);
|
|
151
|
-
|
|
152
|
-
let data = `${localDateString} | UTC:${utcDateString} | ${getStringFromLevel(level)} | ${sourceClass} | ${sourceMethod} | ${message}\n`;
|
|
153
|
-
if (level === LogLevel.Error) {
|
|
154
|
-
data = `⭕️⭕️⭕️ ${data}`;
|
|
155
|
-
}
|
|
103
|
+
module.exports.getStringFromLevel = async function (successCallback, errorCallback, level) {
|
|
104
|
+
switch (level) {
|
|
105
|
+
case LogLevel.Info: successCallback('IMPORTANT');return;
|
|
106
|
+
case LogLevel.Error: successCallback('ERROR');return;
|
|
107
|
+
case LogLevel.Debug: successCallback('DEBUG');return;
|
|
108
|
+
default: successCallback('');return;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
156
111
|
|
|
157
|
-
|
|
158
|
-
|
|
112
|
+
async function logWitLevel(successCallback, errorCallback, args) {
|
|
113
|
+
try {
|
|
114
|
+
if (!args || !args[0]) {
|
|
115
|
+
return errorCallback(new Error("Invalid arguments provided"));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const { userId, level, sourceClass, sourceMethod, message } = args[0];
|
|
119
|
+
|
|
120
|
+
if (!userId) return reject(new Error("userId is required"));
|
|
121
|
+
if (!level) return reject(new Error("level is required"));
|
|
122
|
+
if (!message) return reject(new Error("message is required"));
|
|
123
|
+
|
|
124
|
+
// Normalize log levels for comparison
|
|
125
|
+
const normalizedDefaultLogLevel = (defaultLogLevel || '').toLowerCase();
|
|
126
|
+
const normalizedLevel = (level || '').toLowerCase();
|
|
127
|
+
|
|
128
|
+
// Log level filtering using normalized values
|
|
129
|
+
if (
|
|
130
|
+
(normalizedDefaultLogLevel === LogLevel.Error && (normalizedLevel === LogLevel.Debug || normalizedLevel === LogLevel.Info)) ||
|
|
131
|
+
(normalizedDefaultLogLevel === LogLevel.Info && normalizedLevel === LogLevel.Debug)
|
|
132
|
+
) {
|
|
133
|
+
return successCallback();
|
|
134
|
+
}
|
|
159
135
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
136
|
+
await checkAndRotateLogFile(args);
|
|
137
|
+
|
|
138
|
+
const currentDate = new Date();
|
|
139
|
+
const formatter = new Intl.DateTimeFormat('en-US', {
|
|
140
|
+
day: '2-digit', month: '2-digit', year: 'numeric',
|
|
141
|
+
hour: '2-digit', minute: '2-digit', second: '2-digit'
|
|
142
|
+
});
|
|
143
|
+
const localDateString = formatter.format(currentDate);
|
|
144
|
+
|
|
145
|
+
const dateUtc = new Date(currentDate.toUTCString());
|
|
146
|
+
const utcFormatter = new Intl.DateTimeFormat('en-US', {
|
|
147
|
+
day: '2-digit', month: '2-digit', year: 'numeric',
|
|
148
|
+
hour: '2-digit', minute: '2-digit', second: '2-digit', timeZone: 'UTC'
|
|
149
|
+
});
|
|
150
|
+
const utcDateString = utcFormatter.format(dateUtc);
|
|
151
|
+
|
|
152
|
+
let data = `${localDateString} | UTC:${utcDateString} | ${getStringFromLevel(level)} | ${sourceClass} | ${sourceMethod} | ${message}\n`;
|
|
153
|
+
if (level === LogLevel.Error) {
|
|
154
|
+
data = `⭕️⭕️⭕️ ${data}`;
|
|
155
|
+
}
|
|
173
156
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
157
|
+
// Log to console for browser debugging
|
|
158
|
+
console.log(data);
|
|
159
|
+
|
|
160
|
+
// Store in localStorage
|
|
161
|
+
const storageKey = getStorageKey(userId, LOG_FILE_NAME);
|
|
162
|
+
const existingContent = localStorage.getItem(storageKey) || '';
|
|
163
|
+
const newContent = existingContent + data;
|
|
164
|
+
|
|
165
|
+
// Check size limit
|
|
166
|
+
if (newContent.length > MAX_LOG_SIZE) {
|
|
167
|
+
// If content is too large, truncate it
|
|
168
|
+
const truncatedContent = newContent.substring(newContent.length - MAX_LOG_SIZE / 2);
|
|
169
|
+
localStorage.setItem(storageKey, truncatedContent);
|
|
170
|
+
} else {
|
|
171
|
+
localStorage.setItem(storageKey, newContent);
|
|
177
172
|
}
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
173
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
case LogLevel.Error: return 'ERROR';
|
|
185
|
-
case LogLevel.Debug: return 'DEBUG';
|
|
186
|
-
default: return '';
|
|
174
|
+
successCallback(`Logged to browser storage: ${storageKey}`);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
errorCallback(new Error(`Logging failed: ${err.message}`));
|
|
187
177
|
}
|
|
188
178
|
}
|
|
189
179
|
|
|
190
|
-
function checkAndRotateLogFile(args) {
|
|
191
|
-
return new Promise(
|
|
180
|
+
async function checkAndRotateLogFile (successCallback, errorCallback, args) {
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
192
182
|
try {
|
|
193
183
|
const userId = args[0].userId;
|
|
194
184
|
const storageKey = getStorageKey(userId, LOG_FILE_NAME);
|
|
@@ -203,42 +193,7 @@ function checkAndRotateLogFile(args) {
|
|
|
203
193
|
} catch (err) {
|
|
204
194
|
reject(new Error(`Failed to rotate log file: ${err.message}`));
|
|
205
195
|
}
|
|
206
|
-
})
|
|
196
|
+
})
|
|
207
197
|
}
|
|
208
198
|
|
|
209
|
-
|
|
210
|
-
if (typeof module !== 'undefined' && module.exports) {
|
|
211
|
-
module.exports = {
|
|
212
|
-
logDebug,
|
|
213
|
-
logError,
|
|
214
|
-
logInfo,
|
|
215
|
-
setLogLevel,
|
|
216
|
-
getLogLevel,
|
|
217
|
-
getLogFileContent,
|
|
218
|
-
clearLogFile,
|
|
219
|
-
getBackupLogFileContent,
|
|
220
|
-
LogLevel,
|
|
221
|
-
copyLogToBackup,
|
|
222
|
-
getLogFileURL,
|
|
223
|
-
getBackupLogFileURL,
|
|
224
|
-
};
|
|
225
|
-
require('cordova/exec/proxy').add('UnviredLogger', module.exports);
|
|
226
|
-
} else if (typeof window !== 'undefined') {
|
|
227
|
-
// Browser environment
|
|
228
|
-
window.UnviredLogger = {
|
|
229
|
-
logDebug,
|
|
230
|
-
logError,
|
|
231
|
-
logInfo,
|
|
232
|
-
setLogLevel,
|
|
233
|
-
getLogLevel,
|
|
234
|
-
getLogFileContent,
|
|
235
|
-
clearLogFile,
|
|
236
|
-
getBackupLogFileContent,
|
|
237
|
-
LogLevel,
|
|
238
|
-
copyLogToBackup,
|
|
239
|
-
getLogFileURL,
|
|
240
|
-
getBackupLogFileURL,
|
|
241
|
-
};
|
|
242
|
-
window.Logger = window.UnviredLogger;
|
|
243
|
-
require('cordova/exec/proxy').add('UnviredLogger', window.Logger);
|
|
244
|
-
}
|
|
199
|
+
require('cordova/exec/proxy').add('Logger', module.exports);
|