1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var browser_core_1 = require("@datadog/browser-core");
|
5 | var logger_1 = require("./logger");
|
6 | var logs_1 = require("./logs");
|
7 | exports.datadogLogs = makeLogsGlobal(logs_1.startLogs);
|
8 | browser_core_1.defineGlobal(browser_core_1.getGlobalObject(), 'DD_LOGS', exports.datadogLogs);
|
9 | function makeLogsGlobal(startLogsImpl) {
|
10 | var isAlreadyInitialized = false;
|
11 | var globalContextManager = browser_core_1.createContextManager();
|
12 | var customLoggers = {};
|
13 | var beforeInitSendLog = new browser_core_1.BoundedBuffer();
|
14 | var sendLogStrategy = function (message, currentContext) {
|
15 | beforeInitSendLog.add([message, currentContext]);
|
16 | };
|
17 | var logger = new logger_1.Logger(sendLog);
|
18 | return browser_core_1.makeGlobal({
|
19 | logger: logger,
|
20 | init: browser_core_1.monitor(function (userConfiguration) {
|
21 | if (!browser_core_1.checkIsNotLocalFile() || !canInitLogs(userConfiguration)) {
|
22 | return;
|
23 | }
|
24 | if (userConfiguration.publicApiKey) {
|
25 | userConfiguration.clientToken = userConfiguration.publicApiKey;
|
26 | console.warn('Public API Key is deprecated. Please use Client Token instead.');
|
27 | }
|
28 | sendLogStrategy = startLogsImpl(userConfiguration, logger, globalContextManager.get);
|
29 | beforeInitSendLog.drain(function (_a) {
|
30 | var message = _a[0], context = _a[1];
|
31 | return sendLogStrategy(message, context);
|
32 | });
|
33 | isAlreadyInitialized = true;
|
34 | }),
|
35 | setLoggerGlobalContext: browser_core_1.monitor(globalContextManager.set),
|
36 | addLoggerGlobalContext: browser_core_1.monitor(globalContextManager.add),
|
37 | removeLoggerGlobalContext: browser_core_1.monitor(globalContextManager.remove),
|
38 | createLogger: browser_core_1.monitor(function (name, conf) {
|
39 | if (conf === void 0) { conf = {}; }
|
40 | customLoggers[name] = new logger_1.Logger(sendLog, conf.handler, conf.level, tslib_1.__assign(tslib_1.__assign({}, conf.context), { logger: { name: name } }));
|
41 | return customLoggers[name];
|
42 | }),
|
43 | getLogger: browser_core_1.monitor(function (name) {
|
44 | return customLoggers[name];
|
45 | }),
|
46 | });
|
47 | function canInitLogs(userConfiguration) {
|
48 | if (isAlreadyInitialized) {
|
49 | if (!userConfiguration.silentMultipleInit) {
|
50 | console.error('DD_LOGS is already initialized.');
|
51 | }
|
52 | return false;
|
53 | }
|
54 | if (!userConfiguration || (!userConfiguration.publicApiKey && !userConfiguration.clientToken)) {
|
55 | console.error('Client Token is not configured, we will not send any data.');
|
56 | return false;
|
57 | }
|
58 | if (userConfiguration.sampleRate !== undefined && !browser_core_1.isPercentage(userConfiguration.sampleRate)) {
|
59 | console.error('Sample Rate should be a number between 0 and 100');
|
60 | return false;
|
61 | }
|
62 | return true;
|
63 | }
|
64 | function sendLog(message) {
|
65 | sendLogStrategy(message, browser_core_1.combine({
|
66 | date: Date.now(),
|
67 | view: {
|
68 | referrer: document.referrer,
|
69 | url: window.location.href,
|
70 | },
|
71 | }, globalContextManager.get()));
|
72 | }
|
73 | }
|
74 | exports.makeLogsGlobal = makeLogsGlobal;
|
75 |
|
\ | No newline at end of file |