diff --git a/app.js b/app.js index 29897d73..e0a8591f 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,10 @@ var app = require('express')(); +var peliasConfig = require( 'pelias-config' ).generate().api; -app.use( require( './middleware/access_log' ) ); +if( peliasConfig.accessLog ){ + app.use( require( './middleware/access_log' )( peliasConfig.accessLog ) ); +} /** ----------------------- middleware ----------------------- **/ diff --git a/middleware/access_log.js b/middleware/access_log.js index ff79eb00..812a8849 100644 --- a/middleware/access_log.js +++ b/middleware/access_log.js @@ -1,14 +1,20 @@ /** - * Print out access logs. + * Create a middleware that prints access logs via pelias-logger. */ 'use strict'; -var peliasConfig = require( 'pelias-config' ).generate().api; var morgan = require( 'morgan' ); +var through = require( 'through2' ); +var peliasLogger = require( 'pelias-logger' ).get( 'api' ); -module.exports = peliasConfig.accessLog ? - morgan( peliasConfig.accessLog ) : - function noop(req, res, next){ - next(); - }; +function createAccessLogger( logFormat ){ + return morgan( logFormat, { + stream: through( function write( ln, _, next ){ + peliasLogger.info( ln.toString().trim() ); + next(); + }) + }); +} + +module.exports = createAccessLogger; diff --git a/package.json b/package.json index 67a809a1..ece2b7e2 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "markdown": "0.5.0", "pelias-esclient": "0.0.25", "pelias-logger": "^0.0.8", + "through2": "0.6.5", "morgan": "1.5.2", "pelias-config": "^0.1.4", "pelias-suggester-pipeline": "2.0.2"