diff --git a/app/app.ts b/app/app.ts index b1e82923d..f8c6ea11c 100644 --- a/app/app.ts +++ b/app/app.ts @@ -1,3 +1,4 @@ +import { Express as loggingExpress, Logger } from '@hmcts/nodejs-logging'; import config from 'config'; import cookieParser from 'cookie-parser'; import csurf from 'csurf'; @@ -6,6 +7,7 @@ import express from 'express'; import helmet from 'helmet'; import webpack from 'webpack'; import webpackDevMiddleware, { Options } from 'webpack-dev-middleware'; +import { LoggerInstance } from 'winston'; import internationalization from '../locale/en.json'; import webpackDevConfig from '../webpack/webpack.dev.js'; import { configureIdam, configureLogger, configureNunjucks, configureS2S } from './app-config'; @@ -20,6 +22,7 @@ import { setupSession } from './session'; import { getUrl } from './utils/url-utils'; const uuid = require('uuid'); +const logger: LoggerInstance = Logger.getLogger('app.ts'); function createApp() { const app: express.Application = express(); @@ -148,6 +151,7 @@ function configureHelmet(app) { } })); app.use(helmet.noCache()); + app.use(loggingExpress.accessLogger()); } export { diff --git a/app/service/s2s-service.ts b/app/service/s2s-service.ts index a9b7a70dd..817a523c9 100644 --- a/app/service/s2s-service.ts +++ b/app/service/s2s-service.ts @@ -1,7 +1,9 @@ +import { Logger } from '@hmcts/nodejs-logging'; import axios from 'axios'; +import { LoggerInstance } from 'winston'; import { setupSecrets } from '../setupSecrets'; import { isJWTExpired } from '../utils/jwt-utils'; -import Logger, { getLogLabel } from '../utils/logger'; +// import Logger, { getLogLabel } from '../utils/logger'; const config = setupSecrets(); @@ -12,8 +14,9 @@ const proxyHost: string = config.get('proxy.host'); const proxyPort: number = config.get('proxy.port'); const microServiceName: string = config.get('s2s.microserviceName'); -const logger: Logger = new Logger(); -const logLabel: string = getLogLabel(__filename); +const logger: LoggerInstance = Logger.getLogger('s2s-service.ts'); +// const logger: Logger = new Logger(); +// const logLabel: string = getLogLabel(__filename); interface IS2SService { buildRequest: () => {}; @@ -24,7 +27,7 @@ interface IS2SService { export default class S2SService implements IS2SService { private static instance: S2SService; - private initialization; + private initialization: Promise; private serviceToken: string; public static getInstance(): S2SService { @@ -66,27 +69,27 @@ export default class S2SService implements IS2SService { * Note: This token is stored in memory and this token is only valid for 3 hours. */ async requestServiceToken() { - logger.trace('Attempting to request a S2S token', logLabel); + logger.info('Attempting to request a S2S token'); const request = await this.buildRequest(); let proxyConfig; if (process.env.NODE_ENV === 'development' && !s2sUrl.startsWith('http://localhost')) { proxyConfig = { proxy: { host: proxyHost, port: proxyPort } }; } let res; - for (let i = 0; i < 3; i++) { + for (let i = 0; i < 5; i++) { try { res = await axios.post(request.uri, request.body, proxyConfig); break; } catch (err) { - logger.exception(err, logLabel); + logger.error(err); i++; } } if (res && res.data) { this.serviceToken = res.data; - logger.trace('Received S2S token and stored token', logLabel); + logger.info('Received S2S token and stored token'); } else { - logger.exception('Could not retrieve S2S token', logLabel); + logger.info('Could not retrieve S2S token'); } } diff --git a/app/utils/jwt-utils.ts b/app/utils/jwt-utils.ts index 9c0233392..c218779e4 100644 --- a/app/utils/jwt-utils.ts +++ b/app/utils/jwt-utils.ts @@ -1,15 +1,18 @@ +import { Logger } from '@hmcts/nodejs-logging'; +import { LoggerInstance } from 'winston'; import { jwtRepack } from './jwt-repack'; -import Logger, { getLogLabel } from './logger'; +// import Logger, { getLogLabel } from './logger'; -const logger: Logger = new Logger(); -const logLabel: string = getLogLabel(__filename); +// const logger: Logger = new Logger(); +// const logLabel: string = getLogLabel(__filename); +const logger: LoggerInstance = Logger.getLogger('jwt-utils.ts'); export function decodeJWTToken(jwtToken: string) { let decoded; try { decoded = jwtRepack.decode(jwtToken); } catch (err) { - logger.exception(err, logLabel); + logger.error(err); throw new Error(err); } return decoded; @@ -24,11 +27,17 @@ export function isJWTExpired(jwtToken: string) { let offset = 5 * 60; // 5 minutes let isExpiredToken = false; + if (!jwtToken) { + logger.info('S2S token is null or empty.'); + return true; + } + const decoded = decodeJWTToken(jwtToken); const currentTime = Math.round(new Date().getTime() / 1000); if (decoded && decoded.exp < (currentTime + offset)) { + logger.info('S2S token has expired.'); isExpiredToken = true; } return isExpiredToken;