From 2b31fce0458f6aa66a561d74f0827909fa2e45d9 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Thu, 7 Apr 2022 17:15:27 +0000 Subject: [PATCH] Server: Update and clean logging a bit Sharp: Switch back to sharp due to bugs in main sharp git. --- api/media/routes.mjs | 2 +- api/server.mjs | 43 +++++++++++++++++++++++++++++------------ package.json | 6 +++--- test/media/api.test.mjs | 2 +- test/server.test.mjs | 11 +++++------ 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/api/media/routes.mjs b/api/media/routes.mjs index 0b908d8..d3dd6e8 100644 --- a/api/media/routes.mjs +++ b/api/media/routes.mjs @@ -1,5 +1,5 @@ import path from 'path' -import sharp from 'sharp-lite' +import sharp from 'sharp' import fs from 'fs/promises' import { HttpError } from '../error.mjs' import * as security from './security.mjs' diff --git a/api/server.mjs b/api/server.mjs index 7c1d3be..42416f2 100644 --- a/api/server.mjs +++ b/api/server.mjs @@ -1,5 +1,5 @@ import { performance } from 'perf_hooks' -import { Flaska, QueryHandler, JsonHandler } from 'flaska' +import { Flaska, QueryHandler, JsonHandler, HttpError } from 'flaska' import TestRoutes from './test/routes.mjs' import MediaRoutes from './media/routes.mjs' @@ -20,27 +20,46 @@ app.before(function(ctx) { app.after(function(ctx) { let ended = performance.now() - ctx.__started - let logger = ctx.log.info + + let status = '' + let level = 'info' if (ctx.status >= 400) { - logger = ctx.log.warn - } - logger.apply(ctx.log, [{ - path: ctx.url, + status = ctx.status + ' ' + level = 'warn' + } + if (ctx.status >= 500) { + level = 'error' + } + + ctx.log[level]({ + duration: Math.round(ended), status: ctx.status, - ms: Math.round(ended), - }, 'Request finished']) + }, `<-- ${status}${ctx.method} ${ctx.url}`) }) app.onerror(function(err, ctx) { + if (err.status && err.status >= 400 && err.status < 500) { - ctx.log.warn(err.message) + if (err.body && err.body.request) { + ctx.log.warn({ request: err.body.request}, err.message) + } else { + ctx.log.warn(err.message) + } } else { ctx.log.error(err) } ctx.status = err.status || 500 - ctx.body = { - status: ctx.status, - message: err.message, + + if (err instanceof HttpError) { + ctx.body = err.body || { + status: ctx.status, + message: err.message, + } + } else { + ctx.body = { + status: ctx.status, + message: err.message, + } } }) diff --git a/package.json b/package.json index 8384ef8..41536dd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "storage-upload", - "version": "2.1.2", + "version": "2.1.3", "description": "", "main": "index.js", "scripts": { @@ -31,11 +31,11 @@ }, "homepage": "https://github.com/nfp-projects/storage-upload#readme", "dependencies": { - "bunyan-lite": "^1.1.1", + "bunyan-lite": "^1.2.0", "flaska": "^1.2.3", "formidable": "^1.2.2", "nconf-lite": "^2.0.0", - "sharp-lite": "^1.29.6" + "sharp": "^0.30.3" }, "devDependencies": { "eltro": "^1.2.3" diff --git a/test/media/api.test.mjs b/test/media/api.test.mjs index ca33fae..14a0cbd 100644 --- a/test/media/api.test.mjs +++ b/test/media/api.test.mjs @@ -1,4 +1,4 @@ -import sharp from 'sharp-lite' +import sharp from 'sharp' import { Eltro as t, assert} from 'eltro' import fs from 'fs/promises' import { fileURLToPath } from 'url' diff --git a/test/server.test.mjs b/test/server.test.mjs index 10135a2..aba1000 100644 --- a/test/server.test.mjs +++ b/test/server.test.mjs @@ -19,8 +19,7 @@ t.describe('Server', function() { assert.ok(data.version) assert.strictEqual(server.log.info.callCount, 1) assert.strictEqual(server.log.info.lastCall[0].status, 200) - assert.strictEqual(server.log.info.lastCall[0].path, '/') - assert.strictEqual(typeof(server.log.info.lastCall[0].ms), 'number') + assert.match(server.log.info.lastCall[1], /\<-- GET \//) }) t.test('should handle errors fine', async function() { @@ -32,9 +31,9 @@ t.describe('Server', function() { assert.ok(data.body) assert.strictEqual(data.body.status, 500) assert.match(data.body.message, /test/) - assert.strictEqual(server.log.warn.callCount, 1) - assert.strictEqual(server.log.warn.lastCall[0].status, 500) - assert.strictEqual(server.log.warn.lastCall[0].path, '/error') - assert.strictEqual(typeof(server.log.warn.lastCall[0].ms), 'number') + assert.strictEqual(server.log.error.firstCall[0].message, 'This is a test') + assert.strictEqual(server.log.error.callCount, 2) + assert.strictEqual(server.log.error.secondCall[0].status, 500) + assert.match(server.log.error.secondCall[1], /\<-- 500 GET \/error/) }) })