Browse Source

Emit access-logs via pelias-logger.

(app, middleware/access_log).js
	-Move the logic that determines whether access logging is
	necessary to app.js, since that allows us to maintain the style
	of `app.use(require(/* desired middleware module */))` inside
	`app.js` without returning a "noop" middleware from
	`middleware/access_log.js` in the case that it's unnecessary.
pull/123/head
Severyn Kozak 10 years ago
parent
commit
157734b8d6
  1. 5
      app.js
  2. 20
      middleware/access_log.js
  3. 1
      package.json

5
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 ----------------------- **/

20
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;

1
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"

Loading…
Cancel
Save