Skip to content

Commit

Permalink
[#17] Adding a logging framework instead of console.log
Browse files Browse the repository at this point in the history
Added pino for logging and removed unnecessay logs
  • Loading branch information
howardgao authored and gaohoward committed Oct 24, 2024
1 parent e9b2cb6 commit b234a8b
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 76 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ SECRET_ACCESS_TOKEN=1e13d44f998dee277deae621a9012cf300b94c91

# to trust jolokia certs
NODE_TLS_REJECT_UNAUTHORIZED='0'

# logging
LOG_LEVEL='info'
ENABLE_REQUEST_LOG='false'
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,10 @@
"yaml": "^2.4.5"
},
"readme": "README.md",
"_id": "[email protected]"
"_id": "[email protected]",
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
"dependencies": {
"express-pino-logger": "^7.0.0",
"pino": "^9.5.0"
}
}
1 change: 0 additions & 1 deletion src/api/apiutil/artemis_jolokia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ export class ArtemisJolokia {
this.brokerName = this.brokerName.replace(/"/g, '');
return true;
}
console.log('User is not valid', this.username);
return false;
};

Expand Down
73 changes: 37 additions & 36 deletions src/api/controllers/api_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
JolokiaReadResponse,
} from '../apiutil/artemis_jolokia';
import { API_SUMMARY } from '../../utils/server';
import { logger } from '../../utils/logger';

const BROKER = 'broker';
const ADDRESS = 'address';
Expand Down Expand Up @@ -40,10 +41,10 @@ export const getBrokers = (_: express.Request, res: express.Response): void => {
);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -76,10 +77,10 @@ export const getClusterConnections = (
);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand All @@ -105,11 +106,11 @@ export const readClusterConnectionAttributes = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -141,11 +142,11 @@ export const getClusterConnectionDetails = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -187,11 +188,11 @@ export const execClusterConnectionOperation = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -224,10 +225,10 @@ export const getAcceptors = (
);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand All @@ -253,11 +254,11 @@ export const readAcceptorAttributes = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand All @@ -279,10 +280,10 @@ export const getBrokerComponents = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -314,10 +315,10 @@ export const getAddresses = (
);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand All @@ -343,11 +344,11 @@ export const readAddressAttributes = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -392,10 +393,10 @@ export const getQueues = (
);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -425,11 +426,11 @@ export const readQueueAttributes = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -457,11 +458,11 @@ export const getBrokerDetails = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -513,11 +514,11 @@ export const execBrokerOperation = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand All @@ -539,11 +540,11 @@ export const readBrokerAttributes = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -575,11 +576,11 @@ export const getAddressDetails = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -611,11 +612,11 @@ export const getAcceptorDetails = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down Expand Up @@ -653,11 +654,11 @@ export const getQueueDetails = (
res.json(result);
})
.catch((error: any) => {
console.log(error);
logger.error(error);
res.status(500).json({ status: 'error', message: 'server error' });
});
} catch (err) {
console.log(err);
logger.error(err);
res.status(500).json({
status: 'error',
message: 'server error: ' + JSON.stringify(err),
Expand Down
25 changes: 5 additions & 20 deletions src/api/controllers/security.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as express from 'express';
import jwt from 'jsonwebtoken';
import { ArtemisJolokia } from '../apiutil/artemis_jolokia';
import { logger } from '../../utils/logger';

const securityStore = new Map<string, ArtemisJolokia>();

Expand All @@ -22,7 +23,7 @@ const validateHostName = (host: string) => {
let validHost: string = host;
if (process.env.NODE_ENV === 'production') {
if (!host.includes('wconsj')) {
console.log('invalid host', host);
logger.warn('invalid host', host);
return null;
} else {
validHost = host;
Expand All @@ -35,7 +36,7 @@ const validateScheme = (scheme: string) => {
let validScheme: string = scheme;
if (process.env.NODE_ENV === 'production') {
if (scheme !== 'http' && scheme !== 'https') {
console.log('invalid scheme', scheme);
logger.warn('invalid scheme', scheme);
return null;
} else {
validScheme = scheme;
Expand All @@ -50,7 +51,7 @@ const validatePort = (port: string) => {
if (num >= 1 && num <= 65535 && port === num.toString()) {
validPort = port;
} else {
console.log('invalid port', port);
logger.warn('invalid port', port);
return null;
}
return validPort;
Expand All @@ -62,7 +63,6 @@ export const login = (req: express.Request, res: express.Response) => {
req.body;

const validHost = validateHostName(jolokiaHost);
console.log('validHost is ', validHost);
if (!validHost) {
res.status(401).json({
status: 'failed',
Expand All @@ -87,15 +87,6 @@ export const login = (req: express.Request, res: express.Response) => {
return;
}

console.log(
'login',
'brokerKey',
brokerName,
'user',
userName,
'host',
validHost,
);
const jolokia = new ArtemisJolokia(
userName,
password,
Expand All @@ -109,7 +100,6 @@ export const login = (req: express.Request, res: express.Response) => {
.validateUser()
.then((result) => {
if (result) {
console.log('user is valid');
const token = generateJWTToken(brokerName);
securityStore.set(brokerName, jolokia);

Expand All @@ -127,7 +117,7 @@ export const login = (req: express.Request, res: express.Response) => {
res.end();
})
.catch((e) => {
console.log('got exception while login', e);
logger.error('got exception while login', e);
res.status(500).json({
status: 'failed',
message: 'Internal error',
Expand Down Expand Up @@ -155,25 +145,20 @@ export const VerifyLogin = async (
res: express.Response,
next: any,
) => {
console.log('verify login for request:', req.path);
try {
if (ignoreAuth(req.path)) {
console.log('ignore path', req.path);
next();
} else {
const authHeader = req.headers['jolokia-session-id'] as string;

if (!authHeader) {
console.log('no auth');
res.sendStatus(401);
} else {
console.log('verifying header', authHeader);
jwt.verify(
authHeader,
getSecretToken(),
async (err: any, decoded: any) => {
if (err) {
console.log('verify failed', err);
res.status(401).json({
status: 'failed',
message: 'This session has expired. Please login again',
Expand Down
Loading

0 comments on commit b234a8b

Please sign in to comment.