diff --git a/index.js b/index.js index f5550d6..70d1545 100644 --- a/index.js +++ b/index.js @@ -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'; @@ -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 }); } }; diff --git a/package.json b/package.json index ef0d800..7f46f3e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/bills.js b/src/bills.js index a62a155..c46196b 100644 --- a/src/bills.js +++ b/src/bills.js @@ -1,3 +1,5 @@ +const log = require('winston'); +log.level = process.env.LOG_LEVEL import moment from 'moment'; /** @@ -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; } } @@ -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') } } diff --git a/src/pdf.js b/src/pdf.js index dec6095..9d749d3 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -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 /** @@ -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) { @@ -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', @@ -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(); } }); @@ -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(); }); }); @@ -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; @@ -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); }); }); @@ -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 }; }