UNPKG

@datadog/browser-logs

Version:
60 lines 4.43 kB
import { ContextManagerMethod, CustomerContextKey, addTelemetryUsage, makePublicApi, monitor, sanitize, displayAlreadyInitializedError, deepClone, createTrackingConsentState, defineContextMethod, startBufferingData, callMonitored, mockable, } from '@datadog/browser-core'; import { Logger } from '../domain/logger'; import { buildCommonContext } from '../domain/contexts/commonContext'; import { startLogs } from './startLogs'; import { createPreStartStrategy } from './preStartLogs'; export function makeLogsPublicApi() { const trackingConsentState = createTrackingConsentState(); const bufferedDataObservable = startBufferingData().observable; let strategy = createPreStartStrategy(buildCommonContext, trackingConsentState, (initConfiguration, configuration, hooks) => { const startLogsResult = mockable(startLogs)(configuration, buildCommonContext, trackingConsentState, bufferedDataObservable, hooks); strategy = createPostStartStrategy(initConfiguration, startLogsResult); return startLogsResult; }); const getStrategy = () => strategy; const customLoggers = {}; const mainLogger = new Logger((...params) => strategy.handleLog(...params)); return makePublicApi({ logger: mainLogger, init: (initConfiguration) => { const errorStack = new Error().stack; callMonitored(() => strategy.init(initConfiguration, errorStack)); }, setTrackingConsent: monitor((trackingConsent) => { trackingConsentState.update(trackingConsent); addTelemetryUsage({ feature: 'set-tracking-consent', tracking_consent: trackingConsent }); }), getGlobalContext: defineContextMethod(getStrategy, CustomerContextKey.globalContext, ContextManagerMethod.getContext), setGlobalContext: defineContextMethod(getStrategy, CustomerContextKey.globalContext, ContextManagerMethod.setContext), setGlobalContextProperty: defineContextMethod(getStrategy, CustomerContextKey.globalContext, ContextManagerMethod.setContextProperty), removeGlobalContextProperty: defineContextMethod(getStrategy, CustomerContextKey.globalContext, ContextManagerMethod.removeContextProperty), clearGlobalContext: defineContextMethod(getStrategy, CustomerContextKey.globalContext, ContextManagerMethod.clearContext), createLogger: monitor((name, conf = {}) => { customLoggers[name] = new Logger((...params) => strategy.handleLog(...params), sanitize(name), conf.handler, conf.level, sanitize(conf.context)); return customLoggers[name]; }), getLogger: monitor((name) => customLoggers[name]), getInitConfiguration: monitor(() => deepClone(strategy.initConfiguration)), getInternalContext: monitor((startTime) => strategy.getInternalContext(startTime)), setUser: defineContextMethod(getStrategy, CustomerContextKey.userContext, ContextManagerMethod.setContext), getUser: defineContextMethod(getStrategy, CustomerContextKey.userContext, ContextManagerMethod.getContext), setUserProperty: defineContextMethod(getStrategy, CustomerContextKey.userContext, ContextManagerMethod.setContextProperty), removeUserProperty: defineContextMethod(getStrategy, CustomerContextKey.userContext, ContextManagerMethod.removeContextProperty), clearUser: defineContextMethod(getStrategy, CustomerContextKey.userContext, ContextManagerMethod.clearContext), setAccount: defineContextMethod(getStrategy, CustomerContextKey.accountContext, ContextManagerMethod.setContext), getAccount: defineContextMethod(getStrategy, CustomerContextKey.accountContext, ContextManagerMethod.getContext), setAccountProperty: defineContextMethod(getStrategy, CustomerContextKey.accountContext, ContextManagerMethod.setContextProperty), removeAccountProperty: defineContextMethod(getStrategy, CustomerContextKey.accountContext, ContextManagerMethod.removeContextProperty), clearAccount: defineContextMethod(getStrategy, CustomerContextKey.accountContext, ContextManagerMethod.clearContext), }); } function createPostStartStrategy(initConfiguration, startLogsResult) { return { init: (initConfiguration) => { displayAlreadyInitializedError('DD_LOGS', initConfiguration); }, initConfiguration, ...startLogsResult, }; } //# sourceMappingURL=logsPublicApi.js.map