Server: Update and clean logging a bit
continuous-integration/appveyor/branch AppVeyor build succeeded Details

Sharp: Switch back to sharp due to bugs in main sharp git.
master
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,27 +20,46 @@ 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, [{ }
path: ctx.url, if (ctx.status >= 500) {
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) {
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 { } else {
ctx.log.error(err) ctx.log.error(err)
} }
ctx.status = err.status || 500 ctx.status = err.status || 500
ctx.body = {
status: ctx.status, if (err instanceof HttpError) {
message: err.message, 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", "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/)
}) })
}) })