cordova-plugin-unvired-logger 0.0.5 → 0.0.7
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 +2 -2
- package/src/browser/Logger.js +143 -194
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.7",
|
|
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.7"
|
|
3
3
|
xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
|
4
4
|
xmlns:android="http://schemas.android.com/apk/res/android">
|
|
5
5
|
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
|
|
44
44
|
<platform name="browser">
|
|
45
45
|
<js-module src="src/browser/Logger.js" name="UnviredLogger">
|
|
46
|
-
<
|
|
46
|
+
<run />
|
|
47
47
|
</js-module>
|
|
48
48
|
</platform>
|
|
49
49
|
</plugin>
|
package/src/browser/Logger.js
CHANGED
|
@@ -15,230 +15,179 @@ 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
|
+
loggerWithLevel(successCallback, errorCallback, args);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
function
|
|
23
|
-
|
|
22
|
+
module.exports.logError = async function (successCallback, errorCallback, args) {
|
|
23
|
+
loggerWithLevel(successCallback, errorCallback, args);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
function
|
|
27
|
-
|
|
26
|
+
module.exports.logInfo = async function (successCallback, errorCallback, args) {
|
|
27
|
+
loggerWithLevel(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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
)
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
data = `⭕️⭕️⭕️ ${data}`;
|
|
155
|
-
}
|
|
156
|
-
|
|
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);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
resolve(`Logged to browser storage: ${storageKey}`);
|
|
175
|
-
} catch (err) {
|
|
176
|
-
reject(new Error(`Logging failed: ${err.message}`));
|
|
99
|
+
module.exports.loggerWithLevel = async function(successCallback, errorCallback, args) {
|
|
100
|
+
try {
|
|
101
|
+
if (!args || !args[0]) {
|
|
102
|
+
return errorCallback(new Error("Invalid arguments provided"));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const { userId, level, sourceClass, sourceMethod, message } = args[0];
|
|
106
|
+
|
|
107
|
+
if (!userId) return reject(new Error("userId is required"));
|
|
108
|
+
if (!level) return reject(new Error("level is required"));
|
|
109
|
+
if (!message) return reject(new Error("message is required"));
|
|
110
|
+
|
|
111
|
+
// Normalize log levels for comparison
|
|
112
|
+
const normalizedDefaultLogLevel = (defaultLogLevel || '').toLowerCase();
|
|
113
|
+
const normalizedLevel = (level || '').toLowerCase();
|
|
114
|
+
|
|
115
|
+
// Log level filtering using normalized values
|
|
116
|
+
if (
|
|
117
|
+
(normalizedDefaultLogLevel === LogLevel.Error && (normalizedLevel === LogLevel.Debug || normalizedLevel === LogLevel.Info)) ||
|
|
118
|
+
(normalizedDefaultLogLevel === LogLevel.Info && normalizedLevel === LogLevel.Debug)
|
|
119
|
+
) {
|
|
120
|
+
return successCallback();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
await checkAndRotateLogFile(args);
|
|
124
|
+
|
|
125
|
+
const currentDate = new Date();
|
|
126
|
+
const formatter = new Intl.DateTimeFormat('en-US', {
|
|
127
|
+
day: '2-digit', month: '2-digit', year: 'numeric',
|
|
128
|
+
hour: '2-digit', minute: '2-digit', second: '2-digit'
|
|
129
|
+
});
|
|
130
|
+
const localDateString = formatter.format(currentDate);
|
|
131
|
+
|
|
132
|
+
const dateUtc = new Date(currentDate.toUTCString());
|
|
133
|
+
const utcFormatter = new Intl.DateTimeFormat('en-US', {
|
|
134
|
+
day: '2-digit', month: '2-digit', year: 'numeric',
|
|
135
|
+
hour: '2-digit', minute: '2-digit', second: '2-digit', timeZone: 'UTC'
|
|
136
|
+
});
|
|
137
|
+
const utcDateString = utcFormatter.format(dateUtc);
|
|
138
|
+
|
|
139
|
+
let data = `${localDateString} | UTC:${utcDateString} | ${getStringFromLevel(level)} | ${sourceClass} | ${sourceMethod} | ${message}\n`;
|
|
140
|
+
if (level === LogLevel.Error) {
|
|
141
|
+
data = `⭕️⭕️⭕️ ${data}`;
|
|
177
142
|
}
|
|
178
|
-
|
|
143
|
+
|
|
144
|
+
// Log to console for browser debugging
|
|
145
|
+
console.log(data);
|
|
146
|
+
|
|
147
|
+
// Store in localStorage
|
|
148
|
+
const storageKey = getStorageKey(userId, LOG_FILE_NAME);
|
|
149
|
+
const existingContent = localStorage.getItem(storageKey) || '';
|
|
150
|
+
const newContent = existingContent + data;
|
|
151
|
+
|
|
152
|
+
// Check size limit
|
|
153
|
+
if (newContent.length > MAX_LOG_SIZE) {
|
|
154
|
+
// If content is too large, truncate it
|
|
155
|
+
const truncatedContent = newContent.substring(newContent.length - MAX_LOG_SIZE / 2);
|
|
156
|
+
localStorage.setItem(storageKey, truncatedContent);
|
|
157
|
+
} else {
|
|
158
|
+
localStorage.setItem(storageKey, newContent);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
successCallback(`Logged to browser storage: ${storageKey}`);
|
|
162
|
+
} catch (err) {
|
|
163
|
+
errorCallback(new Error(`Logging failed: ${err.message}`));
|
|
164
|
+
}
|
|
179
165
|
}
|
|
180
166
|
|
|
181
|
-
function
|
|
167
|
+
module.exports.getStringFromLevel = async function (successCallback, errorCallback, level) {
|
|
182
168
|
switch (level) {
|
|
183
|
-
case LogLevel.Info:
|
|
184
|
-
case LogLevel.Error:
|
|
185
|
-
case LogLevel.Debug:
|
|
186
|
-
default:
|
|
169
|
+
case LogLevel.Info: successCallback('IMPORTANT');return;
|
|
170
|
+
case LogLevel.Error: successCallback('ERROR');return;
|
|
171
|
+
case LogLevel.Debug: successCallback('DEBUG');return;
|
|
172
|
+
default: successCallback('');return;
|
|
187
173
|
}
|
|
188
174
|
}
|
|
189
175
|
|
|
190
|
-
function
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
localStorage.setItem(storageKey, '');
|
|
201
|
-
}
|
|
202
|
-
resolve();
|
|
203
|
-
} catch (err) {
|
|
204
|
-
reject(new Error(`Failed to rotate log file: ${err.message}`));
|
|
176
|
+
module.exports.checkAndRotateLogFile = async function (successCallback, errorCallback, args) {
|
|
177
|
+
try {
|
|
178
|
+
const userId = args[0].userId;
|
|
179
|
+
const storageKey = getStorageKey(userId, LOG_FILE_NAME);
|
|
180
|
+
const content = localStorage.getItem(storageKey) || '';
|
|
181
|
+
|
|
182
|
+
if (content.length > MAX_LOG_SIZE) {
|
|
183
|
+
const backupStorageKey = getStorageKey(userId, BACKUP_LOG_FILE_NAME);
|
|
184
|
+
localStorage.setItem(backupStorageKey, content);
|
|
185
|
+
localStorage.setItem(storageKey, '');
|
|
205
186
|
}
|
|
206
|
-
|
|
187
|
+
successCallback();
|
|
188
|
+
} catch (err) {
|
|
189
|
+
errorCallback(new Error(`Failed to rotate log file: ${err.message}`));
|
|
190
|
+
}
|
|
207
191
|
}
|
|
208
192
|
|
|
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
|
-
}
|
|
193
|
+
require('cordova/exec/proxy').add('Logger', module.exports);
|