Skip to content

Commit 75fcf45

Browse files
authored
Merge pull request #29 from plotly/logging
Improve logging
2 parents 4c59c67 + cbb4e13 commit 75fcf45

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

bin/plotly-export-server_electron.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const pkg = require('../package.json')
44

55
const argv = getServerArgs()
66
const SHOW_LOGS = !argv.quiet
7+
const DEBUG = argv.debug
78

89
if (argv.version) {
910
console.log(pkg.version)
@@ -48,36 +49,60 @@ const opts = {
4849
launch()
4950

5051
app.on('after-connect', (info) => {
51-
if (SHOW_LOGS) {
52+
if (DEBUG) {
5253
console.log(`Listening on port ${info.port} after a ${info.startupTime} ms bootup`)
5354
console.log(`Open routes: ${info.openRoutes.join(' ')}`)
5455
}
5556
})
5657

5758
app.on('after-export', (info) => {
5859
if (SHOW_LOGS) {
59-
console.log(`after-export, fig: ${info.fid} in ${info.processingTime} ms`)
60+
console.log(JSON.stringify({
61+
severity: 'INFO',
62+
httpRequest: {
63+
requestMethod: info.method
64+
},
65+
labels: {
66+
fid: info.fid,
67+
head: info.head,
68+
processingTime: info.processingTime
69+
}
70+
}))
6071
}
6172
})
6273

6374
app.on('export-error', (info) => {
6475
if (SHOW_LOGS) {
65-
console.log(`export error ${info.code} - ${info.msg}`)
76+
console.log(JSON.stringify({
77+
severity: 'ERROR',
78+
textPayload: `${info.code} - ${info.msg}`,
79+
labels: {
80+
fid: info.fid,
81+
head: info.head
82+
}
83+
}))
6684
}
6785
})
6886

6987
process.on('uncaughtException', (err) => {
70-
console.warn(err)
88+
if (SHOW_LOGS) {
89+
console.log(JSON.stringify({
90+
severity: 'ERROR',
91+
textPayload: err.toString()
92+
}))
93+
}
7194

7295
if (argv.keepAlive) {
73-
if (SHOW_LOGS) {
96+
if (DEBUG) {
7497
console.log('... relaunching')
7598
}
7699
launch()
77100
}
78101
})
79102

80103
function launch () {
81-
console.log(`Spinning up server with pid: ${process.pid}`)
104+
if (DEBUG) {
105+
console.log(`Spinning up server with pid: ${process.pid}`)
106+
}
82107
app = plotlyExporter.serve(opts)
83108
}

src/app/server/create-server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function createServer (app, BrowserWindow, ipcMain, opts) {
3131
// and is emitted on 'export-error' and 'after-export'
3232
const fullInfo = {
3333
port: opts.port,
34+
method: req.method,
3435
id: id
3536
}
3637

test/unit/server_test.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ tap.test('createServer:', t => {
148148
const app = args[0]
149149

150150
app.on('after-export', (info) => {
151-
t.equal(Object.keys(info).length, 15, '# of keys')
151+
t.equal(Object.keys(info).length, 16, '# of keys')
152+
t.equal(info.method, 'POST', 'method')
152153
t.type(info.id, 'string', 'id')
153154
t.equal(info.port, args[3].port, 'port')
154155
t.equal(info.pending, 0, 'pending')
@@ -179,7 +180,7 @@ tap.test('createServer:', t => {
179180

180181
t.test('on invalid route', t => {
181182
_boot([], (args) => {
182-
wrapApp(t, args, [4, 404, 'invalid route'])
183+
wrapApp(t, args, [5, 404, 'invalid route'])
183184

184185
_post('not-gonna-work', body0(), (err, res, body) => {
185186
if (err) t.fail()
@@ -193,7 +194,7 @@ tap.test('createServer:', t => {
193194
const opts = opts0()
194195

195196
_boot([false, false, false, opts], (args) => {
196-
wrapApp(t, args, [4, 504, 'window for given route does not exist'])
197+
wrapApp(t, args, [5, 504, 'window for given route does not exist'])
197198

198199
_post('', body0(), (err, res, body) => {
199200
if (err) t.fail()
@@ -208,7 +209,7 @@ tap.test('createServer:', t => {
208209
BrowserWindow.getAllWindows = () => ({length: Infinity})
209210

210211
_boot([false, BrowserWindow], (args) => {
211-
wrapApp(t, args, [4, 402, 'too many windows are opened'])
212+
wrapApp(t, args, [5, 402, 'too many windows are opened'])
212213

213214
_post('', body0(), (err, res, body) => {
214215
if (err) t.fail()
@@ -220,7 +221,7 @@ tap.test('createServer:', t => {
220221

221222
t.test('on json parse errors', t => {
222223
_boot([], (args) => {
223-
wrapApp(t, args, [4, 422, 'json parse error'])
224+
wrapApp(t, args, [5, 422, 'json parse error'])
224225

225226
_post('', body0() + '/', (err, res, body) => {
226227
if (err) t.fail()
@@ -236,7 +237,7 @@ tap.test('createServer:', t => {
236237
sinon.stub(_module, 'parse').yields(432)
237238

238239
_boot([false, false, false, opts], (args) => {
239-
wrapApp(t, args, [4, 432, ''])
240+
wrapApp(t, args, [5, 432, ''])
240241

241242
_post('', body0(), (err, res, body) => {
242243
if (err) t.fail()
@@ -251,7 +252,7 @@ tap.test('createServer:', t => {
251252
const opts = mockWebContents(opts0(), 467, {msg: 'error msg'})
252253

253254
_boot([false, false, false, opts], (args) => {
254-
wrapApp(t, args, [11, 467, 'error msg'])
255+
wrapApp(t, args, [12, 467, 'error msg'])
255256

256257
_post('', body0(), (err, res, body) => {
257258
if (err) t.fail()
@@ -267,7 +268,7 @@ tap.test('createServer:', t => {
267268
sinon.stub(_module, 'convert').yields(483)
268269

269270
_boot([false, false, false, opts], (args) => {
270-
wrapApp(t, args, [12, 483, ''])
271+
wrapApp(t, args, [13, 483, ''])
271272

272273
_post('', body0(), (err, res, body) => {
273274
if (err) t.fail()

0 commit comments

Comments
 (0)