Skip to content

Commit

Permalink
Added winston logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Grogan committed Jul 26, 2017
1 parent e9d6595 commit 8443790
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const multipart = require("parse-multipart");
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + '/dist/'
const log = require('winston');
log.level = process.env.LOG_LEVEL
import PDFToText from './src/pdf';
import FormHandler from './src/form-handler';
import Bills from './src/bills';
Expand Down Expand Up @@ -35,26 +37,26 @@ function processPayload(event){
exports.handler = async (event, context, callback) => {
try {

console.log(`Event is: ${JSON.stringify(event)}`);
log.debug(`Event is: ${JSON.stringify(event)}`);
const config = processPayload(event);
console.log('Form Response is: ' + JSON.stringify(config));
log.debug('Form Response is: ' + JSON.stringify(config));

const controller = new PDFToText(config);
const pdfResp = await controller.run();
console.log(`PDF Response is: ${pdfResp}`);
log.debug(`PDF Response is: ${pdfResp}`);

const bi = new Bills(pdfResp.text, bills_config['bills']);
const res = await bi.run();
console.log(`Bill Response is: ${JSON.stringify(res)}`);
log.debug(`Bill Response is: ${JSON.stringify(res)}`);

const db = new Dropbox(bills_config['DB_TOKEN']);
const dbResp = await db.upload(res.path, config.data)
console.log(`Dropbox Response is: ${JSON.stringify(dbResp)}`);
log.debug(`Dropbox Response is: ${JSON.stringify(dbResp)}`);

return context.succeed({ statusCode: 200, body: pdfResp.text, headers: headers });

} catch (e) {
console.log(`Application ERROR: ${e.stack}`);
log.error(`Application ERROR: ${e.stack}`);
return context.fail({ statusCode: 500, body: `Application Error: ${e}`, headers });
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"busboy": "^0.2.14",
"dropbox": "^2.5.7",
"moment": "^2.18.1",
"parse-multipart": "^1.0.4"
"parse-multipart": "^1.0.4",
"winston": "^2.3.1"
},
"devDependencies": {
"babel-cli": "^6.16.0",
Expand Down
14 changes: 8 additions & 6 deletions src/bills.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const log = require('winston');
log.level = process.env.LOG_LEVEL
import moment from 'moment';

/**
Expand All @@ -9,16 +11,16 @@ export default class Bills {
constructor(text, config){
this.text = text;
this.config = config;
console.log(`Init bills ${JSON.stringify(this.config)}`)
log.debug(`Init bills ${JSON.stringify(this.config)}`)
}


match_account(){
for(let i in this.config){
const accObj = this.config[i];
console.log(`Checking account ${JSON.stringify(accObj.bill_type)}`);
log.debug(`Checking account ${JSON.stringify(accObj.bill_type)}`);
if(this.text.includes(accObj.acc_no)){
console.log(`Account matched for: ${JSON.stringify(accObj.bill_type)}`);
log.debug(`Account matched for: ${JSON.stringify(accObj.bill_type)}`);
return accObj;
}
}
Expand All @@ -29,13 +31,13 @@ export default class Bills {
const matches = this.text.match(accObj.regex);
if(matches.length >= accObj.date_num) {
const rawDate = matches[accObj.date_num];
console.log(`Raw date for account ${accObj.bill_type} is: ${rawDate}`);
log.debug(`Raw date for account ${accObj.bill_type} is: ${rawDate}`);
const date = new moment(rawDate, accObj.date_format).toDate();
const formatted_date = moment(date).format('YY-MM');
console.log(`Date for account ${accObj.bill_type} is: ${formatted_date}`);
log.debug(`Date for account ${accObj.bill_type} is: ${formatted_date}`);
return formatted_date;
} else {
console.log(`No Date for account ${accObj.bill_type} matched!`);
log.debug(`No Date for account ${accObj.bill_type} matched!`);
throw new Error('No date matched in bill')
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/pdf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require("fs");
const os = require('os');
const path = require('path');
const log = require('winston');
log.level = process.env.LOG_LEVEL
const exec = require('child_process').exec

/**
Expand All @@ -16,7 +18,7 @@ export default class PDFToText
this.fileName = config.filename;
this.fileData = config.data;
this.filePath = path.join(os.tmpDir(), path.basename(this.fileName));
console.log(`PDFToText init with: ${this.fileName} ${this.filePath} ${JSON.stringify(this.fileData)}`);
log.debug(`PDFToText init with: ${this.fileName} ${this.filePath} ${JSON.stringify(this.fileData)}`);
}

async write_s3(filePath) {
Expand All @@ -25,7 +27,7 @@ export default class PDFToText

AWS.config.region = 'eu-west-1';
const s3 = new AWS.S3();
console.log('Going to call S3');
log.debug('Going to call S3');

var params = {
Bucket: 'gbspdf',
Expand All @@ -36,10 +38,10 @@ export default class PDFToText

s3.upload(params, function (err, res) {
if(err) {
console.log("Error in uploading file on s3 due to "+ err)
log.debug("Error in uploading file on s3 due to "+ err)
reject(err);
} else {
console.log("File successfully uploaded.")
log.debug("File successfully uploaded.")
resolve();
}
});
Expand All @@ -52,10 +54,10 @@ export default class PDFToText
*/
async write_file(filePath, fileData){
return new Promise(function(resolve, reject) {
console.log(`Will write file: ${filePath} with data: ${JSON.stringify(fileData)}`);
log.debug(`Will write file: ${filePath} with data: ${JSON.stringify(fileData)}`);
fs.writeFile(filePath, fileData, 'binary', function (err) {
if (err) { reject('Writing file failed: ' + err) }
console.log('Wrote pdf file to: ' + filePath);
log.debug('Wrote pdf file to: ' + filePath);
resolve();
});
});
Expand All @@ -68,8 +70,8 @@ export default class PDFToText
*/
async get_text(filePath) {
return new Promise(function(resolve, reject) {
console.log('Going to run pdftotext on: ' + filePath);
console.log('System Path: ' + process.env.PATH);
log.debug('Going to run pdftotext on: ' + filePath);
log.debug('System Path: ' + process.env.PATH);
const cmd = 'pdftotext '
const opts = ' - '
const allCmd = cmd + '"' + filePath + '"' + opts;
Expand All @@ -85,7 +87,7 @@ export default class PDFToText
result += data.toString();
});
child.on('close', function(code) {
console.log('stdout result: ' + result);
log.debug('stdout result: ' + result);
resolve(result);
});
});
Expand All @@ -100,7 +102,7 @@ export default class PDFToText
await this.write_file(this.filePath, this.fileData);
// await this.write_s3(this.filePath);
const pdfText = await this.get_text(this.filePath);
console.log('Returning from PDFToText: ' + pdfText);
log.debug('Returning from PDFToText: ' + pdfText);

return { "text": pdfText, "path": this.filePath };
}
Expand Down

0 comments on commit 8443790

Please sign in to comment.