-
Notifications
You must be signed in to change notification settings - Fork 64
/
interface.js
110 lines (99 loc) · 3.01 KB
/
interface.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* #### Import members from **@edx/frontend-platform/logging**
*
* Contains a shared interface for logging information. (The default implementation is in
* NewRelicLoggingService.js.) When in development mode, all messages will instead be sent to the console.
*
* The `initialize` function performs much of the logging configuration for you. If, however,
* you're not using the `initialize` function, logging (via New Relic) can be configured via:
*
* ```
* import { configure, NewRelicLoggingService, logInfo, logError } from '@edx/frontend-platform/logging';
* import { geConfig } from '@edx/frontend-platform';
*
* configureLogging(NewRelicLoggingService, {
* config: getConfig(),
* });
*
* logInfo('Just so you know...');
* logInfo(new Error('Unimportant error'), { type: 'unimportant' });
* logError('Uhoh!');
* logError(new Error('Uhoh error!'));
* ```
*
* As shown in this example, logging depends on the configuration document.
*
* @module Logging
*/
import PropTypes from 'prop-types';
const optionsShape = {
config: PropTypes.object.isRequired,
};
const serviceShape = {
logInfo: PropTypes.func.isRequired,
logError: PropTypes.func.isRequired,
};
let service = null;
/**
*
*/
export function configure(LoggingService, options) {
PropTypes.checkPropTypes(optionsShape, options, 'property', 'Logging');
service = new LoggingService(options);
PropTypes.checkPropTypes(serviceShape, service, 'property', 'LoggingService');
return service;
}
/**
* Logs a message to the 'info' log level. Can accept custom attributes as a property of the error
* object, or as an optional second parameter.
*
* @param {string|Error} infoStringOrErrorObject
* @param {Object} [customAttributes={}]
*/
export function logInfo(infoStringOrErrorObject, customAttributes) {
return service.logInfo(infoStringOrErrorObject, customAttributes);
}
/**
* Logs a message to the 'error' log level. Can accept custom attributes as a property of the error
* object, or as an optional second parameter.
*
* @param {string|Error} errorStringOrObject
* @param {Object} [customAttributes={}]
*/
export function logError(errorStringOrObject, customAttributes) {
return service.logError(errorStringOrObject, customAttributes);
}
/**
* Sets a custom attribute that will be included with all subsequent log messages.
*
* @param {string} name
* @param {string|number|null} value
*/
export function setCustomAttribute(name, value) {
return service.setCustomAttribute(name, value);
}
/**
*
* @throws {Error} Thrown if the logging service has not yet been configured via {@link configure}.
* @returns {LoggingService}
*/
export function getLoggingService() {
if (!service) {
throw Error('You must first configure the logging service.');
}
return service;
}
/**
* Sets the configured logging service back to null.
*
*/
export function resetLoggingService() {
service = null;
}
/**
* @name LoggingService
* @interface
* @memberof module:Logging
* @property {function} logError
* @property {function} logInfo
*/