2018-06-20 00:46:59 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { createLogger, stdSerializers } from 'browser-bunyan';
|
|
|
|
import { ConsoleFormattedStream } from '@browser-bunyan/console-formatted-stream';
|
2018-07-17 05:19:13 +08:00
|
|
|
import { ConsoleRawStream } from '@browser-bunyan/console-raw-stream';
|
2018-06-20 00:46:59 +08:00
|
|
|
import { ServerStream } from '@browser-bunyan/server-stream';
|
|
|
|
import { nameFromLevel } from '@browser-bunyan/levels';
|
|
|
|
|
2018-07-12 06:03:56 +08:00
|
|
|
// The logger accepts "console","server", and "external" as targets
|
|
|
|
// Multiple targets can be set as an array in the settings under public.log
|
|
|
|
// To add more targets use the format { "target": "server", "level": "info" },
|
|
|
|
// and add it to the public.log array
|
2018-06-20 00:46:59 +08:00
|
|
|
// The accepted levels are "debug", "info", "warn", "error"
|
2018-07-20 00:21:26 +08:00
|
|
|
// To send to URL, use the format {"target": "external","level": "info",
|
|
|
|
// "url": "","method": ""}
|
2018-07-12 06:03:56 +08:00
|
|
|
// externalURL is the end-point that logs will be sent to
|
2018-07-14 03:44:22 +08:00
|
|
|
// Call the logger by doing a function call with the level name, I.e, logger.warn('Hi on warn')
|
2018-06-20 00:46:59 +08:00
|
|
|
|
|
|
|
const LOG_CONFIG = Meteor.settings.public.log || {};
|
|
|
|
const { fullInfo } = Auth;
|
|
|
|
|
2018-07-20 00:21:26 +08:00
|
|
|
// Custom stream that logs to an end-point
|
2018-06-20 00:46:59 +08:00
|
|
|
class ServerLoggerStream extends ServerStream {
|
|
|
|
write(rec) {
|
|
|
|
if (fullInfo.meetingId != null) {
|
|
|
|
rec.clientInfo = fullInfo;
|
|
|
|
}
|
|
|
|
return super.write(rec);
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 00:21:26 +08:00
|
|
|
// Custom stream to log to the meteor server
|
2018-06-20 00:46:59 +08:00
|
|
|
class MeteorStream {
|
|
|
|
write(rec) {
|
|
|
|
if (fullInfo.meetingId != null) {
|
|
|
|
Meteor.call('logClient', nameFromLevel[rec.level], rec.msg, fullInfo);
|
|
|
|
} else {
|
|
|
|
Meteor.call('logClient', nameFromLevel[rec.level], rec.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 05:19:13 +08:00
|
|
|
function createStreamForTarget(target, options) {
|
|
|
|
const TARGET_EXTERNAL = 'external';
|
|
|
|
const TARGET_CONSOLE = 'console';
|
|
|
|
const TARGET_SERVER = 'server';
|
2018-07-20 00:21:26 +08:00
|
|
|
|
2018-07-17 05:19:13 +08:00
|
|
|
let Stream = ConsoleRawStream;
|
|
|
|
switch (target) {
|
|
|
|
case TARGET_EXTERNAL:
|
2018-07-20 00:21:26 +08:00
|
|
|
Stream = ServerLoggerStream;
|
2018-07-12 06:03:56 +08:00
|
|
|
break;
|
2018-07-17 05:19:13 +08:00
|
|
|
case TARGET_CONSOLE:
|
|
|
|
Stream = ConsoleFormattedStream;
|
2018-07-12 06:03:56 +08:00
|
|
|
break;
|
2018-07-17 05:19:13 +08:00
|
|
|
case TARGET_SERVER:
|
|
|
|
Stream = MeteorStream;
|
2018-07-12 06:03:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-07-20 00:21:26 +08:00
|
|
|
|
2018-07-17 05:19:13 +08:00
|
|
|
return new Stream(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateLoggerStreams(config) {
|
2018-07-20 00:21:26 +08:00
|
|
|
return config.map(({ target, level, ...streamOptions }) => ({
|
|
|
|
level,
|
|
|
|
stream: createStreamForTarget(target, streamOptions),
|
|
|
|
}));
|
2018-06-20 00:46:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the logger with the array of streams of the chosen targets
|
|
|
|
const logger = createLogger({
|
|
|
|
name: 'clientLogger',
|
2018-07-17 05:19:13 +08:00
|
|
|
streams: generateLoggerStreams(LOG_CONFIG),
|
2018-06-20 00:46:59 +08:00
|
|
|
serializers: stdSerializers,
|
|
|
|
src: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export default logger;
|