Skip to content

Commit

Permalink
[CHORE] Split logger classes to avoid cyclic dependencies (#15559)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto authored and sampaiodiego committed Oct 11, 2019
1 parent 9abf256 commit a9042ab
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 84 deletions.
76 changes: 76 additions & 0 deletions app/logger/server/publish.js
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
88 changes: 4 additions & 84 deletions app/logger/server/server.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -94,7 +86,7 @@ const defaultTypes = {
},
};

class _Logger {
export class Logger {
constructor(name, config = {}) {
const self = this;
this.name = name;
Expand Down Expand Up @@ -319,83 +311,11 @@ 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',
level: 0,
},
},
});


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 };

0 comments on commit a9042ab

Please sign in to comment.