From a9042ab94b6d2b943168143ea64d552c7d69bbbd Mon Sep 17 00:00:00 2001 From: Marcos Spessatto Defendi Date: Fri, 11 Oct 2019 10:45:07 -0300 Subject: [PATCH] [CHORE] Split logger classes to avoid cyclic dependencies (#15559) --- app/logger/server/publish.js | 76 +++++++++++++++++++++++++++++++ app/logger/server/server.js | 88 ++---------------------------------- 2 files changed, 80 insertions(+), 84 deletions(-) create mode 100644 app/logger/server/publish.js diff --git a/app/logger/server/publish.js b/app/logger/server/publish.js new file mode 100644 index 000000000000..d1ccdeef2ad0 --- /dev/null +++ b/app/logger/server/publish.js @@ -0,0 +1,76 @@ +import { EventEmitter } from 'events'; + +import { Meteor } from 'meteor/meteor'; +import { Random } from 'meteor/random'; +import { EJSON } from 'meteor/ejson'; +import { Log } from 'meteor/logging'; + +import { settings } from '../../settings'; +import { hasPermission } from '../../authorization'; + +export const processString = function(string, date) { + let obj; + try { + if (string[0] === '{') { + obj = EJSON.parse(string); + } else { + obj = { + message: string, + time: date, + level: 'info', + }; + } + return Log.format(obj, { color: true }); + } catch (error) { + return string; + } +}; + +export const StdOut = new class extends EventEmitter { + constructor() { + super(); + const { write } = process.stdout; + this.queue = []; + process.stdout.write = (...args) => { + write.apply(process.stdout, args); + const date = new Date(); + const string = processString(args[0], date); + const item = { + id: Random.id(), + string, + ts: date, + }; + this.queue.push(item); + + if (typeof settings !== 'undefined') { + const limit = settings.get('Log_View_Limit'); + if (limit && this.queue.length > limit) { + this.queue.shift(); + } + } + this.emit('write', string, item); + }; + } +}(); + + +Meteor.publish('stdout', function() { + if (!this.userId || hasPermission(this.userId, 'view-logs') !== true) { + return this.ready(); + } + + StdOut.queue.forEach((item) => { + this.added('stdout', item.id, { + string: item.string, + ts: item.ts, + }); + }); + + this.ready(); + StdOut.on('write', (string, item) => { + this.added('stdout', item.id, { + string: item.string, + ts: item.ts, + }); + }); +}); diff --git a/app/logger/server/server.js b/app/logger/server/server.js index 40c3c7dc8237..bce22758c551 100644 --- a/app/logger/server/server.js +++ b/app/logger/server/server.js @@ -1,18 +1,9 @@ import { EventEmitter } from 'events'; -import { Meteor } from 'meteor/meteor'; -import { Random } from 'meteor/random'; -import { EJSON } from 'meteor/ejson'; -import { Log } from 'meteor/logging'; import _ from 'underscore'; import s from 'underscore.string'; -import { settings } from '../../settings'; -import { hasPermission } from '../../authorization'; - -let Logger; - -const LoggerManager = new class extends EventEmitter { +export const LoggerManager = new class extends EventEmitter { constructor() { super(); this.enabled = false; @@ -24,6 +15,7 @@ const LoggerManager = new class extends EventEmitter { } register(logger) { + // eslint-disable-next-line no-use-before-define if (!(logger instanceof Logger)) { return; } @@ -94,7 +86,7 @@ const defaultTypes = { }, }; -class _Logger { +export class Logger { constructor(name, config = {}) { const self = this; this.name = name; @@ -319,26 +311,7 @@ class _Logger { } } -Logger = _Logger; -const processString = function(string, date) { - let obj; - try { - if (string[0] === '{') { - obj = EJSON.parse(string); - } else { - obj = { - message: string, - time: date, - level: 'info', - }; - } - return Log.format(obj, { color: true }); - } catch (error) { - return string; - } -}; - -const SystemLogger = new Logger('System', { +export const SystemLogger = new Logger('System', { methods: { startup: { type: 'success', @@ -346,56 +319,3 @@ const SystemLogger = new Logger('System', { }, }, }); - - -const StdOut = new class extends EventEmitter { - constructor() { - super(); - const { write } = process.stdout; - this.queue = []; - process.stdout.write = (...args) => { - write.apply(process.stdout, args); - const date = new Date(); - const string = processString(args[0], date); - const item = { - id: Random.id(), - string, - ts: date, - }; - this.queue.push(item); - - if (typeof settings !== 'undefined') { - const limit = settings.get('Log_View_Limit'); - if (limit && this.queue.length > limit) { - this.queue.shift(); - } - } - this.emit('write', string, item); - }; - } -}(); - - -Meteor.publish('stdout', function() { - if (!this.userId || hasPermission(this.userId, 'view-logs') !== true) { - return this.ready(); - } - - StdOut.queue.forEach((item) => { - this.added('stdout', item.id, { - string: item.string, - ts: item.ts, - }); - }); - - this.ready(); - StdOut.on('write', (string, item) => { - this.added('stdout', item.id, { - string: item.string, - ts: item.ts, - }); - }); -}); - - -export { SystemLogger, StdOut, LoggerManager, processString, Logger };