col_pkg_log_util
v1.1.1
Published
Logger for AWS cloudwatch.
Downloads
146
Readme
to test package:
- run "npm link"
- go to external project to test from
- run "npm link col_pkg_log_util"
- external project must have a .env file with:
AWS_REGION=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
LOGGROUPNAME=
CRITICALLOGSTREAMNAME=
ERRORLOGSTREAMNAME=
WARNINGLOGSTREAMNAME=
INFOLOGSTREAMNAME=
DEBUGLOGSTREAMNAME=
DEBUGMODE=
** debug mode is false by default
- import function logSend from module col_pkg_log_util
const { logSend } = require('col_pkg_log_util');
- log event:
await logSend({log level}, {logDetails});
log levels:
- critical
- error
- warning
- info
- debug
logDetails:
- json object with details to log
to test as middleware:
npm install col_pkg_log_util
add file loggingMiddleware.js
const { logSend } = require('col_pkg_log_util');
const loggingMiddleware = (req, res, next) => {
const start = Date.now();
const originalSend = res.send;
res.send = function (body) {
res.responseBody = body;
originalSend.call(this, body);
};
res.on('finish', () => {
const duration = Date.now() - start;
// const logData = {
// method: req.method, // HTTP method (GET, POST, etc.)
// url: req.originalUrl, // Full URL requested
// headers: req.headers, // Headers sent by the client
// params: req.params, // Route parameters
// query: req.query, // Query string parameters
// body: req.body, // Body of the request (e.g., POST/PUT data)
// cookies: req.cookies, // Cookies sent by the client (if cookie-parser is used)
// ip: req.ip, // IP address of the client
// protocol: req.protocol, // Protocol used (http/https)
// hostname: req.hostname, // Hostname of the request
// userAgent: req.get('User-Agent'), // User-Agent header for the client
// responseBody: res.body, // Body of the response
// statusCode: res.statusCode, // Status code of the response
// statusMessage: res.statusMessage, // Status message (e.g., "OK")
// responseTime: res.getHeaders()['x-response-time'], // Response time (if added by middleware)
// };
const logData = {
method: req.method,
url: req.originalUrl,
headers: {
userAgent: req.headers['user-agent'],
referer: req.headers.referer
},
params: req.params,
body: req.body,
statusCode: res.statusCode,
responseBody: (() => {
try {
return JSON.parse(res.responseBody);
} catch (e) {
return res.responseBody;
}
})(),
duration: `${duration}ms`,
authInfo: req.user || {}, // Auth info if available
};
// Conditionals for appropriate log stream
if (res.statusCode >= 500) {
logSend('error', logData);
} else if (res.statusCode >= 400 && res.statusCode < 500) {
logSend('warning', logData);
} else {
logSend('info', logData);
}
// To add:
// logSend('debug', logData);
// logSend('critical', logData);
});
next();
};
module.exports = loggingMiddleware;
in server.js add lines
const loggingMiddleware = require('./middleware/loggingMiddleware');
and
app.use(loggingMiddleware);
TODO
- creating event log settings in env file (retention time...etc)
- set pre-set log details for different levels (if needed)