forked from Expensify/expensify-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.jsx
154 lines (137 loc) · 4.96 KB
/
Logger.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
import _ from 'underscore';
export default class Logger {
constructor({serverLoggingCallback, isDebug, clientLoggingCallback}) {
this.TEMP_LOG_LINES_TO_KEEP = 50;
// An array of log lines that limits itself to a certain number of entries (deleting the oldest)
this.logLines = [];
this.serverLoggingCallback = serverLoggingCallback;
this.clientLoggingCallback = clientLoggingCallback;
this.isDebug = isDebug;
// Public Methods
return {
info: this.info.bind(this),
alert: this.alert.bind(this),
warn: this.warn.bind(this),
hmmm: this.hmmm.bind(this),
client: this.client.bind(this),
};
}
/**
* Ask the server to write the log message
*
* @param {String} message The message to write
* @param {Number} recentMessages A number of recent messages to append as context
* @param {Object|String} parameters The parameters to send along with the message
*/
logToServer(message, recentMessages, parameters = {}) {
// Optionally append recent log lines as context
let msg = message;
if (recentMessages > 0) {
msg += ' | Context: ';
msg += JSON.stringify(this.get(recentMessages));
}
// Output the message to the console too.
if (this.isDebug) {
this.client(`${msg} - ${JSON.stringify(parameters)}`);
}
// We don't care about log setting web cookies so let's define it as false
const params = {parameters, message, api_setCookie: false};
this.serverLoggingCallback(params);
}
/**
* Add a message to the list
* @param {String} message
* @param {Object|String} parameters The parameters associated with the message
*/
add(message, parameters) {
const length = this.logLines.push({
message,
parameters,
timestamp: (new Date())
});
// If we're over the limit, remove one line.
if (length > this.TEMP_LOG_LINES_TO_KEEP) {
this.logLines.shift();
}
}
/**
* Get the last messageCount lines
* @param {Number} messageCount Number of messages to get (optional,
* defaults to 1)
* @return {array} an array of messages (oldest first)
*/
get(messageCount = 1) {
// Don't get more than we have
const count = Math.min(this.TEMP_LOG_LINES_TO_KEEP, messageCount);
return this.logLines.slice(this.logLines.length - count);
}
/**
* Caches an informational message locally, to be sent to the server if
* needed later.
*
* @param {String} message The message to log.
* @param {Boolean} sendNow if true, the message will be sent right away.
* @param {Object|String} parameters The parameters to send along with the message
*/
info(message, sendNow, parameters) {
if (sendNow) {
const msg = `[info] ${message}`;
this.logToServer(msg, 0, parameters);
} else {
this.add(message, parameters);
}
}
/**
* Logs an alert.
*
* @param {String} message The message to alert.
* @param {Number} recentMessages A number of recent messages to append as context
* @param {Object|String} parameters The parameters to send along with the message
* @param {Boolean} includeStackTrace Must be disabled for testing
*/
alert(message, recentMessages, parameters = {}, includeStackTrace = true) {
const msg = `[alrt] ${message}`;
const params = parameters;
if (includeStackTrace) {
params.stack = JSON.stringify(new Error().stack);
}
this.logToServer(msg, recentMessages, params);
this.add(msg, params);
}
/**
* Logs a warn.
*
* @param {String} message The message to warn.
* @param {Number} recentMessages A number of recent messages to append as context
* @param {Object|String} parameters The parameters to send along with the message
*/
warn(message, recentMessages, parameters) {
const msg = `[warn] ${message}`;
this.logToServer(msg, recentMessages, parameters);
this.add(msg, parameters);
}
/**
* Logs a hmmm.
*
* @param {String} message The message to hmmm.
* @param {Number} recentMessages A number of recent messages to append as context
* @param {Object|String} parameters The parameters to send along with the message
*/
hmmm(message, recentMessages, parameters) {
const msg = `[hmmm] ${message}`;
this.logToServer(msg, recentMessages, parameters);
this.add(msg, parameters);
}
/**
* Logs a message in the browser console.
*
* @param {String} message The message to log.
*/
client(message) {
this.add(message);
if (!this.clientLoggingCallback) {
return;
}
this.clientLoggingCallback(message);
}
}