2018-06-20 00:46:59 +08:00
|
|
|
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')
|
2024-03-07 01:28:18 +08:00
|
|
|
const fallback = { console: { enabled: true, level: 'info' } };
|
|
|
|
const LOG_CONFIG = (JSON.parse(sessionStorage.getItem('clientStartupSettings') || '{}')?.clientLog) || fallback;
|
2018-06-20 00:46:59 +08:00
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
export function createStreamForTarget(target, options) {
|
|
|
|
const TARGET_EXTERNAL = 'external';
|
|
|
|
const TARGET_CONSOLE = 'console';
|
|
|
|
const TARGET_SERVER = 'server';
|
|
|
|
|
|
|
|
let Stream = ConsoleRawStream;
|
|
|
|
switch (target) {
|
|
|
|
case TARGET_EXTERNAL:
|
|
|
|
Stream = ServerLoggerStream;
|
|
|
|
break;
|
|
|
|
case TARGET_CONSOLE:
|
|
|
|
Stream = ConsoleFormattedStream;
|
|
|
|
break;
|
|
|
|
case TARGET_SERVER:
|
|
|
|
Stream = MeteorStream;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Stream = ConsoleFormattedStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Stream(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function generateLoggerStreams(config) {
|
|
|
|
let result = [];
|
|
|
|
Object.keys(config).forEach((key) => {
|
|
|
|
const logOption = config[key];
|
|
|
|
if (logOption && logOption.enabled) {
|
|
|
|
const { level, ...streamOptions } = logOption;
|
|
|
|
result = result.concat({ level, stream: createStreamForTarget(key, streamOptions) });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-20 00:46:59 +08:00
|
|
|
|
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 {
|
2019-11-16 05:46:42 +08:00
|
|
|
constructor(params) {
|
|
|
|
super(params);
|
|
|
|
|
|
|
|
if (params.logTag) {
|
|
|
|
this.logTagString = params.logTag;
|
|
|
|
}
|
2024-03-07 01:28:18 +08:00
|
|
|
this.auth = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
setConfig(config) {
|
|
|
|
const streams = generateLoggerStreams(config);
|
|
|
|
const { addStream } = this;
|
|
|
|
streams.forEach((stream) => {
|
|
|
|
addStream(stream);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setAuth(auth) {
|
|
|
|
this.auth = auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserData() {
|
|
|
|
let userInfo = {};
|
|
|
|
if (this.auth) {
|
|
|
|
userInfo = this.auth.fullInfo;
|
|
|
|
} else {
|
|
|
|
userInfo = {
|
|
|
|
meetingId: sessionStorage.getItem('meetingId'),
|
|
|
|
userId: sessionStorage.getItem('userId'),
|
|
|
|
logoutUrl: sessionStorage.getItem('logoutUrl'),
|
|
|
|
sessionToken: sessionStorage.getItem('sessionToken'),
|
|
|
|
userName: sessionStorage.getItem('userName'),
|
|
|
|
extId: sessionStorage.getItem('extId'),
|
|
|
|
meetingName: sessionStorage.getItem('meetingName'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userInfo.meetingId) {
|
|
|
|
userInfo = {
|
|
|
|
sessionToken: sessionStorage.getItem('sessionToken'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
fullInfo: userInfo,
|
|
|
|
};
|
2019-11-16 05:46:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-20 00:46:59 +08:00
|
|
|
write(rec) {
|
2024-03-07 01:28:18 +08:00
|
|
|
const { fullInfo } = this.getUserData();
|
2018-09-05 04:28:04 +08:00
|
|
|
|
2018-07-27 02:48:50 +08:00
|
|
|
this.rec = rec;
|
2018-06-20 00:46:59 +08:00
|
|
|
if (fullInfo.meetingId != null) {
|
2019-06-29 05:40:06 +08:00
|
|
|
this.rec.userInfo = fullInfo;
|
2018-06-20 00:46:59 +08:00
|
|
|
}
|
2024-04-07 06:35:41 +08:00
|
|
|
this.rec.clientBuild = window.meetingClientSettings?.public?.app?.html5ClientBuild;
|
|
|
|
this.rec.connectionId = Meteor?.connection?._lastSessionId;
|
2019-11-16 05:46:42 +08:00
|
|
|
if (this.logTagString) {
|
|
|
|
this.rec.logTag = this.logTagString;
|
2019-11-15 07:09:15 +08:00
|
|
|
}
|
2018-07-27 02:48:50 +08:00
|
|
|
return super.write(this.rec);
|
2018-06-20 00:46:59 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-27 02:48:50 +08:00
|
|
|
|
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) {
|
2024-03-07 01:28:18 +08:00
|
|
|
const { fullInfo } = this.getUserData();
|
2020-07-01 03:18:01 +08:00
|
|
|
const clientURL = window.location.href;
|
2018-09-05 04:28:04 +08:00
|
|
|
|
2018-07-27 02:48:50 +08:00
|
|
|
this.rec = rec;
|
2018-06-20 00:46:59 +08:00
|
|
|
if (fullInfo.meetingId != null) {
|
2020-07-01 03:18:01 +08:00
|
|
|
if (!this.rec.extraInfo) {
|
|
|
|
this.rec.extraInfo = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rec.extraInfo.clientURL = clientURL;
|
|
|
|
|
2019-06-29 05:40:06 +08:00
|
|
|
Meteor.call(
|
|
|
|
'logClient',
|
|
|
|
nameFromLevel[this.rec.level],
|
|
|
|
this.rec.msg,
|
|
|
|
this.rec.logCode,
|
2020-07-01 03:18:01 +08:00
|
|
|
this.rec.extraInfo,
|
2019-06-29 05:40:06 +08:00
|
|
|
fullInfo,
|
|
|
|
);
|
2018-06-20 00:46:59 +08:00
|
|
|
} else {
|
2020-07-01 03:18:01 +08:00
|
|
|
Meteor.call(
|
|
|
|
'logClient',
|
|
|
|
nameFromLevel[this.rec.level],
|
|
|
|
this.rec.msg,
|
2020-10-08 03:50:17 +08:00
|
|
|
this.rec.logCode,
|
|
|
|
{ ...rec.extraInfo, clientURL },
|
2020-07-01 03:18:01 +08:00
|
|
|
);
|
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;
|