Skip to content

Commit

Permalink
First pass at using winston instead of logLevel or console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
nodanaonlyzuul committed Jun 13, 2017
1 parent 640efc1 commit 0bb507d
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Logs
logs
*.log
log/*.log
npm-debug.log*

local.json
Expand Down
31 changes: 28 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -13,6 +37,7 @@ var elasticsearch = require('elasticsearch')

var app = express()

app.logger = logger
app.thesaurus = config.thesaurus

require('./lib/agents')(app)
Expand Down Expand Up @@ -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)
})
})
}
Expand Down
1 change: 0 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"loglevel": "error",
"host": "discovery-api.nypltech.org",
"proto": "http",
"elasticsearch": {
Expand Down
2 changes: 0 additions & 2 deletions config/local.json.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"loglevel": "info",

"host": "localhost",
"proto": "http",

Expand Down
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
Expand Down
8 changes: 4 additions & 4 deletions lib/agents.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -415,7 +415,7 @@ module.exports = function (app) {
}
}
}
// console.log('fetching aggs: ', query, aggs, cb)


var serializationOpts = {
packed_fields: ['subject', 'contributor', 'collection']
Expand All @@ -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)
})
}
Expand Down
3 changes: 1 addition & 2 deletions lib/jsonld_serializers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '||'
Expand Down Expand Up @@ -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()
}
}
Expand Down
8 changes: 3 additions & 5 deletions lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Empty file added log/.gitkeep
Empty file.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"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",
"request": "2.53.0",
"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",
Expand Down
2 changes: 1 addition & 1 deletion routes/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 0bb507d

Please sign in to comment.