BVB Source Codes

parse-server Show LoggerController.js Source code

Return Download parse-server: download LoggerController.js Source code - Download parse-server Source code - Type:.js
  1. import { Parse } from 'parse/node';
  2. import AdaptableController from './AdaptableController';
  3. import { LoggerAdapter } from '../Adapters/Logger/LoggerAdapter';
  4. import url from 'url';
  5.  
  6. const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
  7. const LOG_STRING_TRUNCATE_LENGTH = 1000;
  8. const truncationMarker = '... (truncated)';
  9.  
  10. export const LogLevel = {
  11.   INFO: 'info',
  12.   ERROR: 'error'
  13. }
  14.  
  15. export const LogOrder = {
  16.   DESCENDING: 'desc',
  17.   ASCENDING: 'asc'
  18. }
  19.  
  20. export class LoggerController extends AdaptableController {
  21.  
  22.   maskSensitiveUrl(urlString) {
  23.     const password = url.parse(urlString, true).query.password;
  24.  
  25.     if (password) {
  26.       urlString = urlString.replace('password=' + password, 'password=********');
  27.     }
  28.     return urlString;
  29.   }
  30.  
  31.   maskSensitive(argArray) {
  32.     return argArray.map(e => {
  33.       if (!e) {
  34.         return e;
  35.       }
  36.  
  37.       if (typeof e === 'string') {
  38.         return e.replace(/(password".?:.?")[^"]*"/g, '$1********"');
  39.       }
  40.       // else it is an object...
  41.  
  42.       // check the url
  43.       if (e.url) {
  44.         // for strings
  45.         if (typeof e.url === 'string') {
  46.           e.url = this.maskSensitiveUrl(e.url);
  47.         } else if (Array.isArray(e.url)) { // for strings in array
  48.           e.url = e.url.map(item => {
  49.             if (typeof item === 'string') {
  50.               return this.maskSensitiveUrl(item);
  51.             }
  52.  
  53.             return item;
  54.           });
  55.         }
  56.       }
  57.  
  58.       if (e.body) {
  59.         for (const key of Object.keys(e.body)) {
  60.           if (key === 'password') {
  61.             e.body[key] = '********';
  62.             break;
  63.           }
  64.         }
  65.       }
  66.  
  67.       if (e.params) {
  68.         for (const key of Object.keys(e.params)) {
  69.           if (key === 'password') {
  70.             e.params[key] = '********';
  71.             break;
  72.           }
  73.         }
  74.       }
  75.  
  76.       return e;
  77.     });
  78.   }
  79.  
  80.   log(level, args) {
  81.     // make the passed in arguments object an array with the spread operator
  82.     args = this.maskSensitive([...args]);
  83.     args = [].concat(level, args);
  84.     this.adapter.log.apply(this.adapter, args);
  85.   }
  86.  
  87.   info() {
  88.     return this.log('info', arguments);
  89.   }
  90.  
  91.   error() {
  92.     return this.log('error', arguments);
  93.   }
  94.  
  95.   warn() {
  96.     return this.log('warn', arguments);
  97.   }
  98.  
  99.   verbose() {
  100.     return this.log('verbose', arguments);
  101.   }
  102.  
  103.   debug() {
  104.     return this.log('debug', arguments);
  105.   }
  106.  
  107.   silly() {
  108.     return this.log('silly', arguments);
  109.   }
  110.   // check that date input is valid
  111.   static validDateTime(date) {
  112.     if (!date) {
  113.       return null;
  114.     }
  115.     date = new Date(date);
  116.  
  117.     if (!isNaN(date.getTime())) {
  118.       return date;
  119.     }
  120.  
  121.     return null;
  122.   }
  123.  
  124.   truncateLogMessage(string) {
  125.     if (string && string.length > LOG_STRING_TRUNCATE_LENGTH) {
  126.       const truncated = string.substring(0, LOG_STRING_TRUNCATE_LENGTH) + truncationMarker;
  127.       return truncated;
  128.     }
  129.  
  130.     return string;
  131.   }
  132.  
  133.   static parseOptions(options = {}) {
  134.     const from = LoggerController.validDateTime(options.from) ||
  135.       new Date(Date.now() - 7 * MILLISECONDS_IN_A_DAY);
  136.     const until = LoggerController.validDateTime(options.until) || new Date();
  137.     const size = Number(options.size) || 10;
  138.     const order = options.order || LogOrder.DESCENDING;
  139.     const level = options.level || LogLevel.INFO;
  140.  
  141.     return {
  142.       from,
  143.       until,
  144.       size,
  145.       order,
  146.       level,
  147.     };
  148.   }
  149.  
  150.   // Returns a promise for a {response} object.
  151.   // query params:
  152.   // level (optional) Level of logging you want to query for (info || error)
  153.   // from (optional) Start time for the search. Defaults to 1 week ago.
  154.   // until (optional) End time for the search. Defaults to current time.
  155.   // order (optional) Direction of results returned, either 鈥渁sc鈥 or 鈥渄esc鈥? Defaults to 鈥渄esc鈥?
  156.   // size (optional) Number of rows returned by search. Defaults to 10
  157.   getLogs(options = {}) {
  158.     if (!this.adapter) {
  159.       throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
  160.         'Logger adapter is not available');
  161.     }
  162.     if (typeof this.adapter.query !== 'function') {
  163.       throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
  164.         'Querying logs is not supported with this adapter');
  165.     }
  166.     options = LoggerController.parseOptions(options);
  167.     return this.adapter.query(options);
  168.   }
  169.  
  170.   expectedAdapterType() {
  171.     return LoggerAdapter;
  172.   }
  173. }
  174.  
  175. export default LoggerController;
  176.  
downloadLoggerController.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top