Regression: Fix view logs admin screen (#23194)
parent
d2f70b9e41
commit
1bac896c13
@ -1,7 +1,2 @@ |
||||
import { Logger } from '../../../server/lib/logger/Logger'; |
||||
import './streamer'; |
||||
|
||||
// TODO there are imports pointing to this file still, ideally we should point everything to "/server/lib/logger/Logger" and remove this file
|
||||
export { |
||||
Logger, |
||||
}; |
||||
export { Logger } from '../../../server/lib/logger/Logger'; |
||||
|
||||
@ -1,71 +0,0 @@ |
||||
import { EventEmitter } from 'events'; |
||||
|
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { EJSON } from 'meteor/ejson'; |
||||
import { Log } from 'meteor/logging'; |
||||
|
||||
import { settings } from '../../settings/server'; |
||||
import notifications from '../../notifications/server/lib/Notifications'; |
||||
|
||||
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 = Object.assign(new EventEmitter(), { |
||||
queue: [], |
||||
}); |
||||
|
||||
const { write } = process.stdout; |
||||
|
||||
const maxInt = 2147483647; |
||||
let queueSize = 0; |
||||
|
||||
process.stdout.write = (...args) => { |
||||
write.apply(process.stdout, args); |
||||
const date = new Date(); |
||||
const string = processString(args[0], date); |
||||
const item = { |
||||
id: `logid-${ queueSize }`, |
||||
string, |
||||
ts: date, |
||||
}; |
||||
StdOut.queue.push(item); |
||||
|
||||
queueSize = (queueSize + 1) & maxInt; |
||||
|
||||
const limit = settings.get('Log_View_Limit') || 1000; |
||||
if (queueSize > limit) { |
||||
StdOut.queue.shift(); |
||||
} |
||||
|
||||
StdOut.emit('write', string, item); |
||||
}; |
||||
|
||||
Meteor.startup(() => { |
||||
const handler = (string, item) => { |
||||
// TODO having this as 'emitWithoutBroadcast' will not sent this data to ddp-streamer, so this data
|
||||
// won't be available when using micro services.
|
||||
notifications.streamStdout.emitWithoutBroadcast('stdout', { |
||||
...item, |
||||
}); |
||||
}; |
||||
|
||||
// do not emit to StdOut if moleculer log level set to debug because it creates an infinite loop
|
||||
if (String(process.env.MOLECULER_LOG_LEVEL).toLowerCase() !== 'debug') { |
||||
StdOut.on('write', handler); |
||||
} |
||||
}); |
||||
@ -0,0 +1,61 @@ |
||||
import EventEmitter from 'events'; |
||||
|
||||
type LogQueue = { |
||||
id: string; |
||||
data: string; |
||||
ts: Date; |
||||
}; |
||||
|
||||
const queue: LogQueue[] = []; |
||||
const maxInt = 2147483647; |
||||
let queueLimit = 1000; |
||||
let queueSize = 0; |
||||
|
||||
export function setQueueLimit(limit: number): void { |
||||
queueLimit = limit; |
||||
|
||||
if (queueSize > queueLimit) { |
||||
queue.splice(0, queueSize - queueLimit); |
||||
} |
||||
} |
||||
|
||||
export function getQueuedLogs(): LogQueue[] { |
||||
return queue; |
||||
} |
||||
|
||||
export const logEntries = new EventEmitter(); |
||||
|
||||
const { write } = process.stdout; |
||||
|
||||
function queueWrite(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean; |
||||
function queueWrite(str: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean; |
||||
function queueWrite(...args: any): boolean { |
||||
write.apply(process.stdout, args); |
||||
|
||||
const [str] = args; |
||||
if (typeof str !== 'string') { |
||||
return false; |
||||
} |
||||
|
||||
const date = new Date(); |
||||
const item = { |
||||
id: `logid-${ queueSize }`, |
||||
data: str, |
||||
ts: date, |
||||
}; |
||||
queue.push(item); |
||||
|
||||
queueSize = (queueSize + 1) & maxInt; |
||||
|
||||
if (queueSize > queueLimit) { |
||||
queue.shift(); |
||||
} |
||||
|
||||
logEntries.emit('log', item); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
if (String(process.env.MOLECULER_LOG_LEVEL).toLowerCase() !== 'debug') { |
||||
process.stdout.write = queueWrite; |
||||
} |
||||
@ -1,8 +1,15 @@ |
||||
import { settings } from '../../../app/settings/server'; |
||||
import { logLevel, LogLevelSetting } from './logLevel'; |
||||
import { setQueueLimit } from './logQueue'; |
||||
|
||||
settings.get('Log_Level', (_key, value) => { |
||||
if (value != null) { |
||||
logLevel.emit('changed', String(value) as LogLevelSetting); |
||||
} |
||||
}); |
||||
|
||||
settings.get('Log_View_Limit', (_key, value) => { |
||||
if (typeof value === 'number') { |
||||
setQueueLimit(value); |
||||
} |
||||
}); |
||||
|
||||
@ -0,0 +1,41 @@ |
||||
import { EJSON } from 'meteor/ejson'; |
||||
import { Log } from 'meteor/logging'; |
||||
|
||||
import notifications from '../../app/notifications/server/lib/Notifications'; |
||||
import { getQueuedLogs, logEntries } from '../lib/logger/logQueue'; |
||||
|
||||
const processString = function(string: string, date: Date): string { |
||||
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 transformLog = function(item: any): { id: string; string: string; ts: Date } { |
||||
return { |
||||
id: item.id, |
||||
string: processString(item.data, item.ts), |
||||
ts: item.ts, |
||||
}; |
||||
}; |
||||
|
||||
logEntries.on('log', (item) => { |
||||
// TODO having this as 'emitWithoutBroadcast' will not sent this data to ddp-streamer, so this data
|
||||
// won't be available when using micro services.
|
||||
notifications.streamStdout.emitWithoutBroadcast('stdout', transformLog(item)); |
||||
}); |
||||
|
||||
export function getLogs(): { id: string; string: string; ts: Date }[] { |
||||
return getQueuedLogs().map(transformLog); |
||||
} |
||||
Loading…
Reference in new issue