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 sharp from 'sharp-lite'
import sharp from 'sharp'
import fs from 'fs/promises'
import { HttpError } from '../error.mjs'
import * as security from './security.mjs'

View file

@ -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
status = ctx.status + ' '
level = 'warn'
}
logger.apply(ctx.log, [{
path: ctx.url,
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,
}
}
})

View file

@ -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"

View file

@ -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'

View file

@ -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/)
})
})