From 59eb0191bfdf781874d0de29f13947c436fc8c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Syn=C3=A1=C4=8Dek?= Date: Tue, 14 Jul 2020 22:07:42 +0200 Subject: [PATCH] make logger optional so code can be re-used for CLI interface --- lib/cli.js | 9 +++------ lib/services/crawler.js | 13 ++++++++----- lib/services/dom-parser.js | 13 ++++++++----- lib/services/ldjson-parser.js | 13 ++++++++----- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 7779283..420746c 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -5,12 +5,9 @@ const parseUsingDOM = require('./services/dom-parser') const { createParserError, createAppLogger } = require('./utils') const parseHTMLString = (html, { verbose }) => { - const dev = process.env.NODE_ENV === 'development' - const logger = createAppLogger({ dev }) - - if (!verbose) { - logger.transports.forEach((transport) => (transport.silent = true)) - } + const logger = verbose ? + createAppLogger({ dev: true }) : + null try { const LDJSONEventData = parseUsingLDJSONData(html, { logger }) diff --git a/lib/services/crawler.js b/lib/services/crawler.js index e6f5a24..30b8156 100644 --- a/lib/services/crawler.js +++ b/lib/services/crawler.js @@ -1,11 +1,14 @@ const request = require('request') const crawl = async (url, { logger }) => { - logger.log({ - message: `Crawl started for url: ${url}`, - level: 'info', - service: 'parser', - }) + if (logger) { + logger.log({ + message: `Crawl started for url: ${url}`, + level: 'info', + service: 'parser', + }) + } + return new Promise((resolve, reject) => { request({ url, diff --git a/lib/services/dom-parser.js b/lib/services/dom-parser.js index 9fb95ab..4793e86 100644 --- a/lib/services/dom-parser.js +++ b/lib/services/dom-parser.js @@ -37,11 +37,14 @@ const createLocationData = (streetText, areaText) => { // NOTE: Fallback parser // Attempt reading event data directly from DOM const parseUsingDOM = (html, url, { logger }) => { - logger.log({ - message: 'Using fallback DOM parser', - level: 'info', - service: 'parser', - }) + if (logger) { + logger.log({ + message: 'Using fallback DOM parser', + level: 'info', + service: 'parser', + }) + } + const $ = cheerio.load(html) const title = $('title').text() diff --git a/lib/services/ldjson-parser.js b/lib/services/ldjson-parser.js index 05776e7..219f3b0 100644 --- a/lib/services/ldjson-parser.js +++ b/lib/services/ldjson-parser.js @@ -38,11 +38,14 @@ const parseEventData = (eventData) => { } const parseUsingLDJSONData = (html, { logger }) => { - logger.log({ - message: 'Parsing using LDJSON parser', - level: 'info', - service: 'parser', - }) + if (logger) { + logger.log({ + message: 'Parsing using LDJSON parser', + level: 'info', + service: 'parser', + }) + } + try { // NOTE: Mobile web should have serialized // event info in one of the script tags