|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright CERN and copyright holders of ALICE O2. This software is |
| 4 | + * distributed under the terms of the GNU General Public License v3 (GPL |
| 5 | + * Version 3), copied verbatim in the file "COPYING". |
| 6 | + * |
| 7 | + * See http://alice-o2.web.cern.ch/license for full licensing information. |
| 8 | + * |
| 9 | + * In applying this license CERN does not waive the privileges and immunities |
| 10 | + * granted to it by virtue of its status as an Intergovernmental Organization |
| 11 | + * or submit itself to any jurisdiction. |
| 12 | + */ |
| 13 | +const { LogManager, LogLevel } = require('@aliceo2/web-ui'); |
| 14 | + |
| 15 | +/** |
| 16 | + * Logger dedicated to filter-related endpoint access events. |
| 17 | + */ |
| 18 | +class FilterLogger { |
| 19 | + /** |
| 20 | + * Creates an instance of FilterLogger. |
| 21 | + */ |
| 22 | + constructor() { |
| 23 | + LogManager.configure({ infologger: true }); |
| 24 | + this._logger = LogManager.getLogger('FILTERING'); |
| 25 | + this._logLevel = LogLevel.OPERATIONS; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Logs an informational message about endpoint access and applied filters. |
| 30 | + * |
| 31 | + * @param {object} request the request received at any given endpoint. |
| 32 | + * @param {string} endpoint the endpoint that was accessed. |
| 33 | + * @param {string|number} id identifier of the user accessing the endpoint. |
| 34 | + * @param {Object} [filters={}] filters applied to the request. |
| 35 | + * @returns {void} |
| 36 | + */ |
| 37 | + infoMessage({ path, session: { id }, query = {} }) { |
| 38 | + const filters = query.filters ?? {}; |
| 39 | + |
| 40 | + let message = `Endpoint ${path} was accessed by user ${id} `; |
| 41 | + |
| 42 | + if (!Object.keys(filters).length) { |
| 43 | + message += 'without filters'; |
| 44 | + } else { |
| 45 | + message += 'with the following filters:\n'; |
| 46 | + message += JSON.stringify(filters); |
| 47 | + } |
| 48 | + |
| 49 | + this._logger.infoMessage(message, { level: this._logLevel }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +module.exports = new FilterLogger(); |
0 commit comments