From 57d96831358a6df5dd4f92a1d33715cbc3a575e0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 25 Jun 2020 19:41:42 +0300 Subject: feat: enable logging hook for external requests --- hooks/logging.ts | 13 +++++++++++++ services/index.ts | 4 ++++ 2 files changed, 17 insertions(+) create mode 100644 hooks/logging.ts diff --git a/hooks/logging.ts b/hooks/logging.ts new file mode 100644 index 0000000..30353ae --- /dev/null +++ b/hooks/logging.ts @@ -0,0 +1,13 @@ +import { HookContext } from '@feathersjs/feathers'; + +export default async (context: HookContext): Promise => { + if (context.params.provider) { + const { method, path, id, params: { user: { username }} } = context; + const timestamp = new Date().toLocaleString('default', { timeStyle: 'medium', dateStyle: 'short' }); + const message = `${method.toUpperCase()}: /${path}/${id || ''}`; + + console.log(`[${timestamp}] ${message} ${username || ''}`); + } + return context; +}; + diff --git a/services/index.ts b/services/index.ts index ce20901..8d77429 100644 --- a/services/index.ts +++ b/services/index.ts @@ -6,6 +6,7 @@ import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; import tryAuthenticate from '../hooks/tryAuthenticate'; +import logging from '../hooks/logging'; export default (app: Application): void => { app.configure(Auth); @@ -17,6 +18,9 @@ export default (app: Application): void => { app.hooks({ before: { all: tryAuthenticate + }, + after: { + all: logging } }); }; -- cgit v1.2.3