forked from athombv/node-homey-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (185 loc) · 5.76 KB
/
index.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'use strict';
const os = require('node:os');
if (!process.env.HOME) {
// Sentry SDK requires the HOME env var to be set, which is not guaranteed on Homey
process.env.HOME = os.tmpdir();
}
// eslint-disable-next-line import/no-extraneous-dependencies,node/no-unpublished-require
const Sentry = require('@sentry/node');
const HomeyModule = require('homey');
class Log {
_capturedMessages = [];
_capturedExceptions = [];
/**
* Construct a new Log instance.
* @param {object} args
* @param {import('homey').default} args.homey - `this.homey` instance in
* your app (e.g. `App#homey`/`Driver#homey`/`Device#homey`).
*
* @param {object} [args.options] - Additional options for Sentry
*
* @example
* class MyApp extends Homey.App {
* onInit() {
* this.homeyLog = new Log({ homey: this.homey });
* }
* }
*/
constructor({ homey, options }) {
if (typeof homey === 'undefined') {
return Log._error('Error: missing `homey` constructor parameter');
}
if (!HomeyModule.env) {
return Log._error('Error: could not access `Homey.env`');
}
if (typeof HomeyModule.env.HOMEY_LOG_URL !== 'string') {
return Log._error('Error: expected `HOMEY_LOG_URL` env variable, homey-log is disabled');
}
// Check if debug mode is enabled
const disableSentry = process.env.DEBUG === '1' && !Log._isLogForced();
if (disableSentry) {
Log._log('App is running in debug mode, not enabling Sentry logging');
}
this._manifest = HomeyModule.manifest;
this._homeyVersion = homey.version;
this._managerCloud = homey.cloud;
this._homeyPlatform = homey.platform;
this._homeyPlatformVersion = homey.platformVersion;
// Init Sentry, pass enabled option to prevent sending events upstream when in debug mode
this.init(HomeyModule.env.HOMEY_LOG_URL, { ...{ enabled: !disableSentry }, ...options });
}
/**
* Init Sentry.
* @param {string} dsn The Sentry DSN
* @param {object} opts Options to be passed to the Sentry init
* @returns {Log}
* @private
*/
init(dsn, opts) {
Sentry.initWithoutDefaultIntegrations({
...{
dsn,
integrations: [
...Sentry.getDefaultIntegrationsWithoutPerformance(),
],
},
...opts,
});
this.setTags({
appId: this._manifest.id,
appVersion: this._manifest.version,
homeyVersion: this._homeyVersion,
homeyPlatform: this._homeyPlatform,
homeyPlatformVersion: this._homeyPlatformVersion,
});
// Get homey cloud id and set as tag
this._managerCloud.getHomeyId()
.then(homeyId => this.setTags({ homeyId }))
.catch(err => Log._error('Error: could not get `homeyId`', err));
Log._log(`App ${this._manifest.id} v${this._manifest.version} logging on Homey v${this._homeyVersion}...`);
return this;
}
/**
* Set `tags` that will be sent as context with every message or error. See the Sentry Node.js
* documentation: https://docs.sentry.io/platforms/javascript/guides/node/enriching-events/tags/
* @param {object} tags
* @returns {Log}
*/
setTags(tags) {
Sentry.setTags(tags);
return this;
}
/**
* Set `user` that will be sent as context with every message or error. See the Sentry Node.js
* documentation: https://docs.sentry.io/platforms/javascript/guides/node/enriching-events/identify-user/.
* @param {object} user
* @returns {Log}
*/
setUser(user) {
Sentry.setUser(user);
return this;
}
/**
* Create and send message event to Sentry. See the Sentry Node.js documentation:
* https://docs.sentry.io/platforms/javascript/guides/node/usage/
* @param {string} message - Message to be sent
* @returns {Promise<string>|undefined}
*/
async captureMessage(message) {
Log._log('captureMessage:', message);
if (this._capturedMessages.indexOf(message) > -1) {
Log._log('Prevented sending a duplicate message');
return;
}
this._capturedMessages.push(message);
// eslint-disable-next-line consistent-return
return new Promise((resolve, reject) => {
try {
resolve(Sentry.captureMessage(message));
} catch (e) {
reject(e);
}
});
}
/**
* Create and send exception event to Sentry. See the sentry Node.js documentation:
* https://docs.sentry.io/platforms/javascript/guides/node/usage/
* @param {Error} err - Error instance to be sent
* @returns {Promise<string>|undefined}
*/
async captureException(err) {
Log._log('captureException:', err);
if (this._capturedExceptions.indexOf(err) > -1) {
Log._log('Prevented sending a duplicate log');
return;
}
this._capturedExceptions.push(err);
// eslint-disable-next-line consistent-return
return new Promise((resolve, reject) => {
try {
resolve(Sentry.captureException(err));
} catch (e) {
reject(e);
}
});
}
/**
* Mimic SDK log method.
* @private
*/
static _log() {
// eslint-disable-next-line prefer-spread,prefer-rest-params,no-console
console.log.bind(null, Log._logTime(), '[homey-log]').apply(null, arguments);
}
/**
* Mimic SDK error method.
* @private
*/
static _error() {
// eslint-disable-next-line prefer-spread,prefer-rest-params,no-console
console.error.bind(null, Log._logTime(), '[homey-log]').apply(null, arguments);
}
/**
* Mimic SDK timestamp.
* @returns {string}
* @private
*/
static _logTime() {
return `\x1b[35m${(new Date()).toISOString()}\x1b[0m`;
}
/**
* Whether logging is forced.
* @returns {boolean}
* @private
*/
static _isLogForced() {
switch (HomeyModule.env.HOMEY_LOG_FORCE) {
case '1':
case 'true':
return true;
default:
return false;
}
}
}
module.exports = { Log };