Skip to content

Commit

Permalink
updated logging to use winston
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Dec 4, 2023
1 parent 15fa028 commit 178c71b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
34 changes: 26 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import hmpoLogger from 'hmpo-logger'
import logger from './src/utils/logger.js'
import express from 'express'
import cookieParser from 'cookie-parser'
import session from 'express-session'
Expand All @@ -14,12 +14,18 @@ import toErrorList from './src/filters/toErrorList.js'

const { govukMarkdown } = xGovFilters

const logger = hmpoLogger.config(config.logs).get()

const app = express()

// log access requests
app.use(hmpoLogger.middleware())
app.use((req, res, next) => {
// log the request
logger.info({
type: 'Request',
method: req.method,
endpoint: req.originalUrl,
message: `${req.method} request made to ${req.originalUrl}`
})
next()
})

// add routing for static assets
app.use('/assets', express.static('./node_modules/govuk-frontend/govuk/assets'))
Expand All @@ -28,7 +34,7 @@ app.use('/public', express.static('./public'))
// cookies and sessions (redis or elasticache should be used in a prod env)
app.use(cookieParser())
app.use(session({
secret: 'keyboard cat',
secret: 'keyboard cat', // ToDo: move to config
resave: false,
saveUninitialized: false,
cookie: { secure: false }
Expand Down Expand Up @@ -65,7 +71,13 @@ app.use('/', formWizard)

// error handler
app.use((err, req, res, next) => {
logger.error('Request error', { req, err })
logger.info({
type: 'Request error',
method: req.method,
endpoint: req.originalUrl,
message: `${req.method} request made to ${req.originalUrl} but an error occurred`,
error: err
})

// handle session expired
if (err.code === 'SESSION_TIMEOUT') {
Expand All @@ -84,12 +96,18 @@ app.use((err, req, res, next) => {
})

app.get('/health', (req, res) => {
logger.info('healthcheck')
logger.info({ type: 'healthcheck', message: 'Application health endpoint was called and the health is ok' })
res.status(200).json({ applicationHealth: 'ok' })
})

// file not found handler
app.use((req, res, next) => {
logger.info({
type: 'File not found',
method: req.method,
endpoint: req.originalUrl,
message: `${req.method} request made to ${req.originalUrl} but the file/endpoint was not found`
})
res.status(404).render('file-not-found')
})

Expand Down
3 changes: 2 additions & 1 deletion src/controllers/uploadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import MyController from './MyController.js'
import config from '../../config/index.js'

import { severityLevels } from '../utils/utils.js'
import logger from '../utils/logger.js'

const upload = multer({ dest: 'uploads/' })

Expand All @@ -32,7 +33,7 @@ class UploadController extends MyController {
req.body.datafile = req.file
req.body.validationResult = jsonResult
} catch (error) {
console.log(error)
logger.error({ type: 'Upload', message: 'Error uploading file', error })
}
}
super.post(req, res, next)
Expand Down

0 comments on commit 178c71b

Please sign in to comment.