-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CHORE] Split logger classes to avoid cyclic dependencies (#15559)
- Loading branch information
1 parent
9abf256
commit a9042ab
Showing
2 changed files
with
80 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters