Server: Update and clean logging a bit
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

Sharp: Switch back to sharp due to bugs in main sharp git.
This commit is contained in:
Jonatan Nilsson 2022-04-07 17:15:27 +00:00
parent f4afd8bfae
commit 2b31fce045
5 changed files with 41 additions and 23 deletions

View file

@ -1,5 +1,5 @@
import path from 'path' import path from 'path'
import sharp from 'sharp-lite' import sharp from 'sharp'
import fs from 'fs/promises' import fs from 'fs/promises'
import { HttpError } from '../error.mjs' import { HttpError } from '../error.mjs'
import * as security from './security.mjs' import * as security from './security.mjs'

View file

@ -1,5 +1,5 @@
import { performance } from 'perf_hooks' 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 TestRoutes from './test/routes.mjs'
import MediaRoutes from './media/routes.mjs' import MediaRoutes from './media/routes.mjs'
@ -20,28 +20,47 @@ app.before(function(ctx) {
app.after(function(ctx) { app.after(function(ctx) {
let ended = performance.now() - ctx.__started let ended = performance.now() - ctx.__started
let logger = ctx.log.info
let status = ''
let level = 'info'
if (ctx.status >= 400) { if (ctx.status >= 400) {
logger = ctx.log.warn status = ctx.status + ' '
level = 'warn'
} }
logger.apply(ctx.log, [{ if (ctx.status >= 500) {
path: ctx.url, level = 'error'
}
ctx.log[level]({
duration: Math.round(ended),
status: ctx.status, status: ctx.status,
ms: Math.round(ended), }, `<-- ${status}${ctx.method} ${ctx.url}`)
}, 'Request finished'])
}) })
app.onerror(function(err, ctx) { app.onerror(function(err, ctx) {
if (err.status && err.status >= 400 && err.status < 500) { if (err.status && err.status >= 400 && err.status < 500) {
if (err.body && err.body.request) {
ctx.log.warn({ request: err.body.request}, err.message)
} else {
ctx.log.warn(err.message) ctx.log.warn(err.message)
}
} else { } else {
ctx.log.error(err) ctx.log.error(err)
} }
ctx.status = err.status || 500 ctx.status = err.status || 500
if (err instanceof HttpError) {
ctx.body = err.body || {
status: ctx.status,
message: err.message,
}
} else {
ctx.body = { ctx.body = {
status: ctx.status, status: ctx.status,
message: err.message, message: err.message,
} }
}
}) })
const test = new TestRoutes() const test = new TestRoutes()

View file

@ -1,6 +1,6 @@
{ {
"name": "storage-upload", "name": "storage-upload",
"version": "2.1.2", "version": "2.1.3",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -31,11 +31,11 @@
}, },
"homepage": "https://github.com/nfp-projects/storage-upload#readme", "homepage": "https://github.com/nfp-projects/storage-upload#readme",
"dependencies": { "dependencies": {
"bunyan-lite": "^1.1.1", "bunyan-lite": "^1.2.0",
"flaska": "^1.2.3", "flaska": "^1.2.3",
"formidable": "^1.2.2", "formidable": "^1.2.2",
"nconf-lite": "^2.0.0", "nconf-lite": "^2.0.0",
"sharp-lite": "^1.29.6" "sharp": "^0.30.3"
}, },
"devDependencies": { "devDependencies": {
"eltro": "^1.2.3" "eltro": "^1.2.3"

View file

@ -1,4 +1,4 @@
import sharp from 'sharp-lite' import sharp from 'sharp'
import { Eltro as t, assert} from 'eltro' import { Eltro as t, assert} from 'eltro'
import fs from 'fs/promises' import fs from 'fs/promises'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'

View file

@ -19,8 +19,7 @@ t.describe('Server', function() {
assert.ok(data.version) assert.ok(data.version)
assert.strictEqual(server.log.info.callCount, 1) assert.strictEqual(server.log.info.callCount, 1)
assert.strictEqual(server.log.info.lastCall[0].status, 200) assert.strictEqual(server.log.info.lastCall[0].status, 200)
assert.strictEqual(server.log.info.lastCall[0].path, '/') assert.match(server.log.info.lastCall[1], /\<-- GET \//)
assert.strictEqual(typeof(server.log.info.lastCall[0].ms), 'number')
}) })
t.test('should handle errors fine', async function() { t.test('should handle errors fine', async function() {
@ -32,9 +31,9 @@ t.describe('Server', function() {
assert.ok(data.body) assert.ok(data.body)
assert.strictEqual(data.body.status, 500) assert.strictEqual(data.body.status, 500)
assert.match(data.body.message, /test/) assert.match(data.body.message, /test/)
assert.strictEqual(server.log.warn.callCount, 1) assert.strictEqual(server.log.error.firstCall[0].message, 'This is a test')
assert.strictEqual(server.log.warn.lastCall[0].status, 500) assert.strictEqual(server.log.error.callCount, 2)
assert.strictEqual(server.log.warn.lastCall[0].path, '/error') assert.strictEqual(server.log.error.secondCall[0].status, 500)
assert.strictEqual(typeof(server.log.warn.lastCall[0].ms), 'number') assert.match(server.log.error.secondCall[1], /\<-- 500 GET \/error/)
}) })
}) })