Created
August 21, 2014 17:02
-
-
Save 58bits/d4cb3c0379011aa547f0 to your computer and use it in GitHub Desktop.
hapi plugin logging strategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _ = require('lodash'); | |
var Routes = require('./routes'); | |
var logger = { | |
maxLogSize: 1024 * 1024, //1MB | |
subscribers: { | |
'console': ['ops', 'log', 'request', 'error'], | |
'api/storage/v1/logs/api-access': ['request'], | |
'api/storage/v1/logs/api-log': { tags: ['api'], events: ['log'] } | |
} | |
}; | |
exports.register = function (plugin, options, next) { | |
var Hapi = plugin.hapi; | |
plugin.bind({ | |
config: plugin.app.config | |
}); | |
plugin.events.on('request', function (request, event, tags) { | |
var keys = Object.keys(tags); | |
if (_.intersection(plugin.app.config.server.api.log, keys).length > 0) { | |
plugin.log(keys, event); | |
} | |
}); | |
plugin.config.route.prefix = "/api/v1"; | |
plugin.register({ | |
plugin: require('good'), | |
options: logger | |
}, function (err) { | |
if (err) { | |
console.log(err); | |
} else { | |
plugin.route(Routes.endpoints); | |
plugin.log(['api', 'info'], 'API Server running on ' + plugin.app.config.server.api.uri); | |
next(); | |
} | |
}); | |
}; | |
exports.register.attributes = { | |
name: 'myapp-api', | |
version: '1.0.0' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
product: { | |
name: 'MyApp' | |
}, | |
server: { | |
web: { | |
host: 'localhost', | |
port: 8000, | |
log: ["info", "trace", "debug", "error"] | |
}, | |
api: { | |
host: 'localhost', | |
port: 8001, | |
log: ["info", "trace", "debug", "error"] | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Load modules | |
var Path = require('path'); | |
var Hapi = require('hapi'); | |
var Config = require('./config'); | |
// Declare internals | |
var internals = {}; | |
Config.server.web.uri = (Config.server.web.tls ? 'https://' : 'http://') + Config.server.web.host + ':' + Config.server.web.port; | |
Config.server.api.uri = (Config.server.api.tls ? 'https://' : 'http://') + Config.server.api.host + ':' + Config.server.api.port; | |
var manifest = { | |
pack: { | |
app: { | |
config: Config | |
} | |
}, | |
servers: [ | |
{ | |
host: Config.server.api.host, | |
port: Config.server.api.port, | |
options: { | |
labels: 'api', | |
cors: true | |
} | |
}, | |
{ | |
host: Config.server.web.host, | |
port: Config.server.web.port, | |
options: { | |
labels: 'web', | |
state: { | |
cookies: { | |
clearInvalid: true | |
} | |
} | |
} | |
} | |
], | |
plugins: { | |
'./api': [{ select: 'api' }], | |
'./web': [{ select: 'web' }] | |
} | |
}; | |
Hapi.Pack.compose(manifest, { relativeTo: __dirname }, function (err, pack) { | |
pack.start(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment