From 0bb507de74cfc06c7f1b1abec7bb7443ceed7b62 Mon Sep 17 00:00:00 2001 From: nodanaonlyzuul Date: Tue, 13 Jun 2017 11:57:04 -0400 Subject: [PATCH] First pass at using winston instead of logLevel or console.log --- .gitignore | 3 +-- app.js | 31 ++++++++++++++++++++++++++++--- config/default.json | 1 - config/local.json.example | 2 -- index.js | 4 +--- lib/agents.js | 8 ++++---- lib/jsonld_serializers.js | 3 +-- lib/resources.js | 8 +++----- log/.gitkeep | 0 package.json | 4 ++-- routes/resources.js | 2 +- 11 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 log/.gitkeep diff --git a/.gitignore b/.gitignore index 1f2a21aa..1e964257 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Logs -logs -*.log +log/*.log npm-debug.log* local.json diff --git a/app.js b/app.js index c5103643..3947fbaf 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,31 @@ const config = require('config') -const log = require('loglevel') -log.setLevel(process.env.LOGLEVEL || config.get('loglevel') || 'error') +const winston = require('winston') +winston.emitErrs = false + +const logLevel = (process.env.NODE_ENV === 'production') ? 'info' : 'debug' + +const logger = new winston.Logger({ + transports: [ + new winston.transports.File({ + level: logLevel, + filename: './log/discovery-api.log', + handleExceptions: true, + json: true, + maxsize: 5242880, //5MB + maxFiles: 5, + colorize: false + }), + new winston.transports.Console({ + level: logLevel, + handleExceptions: true, + json: true, + stringify: true, + colorize: true + }) + ], + exitOnError: false +}) const swaggerDocs = require('./swagger.v0.1.1.json') const pjson = require('./package.json') @@ -13,6 +37,7 @@ var elasticsearch = require('elasticsearch') var app = express() +app.logger = logger app.thesaurus = config.thesaurus require('./lib/agents')(app) @@ -53,7 +78,7 @@ if (process.env.LOCAL) { require('./lib/globals')(app).then((app) => { app.listen(port, function () { - console.log('Server started on port ' + port) + app.logger.info('Server started on port ' + port) }) }) } diff --git a/config/default.json b/config/default.json index 4994ac58..be965715 100644 --- a/config/default.json +++ b/config/default.json @@ -1,5 +1,4 @@ { - "loglevel": "error", "host": "discovery-api.nypltech.org", "proto": "http", "elasticsearch": { diff --git a/config/local.json.example b/config/local.json.example index 33e80ea1..4d0eb0be 100644 --- a/config/local.json.example +++ b/config/local.json.example @@ -1,6 +1,4 @@ { - "loglevel": "info", - "host": "localhost", "proto": "http", diff --git a/index.js b/index.js index e88d5185..0825b7a8 100644 --- a/index.js +++ b/index.js @@ -8,9 +8,7 @@ var log = null const config = require('config') exports.handler = (event, context, callback) => { - log = require('loglevel') - log.setLevel(process.env.LOGLEVEL || config.get('loglevel') || 'error') - + if (Object.keys(event).length === 0 && event.constructor === Object) { return callback('No event was received.') } diff --git a/lib/agents.js b/lib/agents.js index f390cfd4..5ad88ac5 100644 --- a/lib/agents.js +++ b/lib/agents.js @@ -23,7 +23,7 @@ module.exports = function (app) { }).then((resp) => { cb(serializers.AgentSerializer.serialize(resp._source, {root: true, expandContext: params.expandContext === 'true'})) }, function (err) { - console.trace(err.message) + app.logger.error(err.message) cb(false) }) } @@ -289,7 +289,7 @@ module.exports = function (app) { size: params.per_page } }).then((resp) => cb(serializers.AgentResultsSerializer.serialize(resp)), function (err) { - console.trace(err.message) + app.logger.error(err.message) cb(false) }) } @@ -415,7 +415,7 @@ module.exports = function (app) { } } } - // console.log('fetching aggs: ', query, aggs, cb) + var serializationOpts = { packed_fields: ['subject', 'contributor', 'collection'] @@ -436,7 +436,7 @@ module.exports = function (app) { }).then((resp) => { cb(serializers.AggregationsSerializer.serialize(resp, serializationOpts)) }, function (err) { - console.trace(err.message) + app.logger.error(err.message) cb(false) }) } diff --git a/lib/jsonld_serializers.js b/lib/jsonld_serializers.js index ebd26f09..7c6b180d 100644 --- a/lib/jsonld_serializers.js +++ b/lib/jsonld_serializers.js @@ -4,7 +4,6 @@ var R = require('ramda') var lexicon = require('nypl-registry-utils-lexicon') var util = require('./util.js') -const log = require('loglevel') const config = require('config') const PACK_DELIM = '||' @@ -219,7 +218,7 @@ class ResourceSerializer extends JsonLdItemSerializer { } static serialize (resp, options) { - log.debug('ResourceSerializer#serialize', resp) + console.log('ResourceSerializer#serialize', resp) return (new ResourceSerializer(resp, options)).format() } } diff --git a/lib/resources.js b/lib/resources.js index 2f0b5dd6..91702478 100644 --- a/lib/resources.js +++ b/lib/resources.js @@ -6,8 +6,6 @@ var AggregationSerializer = require('./jsonld_serializers.js').AggregationSerial var util = require('../lib/util') var config = require('config') -const log = require('loglevel') - const RESOURCES_INDEX = config.get('elasticsearch').indexes.resources // Configures aggregations: @@ -126,7 +124,7 @@ module.exports = function (app) { } } - log.debug('Resources#search', body) + app.logger.debug('Resources#search', body) return app.esClient.search({ index: RESOURCES_INDEX, body: body @@ -138,7 +136,7 @@ module.exports = function (app) { params = parseSearchParams(params) var body = buildElasticBody(params) - log.error('Resources#search', RESOURCES_INDEX, JSON.stringify(body, null, 2)) + app.logger.debug('Resources#search', RESOURCES_INDEX, body) return app.esClient.search({ index: RESOURCES_INDEX, body: body @@ -187,7 +185,7 @@ module.exports = function (app) { packed_fields: ['subject', 'contributor', 'collection', 'location', 'materialType', 'locationBuilding', 'language', 'carrierType', 'mediaType', 'issuance', 'status', 'owner'] } - log.debug('Resources#aggregation:', JSON.stringify(body, null, 2)) + app.logger.debug('Resources#aggregation:', JSON.stringify(body, null, 2)) return app.esClient.search({ index: RESOURCES_INDEX, body: body diff --git a/log/.gitkeep b/log/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/package.json b/package.json index ffb2233d..0555cc30 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,6 @@ "fast-csv": "^2.3.0", "jsonapi-serializer": "^2.0.0", "jsonld": "^0.4.5", - "loglevel": "^1.4.1", "md5": "^2.0.0", "nypl-registry-utils-lexicon": "nypl-registry/utils-lexicon", "ramda": "^0.21.0", @@ -19,7 +18,8 @@ "request-promise": "^4.1.1", "serve-static": "^1.10.0", "simple-node-logger": "^0.92.21", - "string_score": "^0.1.22" + "string_score": "^0.1.22", + "winston": "2.3.1" }, "devDependencies": { "lambda-tester": "^2.8.1", diff --git a/routes/resources.js b/routes/resources.js index af272fd2..686a1608 100644 --- a/routes/resources.js +++ b/routes/resources.js @@ -27,7 +27,7 @@ module.exports = function (app) { } const handleError = (res, error, params) => { - console.error('Resources#handleError:', error) + app.logger.error('Resources#handleError:', error) res.status(500).send({ error: error.message ? error.message : error }) return false }