Commit fa27ca75 authored by Jarrod's avatar Jarrod 💬

..

parent 452bed99
# @nahanil/sails-hook-apilog
# @nahanil/sails-hook-api-log
Terse request logging for Sails.js applications.
Forked from Mike McNeil's pretty awesome [sails-hook-apianalytics](https://github.com/sailshq/sails-hook-apianalytics).
## Install
```
yarn add @nahanil/sails-hook-api-log
```
That's it! Next time you lift, and then send a request to the server, you should see stuff getting logged.
## Configuration
Optionally, you can customize this hook in a few different ways by configuring `sails.config['api-log]`.
For instance, you might create `config/api-log.js`:
```js
module.exports = {
'api-log': {
/**
* An array of route addresses to monitor.
*
* (e.g. [ 'GET /foo/bar', 'POST /foo', 'all /admin/*' ])
*
* Defaults to logging all POST, PATCH, PUT, DELETE requests, and all
* GET requests except for those that appear to be for assets
* (i.e. using "GET r|^((?![^?]*\\/[^?\\/]+\\.[^?\\/]+(\\?.*)?).)*$|")
*/
routesToLog: [
// If you want to log everything- including requests for assets, use the following:
'/*'
],
/**
* Custom log function
* Uses `console.log` by default and recieves the text string that would be logged
*/
logFn: (str) => {
console.log(`APILOG: ${str}`)
}
}
}
```
\ No newline at end of file
......@@ -29,6 +29,8 @@ module.exports = function sailsHookApiLog (sails) {
*/
defaults: {
'api-log': {
logFn: console.log,
routesToLog: [
'GET r|^((?![^?]*\\/[^?\\/]+\\.[^?\\/]+(\\?.*)?).)*$|',
// (^^Leave out assets)
......@@ -51,9 +53,6 @@ module.exports = function sailsHookApiLog (sails) {
// When response has finished...
onResponse: function (report, req /*, res */) {
try {
// The log function to use.
const logFn = console.log
// Build final, formatted output.
const formattedOutput = util.format(
'%s request: [%s] {%s} %s %s %s - %s',
......@@ -67,7 +66,7 @@ module.exports = function sailsHookApiLog (sails) {
)
// Now log it.
logFn(formattedOutput)
sails.config['api-log'].logFn(formattedOutput)
} catch (e) {
// If an unhandled error occurs, dump it to the terminal.
// (but don't let it crash the process!)
......@@ -87,6 +86,10 @@ module.exports = function sailsHookApiLog (sails) {
initialize: function initialize (next) {
sails.log.info('Initializing `api-log` hook... (requests to monitored routes will be logged!)')
if (!_.isFunction(sails.config['api-log'].logFn)) {
throw new Error('Invalid configuration: `@nahanil/sails-hookapi-analytics` recieved non-function as `logFn` parameter')
}
// Listen for when the router in Sails says it's time to bind "before" shadow routes:
sails.on('router:before', function routerBefore () {
_.each(sails.config['api-log'].routesToLog, function iterator (routeAddress) {
......
......@@ -23,6 +23,9 @@
],
"author": "Jarrod Linahan <jarrod@linahan.id.au>",
"license": "MIT",
"dependencies": {
"@sailshq/lodash": "^3.10.2"
},
"devDependencies": {
"standard": "^14.3.1"
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment