@cloudcare/browser-logs 1.1.24 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +5 -4
  2. package/bundle/dataflux-logs.js +1 -1
  3. package/cjs/boot/buildEnv.js +1 -1
  4. package/cjs/boot/logsPublicApi.js +133 -0
  5. package/cjs/boot/startLogs.js +63 -0
  6. package/cjs/domain/assembly.js +74 -0
  7. package/cjs/domain/configuration.js +55 -0
  8. package/cjs/domain/internalContext.js +22 -0
  9. package/cjs/domain/logger.js +28 -36
  10. package/cjs/domain/logsCollection/console/consoleCollection.js +39 -0
  11. package/cjs/domain/logsCollection/logger/loggerCollection.js +51 -0
  12. package/cjs/domain/logsCollection/networkError/networkErrorCollection.js +238 -0
  13. package/cjs/domain/logsCollection/report/reportCollection.js +50 -0
  14. package/cjs/domain/logsCollection/rumtimeError/runtimeErrorCollection.js +46 -0
  15. package/cjs/domain/{loggerSession.js → logsSessionManager.js} +19 -23
  16. package/cjs/entries/main.js +16 -0
  17. package/cjs/index.js +2 -2
  18. package/cjs/transport/startLogsBatch.js +15 -0
  19. package/esm/boot/buildEnv.js +1 -1
  20. package/esm/boot/logsPublicApi.js +122 -0
  21. package/esm/boot/startLogs.js +45 -0
  22. package/esm/domain/assembly.js +59 -0
  23. package/esm/domain/configuration.js +41 -0
  24. package/esm/domain/internalContext.js +15 -0
  25. package/esm/domain/logger.js +26 -36
  26. package/esm/domain/logsCollection/console/consoleCollection.js +31 -0
  27. package/esm/domain/logsCollection/logger/loggerCollection.js +38 -0
  28. package/esm/domain/logsCollection/networkError/networkErrorCollection.js +223 -0
  29. package/esm/domain/logsCollection/report/reportCollection.js +40 -0
  30. package/esm/domain/logsCollection/rumtimeError/runtimeErrorCollection.js +37 -0
  31. package/esm/domain/logsSessionManager.js +45 -0
  32. package/esm/entries/main.js +5 -0
  33. package/esm/index.js +1 -1
  34. package/esm/transport/startLogsBatch.js +7 -0
  35. package/package.json +3 -3
  36. package/src/boot/logsPublicApi.js +140 -0
  37. package/src/boot/startLogs.js +51 -0
  38. package/src/domain/assembly.js +101 -0
  39. package/src/domain/configuration.js +70 -0
  40. package/src/domain/internalContext.js +15 -0
  41. package/src/domain/logger.js +37 -58
  42. package/src/domain/logsCollection/console/consoleCollection.js +36 -0
  43. package/src/domain/logsCollection/logger/loggerCollection.js +48 -0
  44. package/src/domain/logsCollection/networkError/networkErrorCollection.js +260 -0
  45. package/src/domain/logsCollection/report/reportCollection.js +49 -0
  46. package/src/domain/logsCollection/rumtimeError/runtimeErrorCollection.js +35 -0
  47. package/src/domain/logsSessionManager.js +52 -0
  48. package/src/entries/main.js +7 -0
  49. package/src/index.js +1 -1
  50. package/src/transport/startLogsBatch.js +17 -0
  51. package/cjs/boot/log.entry.js +0 -115
  52. package/cjs/boot/log.js +0 -132
  53. package/esm/boot/log.entry.js +0 -87
  54. package/esm/boot/log.js +0 -115
  55. package/esm/domain/loggerSession.js +0 -46
  56. package/src/boot/log.entry.js +0 -119
  57. package/src/boot/log.js +0 -185
  58. package/src/domain/loggerSession.js +0 -56
@@ -0,0 +1,40 @@
1
+ import { timeStampNow, ErrorSource, RawReportType, getFileFromStackTraceString, initReportObservable, LifeCycleEventType } from '@cloudcare/browser-core';
2
+ import { StatusType } from '../../logger';
3
+ var LogStatusForReport = {
4
+ [RawReportType.cspViolation]: StatusType.error,
5
+ [RawReportType.intervention]: StatusType.error,
6
+ [RawReportType.deprecation]: StatusType.warn
7
+ };
8
+ export function startReportCollection(configuration, lifeCycle) {
9
+ var reportSubscription = initReportObservable(configuration.forwardReports).subscribe(function (report) {
10
+ var message = report.message;
11
+ var status = LogStatusForReport[report.type];
12
+ var error;
13
+
14
+ if (status === StatusType.error) {
15
+ error = {
16
+ kind: report.subtype,
17
+ origin: ErrorSource.REPORT,
18
+ // Todo: Remove in the next major release
19
+ stack: report.stack
20
+ };
21
+ } else if (report.stack) {
22
+ message += ' Found in ' + getFileFromStackTraceString(report.stack);
23
+ }
24
+
25
+ lifeCycle.notify(LifeCycleEventType.RAW_LOG_COLLECTED, {
26
+ rawLogsEvent: {
27
+ date: timeStampNow(),
28
+ message: message,
29
+ origin: ErrorSource.REPORT,
30
+ error: error,
31
+ status: status
32
+ }
33
+ });
34
+ });
35
+ return {
36
+ stop: function stop() {
37
+ reportSubscription.unsubscribe();
38
+ }
39
+ };
40
+ }
@@ -0,0 +1,37 @@
1
+ import { noop, ErrorSource, trackRuntimeError, Observable, LifeCycleEventType } from '@cloudcare/browser-core';
2
+ import { StatusType } from '../../logger';
3
+ export function startRuntimeErrorCollection(configuration, lifeCycle) {
4
+ if (!configuration.forwardErrorsToLogs) {
5
+ return {
6
+ stop: noop
7
+ };
8
+ }
9
+
10
+ var rawErrorObservable = new Observable();
11
+
12
+ var _trackRuntimeError = trackRuntimeError(rawErrorObservable);
13
+
14
+ var rawErrorSubscription = rawErrorObservable.subscribe(function (rawError) {
15
+ lifeCycle.notify(LifeCycleEventType.RAW_LOG_COLLECTED, {
16
+ rawLogsEvent: {
17
+ message: rawError.message,
18
+ date: rawError.startClocks.timeStamp,
19
+ error: {
20
+ kind: rawError.type,
21
+ origin: ErrorSource.SOURCE,
22
+ // Todo: Remove in the next major release
23
+ stack: rawError.stack
24
+ },
25
+ origin: ErrorSource.SOURCE,
26
+ status: StatusType.error
27
+ }
28
+ });
29
+ });
30
+ return {
31
+ stop: function stop() {
32
+ _trackRuntimeError.stop();
33
+
34
+ rawErrorSubscription.unsubscribe();
35
+ }
36
+ };
37
+ }
@@ -0,0 +1,45 @@
1
+ import { performDraw, startSessionManager } from '@cloudcare/browser-core';
2
+ export var LOGS_SESSION_KEY = 'logs';
3
+ export var LoggerTrackingType = {
4
+ NOT_TRACKED: '0',
5
+ TRACKED: '1'
6
+ };
7
+ export function startLogsSessionManager(configuration) {
8
+ var sessionManager = startSessionManager(configuration.cookieOptions, LOGS_SESSION_KEY, function (rawTrackingType) {
9
+ return computeSessionState(configuration, rawTrackingType);
10
+ });
11
+ return {
12
+ findTrackedSession: function findTrackedSession(startTime) {
13
+ var session = sessionManager.findActiveSession(startTime);
14
+ return session && session.trackingType === LoggerTrackingType.TRACKED ? {
15
+ id: session.id
16
+ } : undefined;
17
+ }
18
+ };
19
+ } // export function startLogsSessionManagerStub(configuration) {
20
+ // var isTracked = computeTrackingType(configuration) === LoggerTrackingType.TRACKED
21
+ // var session = isTracked ? {} : undefined
22
+ // return {
23
+ // findTrackedSession: function() { return session },
24
+ // }
25
+ // }
26
+
27
+ function computeTrackingType(configuration) {
28
+ if (!performDraw(configuration.sampleRate)) {
29
+ return LoggerTrackingType.NOT_TRACKED;
30
+ }
31
+
32
+ return LoggerTrackingType.TRACKED;
33
+ }
34
+
35
+ function computeSessionState(configuration, rawSessionType) {
36
+ var trackingType = hasValidLoggerSession(rawSessionType) ? rawSessionType : computeTrackingType(configuration);
37
+ return {
38
+ trackingType: trackingType,
39
+ isTracked: trackingType === LoggerTrackingType.TRACKED
40
+ };
41
+ }
42
+
43
+ function hasValidLoggerSession(trackingType) {
44
+ return trackingType === LoggerTrackingType.NOT_TRACKED || trackingType === LoggerTrackingType.TRACKED;
45
+ }
@@ -0,0 +1,5 @@
1
+ import { defineGlobal, getGlobalObject } from '@cloudcare/browser-core';
2
+ import { makeLogsPublicApi } from '../boot/logsPublicApi';
3
+ import { startLogs } from '../boot/startLogs';
4
+ export var datafluxLogs = makeLogsPublicApi(startLogs);
5
+ defineGlobal(getGlobalObject(), 'DATAFLUX_LOGS', datafluxLogs);
package/esm/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { datafluxLogs } from './boot/log.entry';
1
+ import { datafluxLogs } from './entries/main';
2
2
  export { datafluxLogs };
@@ -0,0 +1,7 @@
1
+ import { startBatchWithReplica, LifeCycleEventType } from '@cloudcare/browser-core';
2
+ export function startLogsBatch(configuration, lifeCycle, reportError) {
3
+ var batch = startBatchWithReplica(configuration, configuration.logsEndpoint, reportError);
4
+ lifeCycle.subscribe(LifeCycleEventType.LOG_COLLECTED, function (serverLogsEvent) {
5
+ batch.add(serverLogsEvent);
6
+ });
7
+ }
package/package.json CHANGED
@@ -2,9 +2,9 @@
2
2
  "name": "@cloudcare/browser-logs",
3
3
  "main": "cjs/index.js",
4
4
  "module": "esm/index.js",
5
- "version": "1.1.24",
5
+ "version": "1.2.1",
6
6
  "dependencies": {
7
- "@cloudcare/browser-core": "1.1.3"
7
+ "@cloudcare/browser-core": "1.2.1"
8
8
  },
9
9
  "scripts": {
10
10
  "dev": "webpack serve --open --mode=development",
@@ -28,5 +28,5 @@
28
28
  "author": "dataflux",
29
29
  "license": "MIT",
30
30
  "description": "DataFlux RUM Web 端数据指标监控",
31
- "gitHead": "8cd2d38961b7663bde39072730de22bcf4ba9c2b"
31
+ "gitHead": "e8411cbf5448e90b81a5e902c54cceb44e6e2501"
32
32
  }
@@ -0,0 +1,140 @@
1
+ import {
2
+ BoundedBuffer,
3
+ createContextManager,
4
+ makePublicApi,
5
+ display,
6
+ deepClone,
7
+ timeStampNow,
8
+ } from '@cloudcare/browser-core'
9
+ import { validateAndBuildLogsConfiguration } from '../domain/configuration'
10
+ import { Logger } from '../domain/logger'
11
+
12
+ export function makeLogsPublicApi(startLogsImpl) {
13
+ var isAlreadyInitialized = false
14
+
15
+ var globalContextManager = createContextManager()
16
+ var customLoggers = {}
17
+ var getInternalContextStrategy = function() {
18
+ return undefined
19
+ }
20
+
21
+ var beforeInitLoggerLog = new BoundedBuffer()
22
+
23
+ var handleLogStrategy = function(
24
+ logsMessage,
25
+ logger,
26
+ savedCommonContext,
27
+ date
28
+ ){
29
+ if (typeof savedCommonContext === 'undefined') {
30
+ savedCommonContext = deepClone(getCommonContext())
31
+ }
32
+ if (typeof date === 'undefined') {
33
+ date = timeStampNow()
34
+ }
35
+ beforeInitLoggerLog.add(function() {
36
+ return handleLogStrategy(logsMessage, logger, savedCommonContext, date)
37
+ })
38
+ }
39
+
40
+ var getInitConfigurationStrategy = function() {
41
+ return undefined
42
+ }
43
+
44
+ var mainLogger = new Logger(function() {
45
+ return handleLogStrategy.apply(this, arguments)
46
+ })
47
+
48
+ function getCommonContext() {
49
+ return {
50
+ view: {
51
+ referrer: document.referrer,
52
+ url: window.location.href,
53
+ },
54
+ context: globalContextManager.get(),
55
+ }
56
+ }
57
+
58
+ return makePublicApi({
59
+ logger: mainLogger,
60
+
61
+ init: function(initConfiguration) {
62
+ if (!canInitLogs(initConfiguration)) {
63
+ return
64
+ }
65
+
66
+ var configuration = validateAndBuildLogsConfiguration(initConfiguration)
67
+ if (!configuration) {
68
+ return
69
+ }
70
+ var _startLogsImpl = startLogsImpl(configuration, getCommonContext, mainLogger)
71
+ handleLogStrategy = _startLogsImpl.handleLog
72
+ getInternalContextStrategy = _startLogsImpl.getInternalContext
73
+ getInitConfigurationStrategy = function() {
74
+ return deepClone(initConfiguration)
75
+ }
76
+ beforeInitLoggerLog.drain()
77
+
78
+ isAlreadyInitialized = true
79
+ },
80
+
81
+ /** @deprecated: use getGlobalContext instead */
82
+ getGlobalContext: globalContextManager.getContext,
83
+
84
+ /** @deprecated: use setGlobalContext instead */
85
+ setGlobalContext: globalContextManager.setContext,
86
+
87
+ /** @deprecated: use setGlobalContextProperty instead */
88
+ setGlobalContextProperty: globalContextManager.setContextProperty,
89
+
90
+ /** @deprecated: use removeGlobalContextProperty instead */
91
+ removeGlobalContextProperty: globalContextManager.removeContextProperty,
92
+
93
+ clearGlobalContext: globalContextManager.clearContext,
94
+
95
+ createLogger: function(name, conf){
96
+ if (typeof conf == 'undefined') {
97
+ conf = {}
98
+ }
99
+ customLoggers[name] = new Logger(
100
+ function() {
101
+ return handleLogStrategy(this. arguments)
102
+ },
103
+ name,
104
+ conf.handler,
105
+ conf.level,
106
+ conf.context
107
+ )
108
+ return customLoggers[name]
109
+ },
110
+
111
+ getLogger: function(name) {
112
+ return customLoggers[name]
113
+ },
114
+
115
+ getInitConfiguration: function() {
116
+ return getInitConfigurationStrategy()
117
+ },
118
+
119
+ getInternalContext: function(startTime) {
120
+ return getInternalContextStrategy(startTime)
121
+ },
122
+ })
123
+
124
+
125
+ function canInitLogs(initConfiguration) {
126
+ if (isAlreadyInitialized) {
127
+ if (!initConfiguration.silentMultipleInit) {
128
+ display.error('DD_LOGS is already initialized.')
129
+ }
130
+ return false
131
+ }
132
+ if (!initConfiguration.datakitUrl && !initConfiguration.datakitOrigin) {
133
+ display.error(
134
+ 'datakitOrigin is not configured, no RUM data will be collected.'
135
+ )
136
+ return false
137
+ }
138
+ return true
139
+ }
140
+ }
@@ -0,0 +1,51 @@
1
+ import {
2
+ ErrorSource,
3
+ LifeCycle,
4
+ LifeCycleEventType
5
+ } from '@cloudcare/browser-core'
6
+ import { startLogsSessionManager } from '../domain/logsSessionManager'
7
+ import { startLogsAssembly } from '../domain/assembly'
8
+ import { startConsoleCollection } from '../domain/logsCollection/console/consoleCollection'
9
+ import { startReportCollection } from '../domain/logsCollection/report/reportCollection'
10
+ import { startNetworkErrorCollection } from '../domain/logsCollection/networkError/networkErrorCollection'
11
+ import { startRuntimeErrorCollection } from '../domain/logsCollection/rumtimeError/runtimeErrorCollection'
12
+ import { startLoggerCollection } from '../domain/logsCollection/logger/loggerCollection'
13
+ import { startLogsBatch } from '../transport/startLogsBatch'
14
+ import { StatusType } from '../domain/logger'
15
+ import { startInternalContext } from '../domain/internalContext'
16
+
17
+ export function startLogs(configuration, getCommonContext, mainLogger) {
18
+ var lifeCycle = new LifeCycle()
19
+
20
+ const reportError = function(error) {
21
+ lifeCycle.notify(LifeCycleEventType.RAW_LOG_COLLECTED, {
22
+ rawLogsEvent: {
23
+ message: error.message,
24
+ date: error.startClocks.timeStamp,
25
+ error: {
26
+ origin: ErrorSource.AGENT, // Todo: Remove in the next major release
27
+ },
28
+ origin: ErrorSource.AGENT,
29
+ status: StatusType.error,
30
+ },
31
+ })
32
+ }
33
+ startNetworkErrorCollection(configuration, lifeCycle)
34
+ startRuntimeErrorCollection(configuration, lifeCycle)
35
+ startConsoleCollection(configuration, lifeCycle)
36
+ startReportCollection(configuration, lifeCycle)
37
+ var _startLoggerCollection = startLoggerCollection(lifeCycle)
38
+
39
+ var session = startLogsSessionManager(configuration)
40
+ startLogsAssembly(session, configuration, lifeCycle, getCommonContext, mainLogger, reportError)
41
+
42
+ startLogsBatch(configuration, lifeCycle, reportError)
43
+
44
+ var internalContext = startInternalContext(session)
45
+
46
+ return {
47
+ handleLog: _startLoggerCollection.handleLog,
48
+ getInternalContext: internalContext.get,
49
+ }
50
+ }
51
+
@@ -0,0 +1,101 @@
1
+ import {
2
+ ErrorSource,
3
+ extend2Lev,
4
+ createEventRateLimiter,
5
+ getRelativeTime,
6
+ withSnakeCaseKeys,
7
+ LifeCycleEventType,
8
+ deviceInfo,
9
+ each,
10
+ RumEventType,
11
+ isNullUndefinedDefaultValue
12
+ } from '@cloudcare/browser-core'
13
+
14
+ import { STATUSES, HandlerType } from './logger'
15
+ import { isAuthorized } from './logsCollection/logger/loggerCollection'
16
+
17
+ export function startLogsAssembly(
18
+ sessionManager,
19
+ configuration,
20
+ lifeCycle,
21
+ getCommonContext,
22
+ mainLogger,
23
+ reportError
24
+ ) {
25
+ var statusWithCustom = STATUSES.concat(['custom'])
26
+ var logRateLimiters = {}
27
+ each(statusWithCustom, function(status) {
28
+ logRateLimiters[status] = createEventRateLimiter(status, configuration.eventRateLimiterThreshold, reportError)
29
+ })
30
+ lifeCycle.subscribe(
31
+ LifeCycleEventType.RAW_LOG_COLLECTED,
32
+ function(data) {
33
+ // { rawLogsEvent, messageContext = undefined, savedCommonContext = undefined, logger = mainLogger }
34
+ var rawLogsEvent = data.rawLogsEvent
35
+ var messageContext = data.messageContext || undefined
36
+ var savedCommonContext = data.messageContext || undefined
37
+ var logger = data.logger || mainLogger
38
+ var startTime = getRelativeTime(rawLogsEvent.date)
39
+ var session = sessionManager.findTrackedSession(startTime)
40
+
41
+ if (!session) {
42
+ return
43
+ }
44
+ var commonContext = savedCommonContext || getCommonContext()
45
+ var log = extend2Lev(
46
+ {
47
+ service: configuration.service || 'browser',
48
+ env: configuration.env || '',
49
+ version: configuration.version || '',
50
+ _dd: {
51
+ sdkName: configuration.sdkName,
52
+ sdkVersion: configuration.sdkVersion,
53
+ },
54
+ session: {
55
+ id: session.id
56
+ },
57
+ view: commonContext.view,
58
+ device: deviceInfo,
59
+ type: RumEventType.LOGGER
60
+ },
61
+ commonContext.context,
62
+ getRUMInternalContext(startTime),
63
+ rawLogsEvent,
64
+ logger.getContext(),
65
+ messageContext
66
+ )
67
+
68
+ if (
69
+ // Todo: [RUMF-1230] Move this check to the logger collection in the next major release
70
+ !isAuthorized(rawLogsEvent.status, HandlerType.http, logger) || (configuration.beforeSend && configuration.beforeSend(log) === false)
71
+ ||
72
+ (
73
+ log.error
74
+ &&
75
+ log.error.origin !== ErrorSource.AGENT
76
+ &&
77
+ isNullUndefinedDefaultValue(logRateLimiters[log.status], logRateLimiters['custom']).isLimitReached()
78
+ )
79
+ ) {
80
+ return
81
+ }
82
+
83
+ lifeCycle.notify(LifeCycleEventType.LOG_COLLECTED, withSnakeCaseKeys(log))
84
+ }
85
+ )
86
+ }
87
+
88
+
89
+ export function getRUMInternalContext(startTime) {
90
+ return getInternalContextFromRumGlobal(window.DATAFLUX_RUM)
91
+
92
+ function getInternalContextFromRumGlobal(rumGlobal) {
93
+ if (rumGlobal && rumGlobal.getInternalContext) {
94
+ return rumGlobal.getInternalContext(startTime)
95
+ }
96
+ }
97
+ }
98
+
99
+ export function resetRUMInternalContext() {
100
+ logsSentBeforeRumInjectionTelemetryAdded = false
101
+ }
@@ -0,0 +1,70 @@
1
+ import {
2
+ assign,
3
+ ONE_KIBI_BYTE,
4
+ validateAndBuildConfiguration,
5
+ display,
6
+ unique,
7
+ RawReportType,
8
+ isArray,
9
+ every,
10
+ ConsoleApiName,
11
+ includes,
12
+ values,
13
+ } from '@cloudcare/browser-core'
14
+ import { buildEnv } from '../boot/buildEnv'
15
+ /**
16
+ * arbitrary value, byte precision not needed
17
+ */
18
+ export var DEFAULT_REQUEST_ERROR_RESPONSE_LENGTH_LIMIT = 32 * ONE_KIBI_BYTE
19
+
20
+ export function validateAndBuildLogsConfiguration(
21
+ initConfiguration
22
+ ){
23
+ var baseConfiguration = validateAndBuildConfiguration(initConfiguration)
24
+
25
+ var forwardConsoleLogs = validateAndBuildForwardOption(
26
+ initConfiguration.forwardConsoleLogs,
27
+ values(ConsoleApiName),
28
+ 'Forward Console Logs'
29
+ )
30
+
31
+ var forwardReports = validateAndBuildForwardOption(
32
+ initConfiguration.forwardReports,
33
+ values(RawReportType),
34
+ 'Forward Reports'
35
+ )
36
+
37
+ if (!baseConfiguration || !forwardConsoleLogs || !forwardReports) {
38
+ return
39
+ }
40
+
41
+ if (initConfiguration.forwardErrorsToLogs && !includes(forwardConsoleLogs, ConsoleApiName.error)) {
42
+ forwardConsoleLogs.push(ConsoleApiName.error)
43
+ }
44
+ return assign(
45
+ {
46
+ forwardErrorsToLogs: initConfiguration.forwardErrorsToLogs !== false,
47
+ forwardConsoleLogs: forwardConsoleLogs,
48
+ forwardReports: forwardReports,
49
+ requestErrorResponseLengthLimit: DEFAULT_REQUEST_ERROR_RESPONSE_LENGTH_LIMIT,
50
+ },
51
+ baseConfiguration,
52
+ buildEnv
53
+ )
54
+ }
55
+
56
+ export function validateAndBuildForwardOption(
57
+ option,
58
+ allowedValues,
59
+ label
60
+ ) {
61
+ if (option === undefined) {
62
+ return []
63
+ }
64
+
65
+ if (!(option === 'all' || (isArray(option) && every(option, function(api) { return includes(allowedValues, api) })))) {
66
+ display.error(label + ' should be "all" or an array with allowed values "' + allowedValues.join('", "') + '"')
67
+ return
68
+ }
69
+ return option === 'all' ? allowedValues : unique(option)
70
+ }
@@ -0,0 +1,15 @@
1
+
2
+ export function startInternalContext(sessionManager) {
3
+ return {
4
+ get: function(startTime) {
5
+ var trackedSession = sessionManager.findTrackedSession(startTime)
6
+ if (trackedSession) {
7
+ return {
8
+ session: {
9
+ id: trackedSession.id
10
+ },
11
+ }
12
+ }
13
+ },
14
+ }
15
+ }
@@ -1,36 +1,23 @@
1
- import {
2
- createContextManager,
3
- extend2Lev,
4
- keys,
5
- ErrorSource,
6
- jsonStringify
7
- } from '@cloudcare/browser-core'
1
+ import { deepClone, assign, extend2Lev, createContextManager, ErrorSource, keys } from '@cloudcare/browser-core'
2
+
3
+
8
4
  export var StatusType = {
9
5
  debug: 'debug',
10
- critical: 'critical',
11
6
  error: 'error',
12
7
  info: 'info',
13
- warn: 'warning'
8
+ warn: 'warn',
9
+ critical: 'critical'
14
10
  }
15
- // eslint-disable-next-line @typescript-eslint/no-redeclare
16
11
 
17
- var STATUS_PRIORITIES = {
18
- [StatusType.debug]: 0,
19
- [StatusType.info]: 1,
20
- [StatusType.warn]: 2,
21
- [StatusType.error]: 3,
22
- [StatusType.critical]: 4
23
- }
24
-
25
- export const STATUSES = keys(StatusType)
26
-
27
- export const HandlerType = {
12
+ export var HandlerType = {
28
13
  console: 'console',
29
14
  http: 'http',
30
- silent: 'silent'
15
+ silent: 'silent',
31
16
  }
17
+
18
+ export var STATUSES = keys(StatusType)
32
19
  // eslint-disable-next-line @typescript-eslint/no-redeclare
33
- export function Logger(sendLog, handlerType, level, loggerContext) {
20
+ export function Logger(handleLogStrategy, name, handlerType, level, loggerContext) {
34
21
  this.contextManager = createContextManager()
35
22
  if (typeof handlerType === 'undefined') {
36
23
  handlerType = HandlerType.http
@@ -41,77 +28,69 @@ export function Logger(sendLog, handlerType, level, loggerContext) {
41
28
  if (typeof loggerContext === 'undefined') {
42
29
  loggerContext = {}
43
30
  }
44
- this.sendLog = sendLog
31
+ this.handleLogStrategy = handleLogStrategy
45
32
  this.handlerType = handlerType
46
33
  this.level = level
47
- this.contextManager.set(loggerContext)
34
+ this.contextManager.set(assign({}, loggerContext, name ? { logger: { name: name } } : undefined))
48
35
  }
49
36
  Logger.prototype = {
50
- log: function (message, messageContext, status) {
37
+ log: function(message, messageContext, status) {
51
38
  if (typeof status === 'undefined') {
52
39
  status = StatusType.info
53
40
  }
54
- if (STATUS_PRIORITIES[status] >= STATUS_PRIORITIES[this.level]) {
55
- switch (this.handlerType) {
56
- case HandlerType.http:
57
- var logMessage = extend2Lev(
58
- { message: message, status: status },
59
- { tags: this.contextManager.get() },
60
- messageContext
61
- )
62
- this.sendLog(logMessage)
63
- break
64
- case HandlerType.console:
65
- console.log(
66
- status + ' : ' + message,
67
- extend2Lev(this.contextManager.get(), messageContext)
68
- )
69
- break
70
- case HandlerType.silent:
71
- break
72
- }
73
- }
41
+ this.handleLogStrategy({ message: message, context: deepClone(messageContext), status:status }, this)
74
42
  },
75
- debug: function (message, messageContext) {
43
+
44
+ debug: function(message, messageContext) {
76
45
  this.log(message, messageContext, StatusType.debug)
77
46
  },
78
47
 
79
- info: function (message, messageContext) {
48
+ info: function(message, messageContext) {
80
49
  this.log(message, messageContext, StatusType.info)
81
50
  },
82
51
 
83
- warn: function (message, messageContext) {
52
+ warn: function(message, messageContext) {
84
53
  this.log(message, messageContext, StatusType.warn)
85
54
  },
86
- critical: function (message, messageContext) {
55
+ critical: function(message, messageContext) {
87
56
  this.log(message, messageContext, StatusType.critical)
88
57
  },
89
- error: function (message, messageContext) {
58
+ error: function(message, messageContext) {
90
59
  var errorOrigin = {
91
60
  error: {
92
- origin: ErrorSource.LOGGER
93
- }
61
+ origin: ErrorSource.LOGGER,
62
+ },
94
63
  }
95
64
  this.log(message, extend2Lev(errorOrigin, messageContext), StatusType.error)
96
65
  },
97
66
 
98
- setContext: function (context) {
67
+ setContext:function(context) {
99
68
  this.contextManager.set(context)
100
69
  },
101
70
 
102
- addContext: function (key, value) {
71
+ getContext: function() {
72
+ return this.contextManager.get()
73
+ },
74
+
75
+ addContext: function(key, value) {
103
76
  this.contextManager.add(key, value)
104
77
  },
105
78
 
106
- removeContext: function (key) {
79
+ removeContext: function(key) {
107
80
  this.contextManager.remove(key)
108
81
  },
109
82
 
110
- setHandler: function (handler) {
83
+ setHandler:function(handler) {
111
84
  this.handlerType = handler
112
85
  },
113
86
 
114
- setLevel: function (level) {
87
+ getHandler: function() {
88
+ return this.handlerType
89
+ },
90
+ setLevel: function(level) {
115
91
  this.level = level
92
+ },
93
+ getLevel: function() {
94
+ return this.level
116
95
  }
117
96
  }