-
Notifications
You must be signed in to change notification settings - Fork 2
/
moduleLogger.js
72 lines (64 loc) · 1.62 KB
/
moduleLogger.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
const { createChildLogger, hashValue, logger } = require('@advana/logger');
const { API_AUTH_LOG_LEVEL, SSO_DISABLED } = require('./environment');
const loggerOptions = {
source: 'api-auth',
level: API_AUTH_LOG_LEVEL ?? logger.level,
};
/**
* A child instance of the default logger for logging module events.
* @internal
* @since 2.6.0
*/
const moduleLogger = createChildLogger(logger, loggerOptions);
/**
* Gets a child instance of the request logger for logging module events.
* @internal
* @since 2.6.0
*/
const getRequestLogger = (req) => {
if (!req.log) {
return moduleLogger;
}
return createChildLogger(req.log, loggerOptions);
};
/**
* Creates an `auth` logging context with basic information, and adds the req
* logging context if not provided by httpLogger.
* @returns The logging context
* @internal
* @since 2.6.0
*/
const createLoggingContext = (req) => {
const context = {
auth: {
authenticated: SSO_DISABLED
? SSO_DISABLED
: req.isAuthenticated && req.isAuthenticated(),
},
};
if (SSO_DISABLED) context.auth.sso = 'disabled';
// if req.log exists, it means we're using httpLogger, which should already
// log all of this data. use the same structure for consistency, but don't
// create a duplicate req object.
if (!req.log) {
const sessionID = hashValue(req.sessionID);
context.req = {
id: req.id,
remoteAddress: req.remoteAddress,
sessionID,
session: {
id: sessionID,
user: {
id: req.session?.user?.id,
disabled: req.session?.user?.disabled,
},
},
};
}
return context;
};
module.exports = {
createLoggingContext,
getRequestLogger,
moduleLogger,
};