@beforeyoubid/logger-adapter 1.0.7 → 1.0.11
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.
- package/dist/console/index.js +1 -2
- package/dist/console/logger.js +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +5 -6
- package/dist/logger/index.d.ts +1 -2
- package/dist/logger/index.js +1 -3
- package/dist/logger/logger.d.ts +1 -2
- package/dist/logger/logger.js +2 -6
- package/dist/params.js +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/util.d.ts +22 -1
- package/dist/util.js +89 -2
- package/package.json +5 -2
- package/src/console/index.ts +1 -2
- package/src/console/logger.ts +1 -1
- package/src/index.ts +3 -3
- package/src/logger/index.ts +2 -3
- package/src/logger/logger.ts +2 -7
- package/src/params.ts +1 -0
- package/src/types.ts +1 -0
- package/src/util.ts +84 -1
- package/src/flush.ts +0 -77
package/dist/console/index.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.consoleLogger = void 0;
|
|
|
4
4
|
const params_1 = require("../params");
|
|
5
5
|
const logger_1 = require("./logger");
|
|
6
6
|
const util_1 = require("../util");
|
|
7
|
-
const flush_1 = require("../flush");
|
|
8
7
|
let loggerObject;
|
|
9
8
|
const logParams = params_1.getLogParams();
|
|
10
9
|
/**
|
|
@@ -25,6 +24,6 @@ const init = (params = logParams) => {
|
|
|
25
24
|
};
|
|
26
25
|
const consoleLogger = {
|
|
27
26
|
init,
|
|
28
|
-
flushAll:
|
|
27
|
+
flushAll: util_1.flushAll,
|
|
29
28
|
};
|
|
30
29
|
exports.consoleLogger = consoleLogger;
|
package/dist/console/logger.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getDefaultConsoleLog = exports.getLogger = void 0;
|
|
4
|
-
const LogDNALogger = require('
|
|
4
|
+
const LogDNALogger = require('logdna');
|
|
5
5
|
const { flushAll } = require('../util');
|
|
6
6
|
let logger;
|
|
7
7
|
// Default console
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { consoleLogger } from './console';
|
|
2
|
-
import { logger
|
|
2
|
+
import { logger } from './logger';
|
|
3
3
|
import { getLogParams } from './params';
|
|
4
|
-
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './
|
|
5
|
-
export {
|
|
4
|
+
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './util';
|
|
5
|
+
export { consoleLogger, logger, getLogParams, flushAll, ensureFlushAll, ensureFlushAllCallback };
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ensureFlushAllCallback = exports.ensureFlushAll = exports.flushAll = exports.getLogParams = exports.logger = exports.consoleLogger =
|
|
3
|
+
exports.ensureFlushAllCallback = exports.ensureFlushAll = exports.flushAll = exports.getLogParams = exports.logger = exports.consoleLogger = void 0;
|
|
4
4
|
const console_1 = require("./console");
|
|
5
5
|
Object.defineProperty(exports, "consoleLogger", { enumerable: true, get: function () { return console_1.consoleLogger; } });
|
|
6
6
|
const logger_1 = require("./logger");
|
|
7
7
|
Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return logger_1.logger; } });
|
|
8
|
-
Object.defineProperty(exports, "logdnaTransport", { enumerable: true, get: function () { return logger_1.logdnaTransport; } });
|
|
9
8
|
const params_1 = require("./params");
|
|
10
9
|
Object.defineProperty(exports, "getLogParams", { enumerable: true, get: function () { return params_1.getLogParams; } });
|
|
11
|
-
const
|
|
12
|
-
Object.defineProperty(exports, "flushAll", { enumerable: true, get: function () { return
|
|
13
|
-
Object.defineProperty(exports, "ensureFlushAll", { enumerable: true, get: function () { return
|
|
14
|
-
Object.defineProperty(exports, "ensureFlushAllCallback", { enumerable: true, get: function () { return
|
|
10
|
+
const util_1 = require("./util");
|
|
11
|
+
Object.defineProperty(exports, "flushAll", { enumerable: true, get: function () { return util_1.flushAll; } });
|
|
12
|
+
Object.defineProperty(exports, "ensureFlushAll", { enumerable: true, get: function () { return util_1.ensureFlushAll; } });
|
|
13
|
+
Object.defineProperty(exports, "ensureFlushAllCallback", { enumerable: true, get: function () { return util_1.ensureFlushAllCallback; } });
|
package/dist/logger/index.d.ts
CHANGED
package/dist/logger/index.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.logger = void 0;
|
|
4
4
|
const params_1 = require("../params");
|
|
5
5
|
const logger_1 = require("./logger");
|
|
6
6
|
const logParams = params_1.getLogParams();
|
|
7
7
|
const logger = logger_1.getLogger(logParams);
|
|
8
8
|
exports.logger = logger;
|
|
9
|
-
const logdnaTransport = logger_1.getLogdnaTransport();
|
|
10
|
-
exports.logdnaTransport = logdnaTransport;
|
package/dist/logger/logger.d.ts
CHANGED
package/dist/logger/logger.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getLogger = void 0;
|
|
4
4
|
const logdnaWinston = require('logdna-winston');
|
|
5
5
|
const winston = require('winston');
|
|
6
6
|
const { isLogDNAEnabled } = require('../util');
|
|
@@ -17,14 +17,12 @@ const createWinstonLogger = (logLevel) => {
|
|
|
17
17
|
});
|
|
18
18
|
return logger;
|
|
19
19
|
};
|
|
20
|
-
let logdnaTransport;
|
|
21
20
|
const getLogger = (params) => {
|
|
22
21
|
const { logLevel, logDNAOptions, logDNAKey, sendToRemote } = params;
|
|
23
22
|
const isEnabled = isLogDNAEnabled(logDNAKey, sendToRemote);
|
|
24
23
|
const logger = createWinstonLogger(logLevel);
|
|
25
|
-
logdnaTransport = new logdnaWinston(logDNAOptions);
|
|
26
24
|
if (isEnabled) {
|
|
27
|
-
logger.add(
|
|
25
|
+
logger.add(new logdnaWinston(logDNAOptions));
|
|
28
26
|
}
|
|
29
27
|
else {
|
|
30
28
|
logger.info(`LOGDNA is disabled`);
|
|
@@ -32,5 +30,3 @@ const getLogger = (params) => {
|
|
|
32
30
|
return logger;
|
|
33
31
|
};
|
|
34
32
|
exports.getLogger = getLogger;
|
|
35
|
-
const getLogdnaTransport = () => logdnaTransport;
|
|
36
|
-
exports.getLogdnaTransport = getLogdnaTransport;
|
package/dist/params.js
CHANGED
package/dist/types.d.ts
CHANGED
package/dist/util.d.ts
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
|
+
import { LambdaHandlerWithAsyncFunction, LambdaHandlerWithCallbackFunction } from './types';
|
|
1
2
|
/**
|
|
2
3
|
* Handy function to check if we are sending to LogDNA
|
|
3
4
|
* @param logDNAKey
|
|
4
5
|
* @param sendToRemote
|
|
5
6
|
*/
|
|
6
|
-
|
|
7
|
+
declare const isLogDNAEnabled: (logDNAKey: string, sendToRemote: boolean) => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Send all log messages to LogDNA before lambda is terminated
|
|
10
|
+
*/
|
|
11
|
+
declare const flushAll: () => Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Lambda wrapper to ensure we flush messages before returning the response
|
|
14
|
+
* @param lambdaHandler
|
|
15
|
+
*/
|
|
16
|
+
declare const ensureFlushAll: (lambdaHandler: LambdaHandlerWithAsyncFunction) => LambdaHandlerWithAsyncFunction;
|
|
17
|
+
/**
|
|
18
|
+
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated
|
|
19
|
+
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed.
|
|
20
|
+
* This means after a callback function is called, a Lambda function still run
|
|
21
|
+
* and we can catch the final clean up.
|
|
22
|
+
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
|
|
23
|
+
*
|
|
24
|
+
* @param lambdaHandler
|
|
25
|
+
*/
|
|
26
|
+
declare const ensureFlushAllCallback: (lambdaHandler: LambdaHandlerWithCallbackFunction) => LambdaHandlerWithCallbackFunction;
|
|
27
|
+
export { isLogDNAEnabled, flushAll, ensureFlushAll, ensureFlushAllCallback };
|
package/dist/util.js
CHANGED
|
@@ -1,9 +1,96 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isLogDNAEnabled = void 0;
|
|
12
|
+
exports.ensureFlushAllCallback = exports.ensureFlushAll = exports.flushAll = exports.isLogDNAEnabled = void 0;
|
|
13
|
+
const LogDNALogger = require('logdna');
|
|
14
|
+
const debug = require('util').debuglog('logger-adapter');
|
|
15
|
+
const { FALLBACK_TIMEOUT_AFTER_FLUSHING } = require('./consts');
|
|
16
|
+
const { getLogParams } = require('./params');
|
|
17
|
+
// timeout
|
|
18
|
+
let timeout;
|
|
4
19
|
/**
|
|
5
20
|
* Handy function to check if we are sending to LogDNA
|
|
6
21
|
* @param logDNAKey
|
|
7
22
|
* @param sendToRemote
|
|
8
23
|
*/
|
|
9
|
-
|
|
24
|
+
const isLogDNAEnabled = (logDNAKey, sendToRemote) => !!logDNAKey && sendToRemote;
|
|
25
|
+
exports.isLogDNAEnabled = isLogDNAEnabled;
|
|
26
|
+
/**
|
|
27
|
+
* REturn if we need to suppress flushAll on a local machine
|
|
28
|
+
*/
|
|
29
|
+
const suppressFlushAll = () => { var _a; return ((_a = getLogParams()) === null || _a === void 0 ? void 0 : _a.logDNASuppressFlushAll) || false; };
|
|
30
|
+
/**
|
|
31
|
+
* Send all log messages to LogDNA before lambda is terminated
|
|
32
|
+
*/
|
|
33
|
+
const flushAll = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
return new Promise(resolve => {
|
|
35
|
+
debug('Flushing all logs...');
|
|
36
|
+
LogDNALogger.flushAll(() => {
|
|
37
|
+
debug('Completed flushing all log messages');
|
|
38
|
+
if (timeout) {
|
|
39
|
+
clearTimeout(timeout);
|
|
40
|
+
}
|
|
41
|
+
resolve();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
exports.flushAll = flushAll;
|
|
46
|
+
/**
|
|
47
|
+
* Fallback promise just in
|
|
48
|
+
*/
|
|
49
|
+
const timeoutPromise = () => {
|
|
50
|
+
return new Promise(resolve => {
|
|
51
|
+
timeout = setTimeout(() => {
|
|
52
|
+
debug('Flushing timeout has reached. Ignore flushing messages...');
|
|
53
|
+
resolve();
|
|
54
|
+
}, FALLBACK_TIMEOUT_AFTER_FLUSHING);
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Lambda wrapper to ensure we flush messages before returning the response
|
|
59
|
+
* @param lambdaHandler
|
|
60
|
+
*/
|
|
61
|
+
const ensureFlushAll = (lambdaHandler) => {
|
|
62
|
+
return (event, context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
try {
|
|
64
|
+
const result = yield lambdaHandler(event, context);
|
|
65
|
+
if (!suppressFlushAll())
|
|
66
|
+
yield Promise.race([flushAll(), timeoutPromise()]);
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
if (!suppressFlushAll())
|
|
71
|
+
yield Promise.race([flushAll(), timeoutPromise()]);
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
exports.ensureFlushAll = ensureFlushAll;
|
|
77
|
+
/**
|
|
78
|
+
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated
|
|
79
|
+
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed.
|
|
80
|
+
* This means after a callback function is called, a Lambda function still run
|
|
81
|
+
* and we can catch the final clean up.
|
|
82
|
+
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
|
|
83
|
+
*
|
|
84
|
+
* @param lambdaHandler
|
|
85
|
+
*/
|
|
86
|
+
const ensureFlushAllCallback = (lambdaHandler) => {
|
|
87
|
+
return (event, context, callback) => {
|
|
88
|
+
const thisCallback = (error, result) => {
|
|
89
|
+
callback(error, result);
|
|
90
|
+
if (!suppressFlushAll())
|
|
91
|
+
flushAll();
|
|
92
|
+
};
|
|
93
|
+
lambdaHandler(event, context, thisCallback);
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
exports.ensureFlushAllCallback = ensureFlushAllCallback;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@beforeyoubid/logger-adapter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "A platform logger module to send the log messages to LogDNA.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/beforeyoubid/npm-logger-adapter#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"
|
|
33
|
+
"logdna": "^3.5.2",
|
|
34
34
|
"logdna-winston": "^2.3.2",
|
|
35
35
|
"winston": "^3.3.3"
|
|
36
36
|
},
|
|
@@ -46,5 +46,8 @@
|
|
|
46
46
|
"prettier": "^2.0.5",
|
|
47
47
|
"ts-jest": "^26.2.0",
|
|
48
48
|
"typescript": "^3.9.7"
|
|
49
|
+
},
|
|
50
|
+
"resolutions": {
|
|
51
|
+
"colors": "1.4.0"
|
|
49
52
|
}
|
|
50
53
|
}
|
package/src/console/index.ts
CHANGED
|
@@ -2,8 +2,7 @@ import { ILogDNAParams } from '../types';
|
|
|
2
2
|
|
|
3
3
|
import { getLogParams } from '../params';
|
|
4
4
|
import { getLogger } from './logger';
|
|
5
|
-
import { isLogDNAEnabled } from '../util';
|
|
6
|
-
import { flushAll } from '../flush';
|
|
5
|
+
import { isLogDNAEnabled, flushAll } from '../util';
|
|
7
6
|
|
|
8
7
|
let loggerObject: any;
|
|
9
8
|
const logParams = getLogParams();
|
package/src/console/logger.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { consoleLogger } from './console';
|
|
2
|
-
import { logger
|
|
2
|
+
import { logger } from './logger';
|
|
3
3
|
import { getLogParams } from './params';
|
|
4
|
-
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './
|
|
5
|
-
export {
|
|
4
|
+
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './util';
|
|
5
|
+
export { consoleLogger, logger, getLogParams, flushAll, ensureFlushAll, ensureFlushAllCallback };
|
package/src/logger/index.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { getLogParams } from '../params';
|
|
2
|
-
import { getLogger
|
|
2
|
+
import { getLogger } from './logger';
|
|
3
3
|
|
|
4
4
|
const logParams = getLogParams();
|
|
5
5
|
const logger = getLogger(logParams);
|
|
6
|
-
const logdnaTransport = getLogdnaTransport();
|
|
7
6
|
|
|
8
|
-
export { logger
|
|
7
|
+
export { logger };
|
package/src/logger/logger.ts
CHANGED
|
@@ -26,21 +26,16 @@ const createWinstonLogger = (logLevel: string) => {
|
|
|
26
26
|
return logger;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
let logdnaTransport: any;
|
|
30
|
-
|
|
31
29
|
const getLogger = (params: ILogDNAParams): any => {
|
|
32
30
|
const { logLevel, logDNAOptions, logDNAKey, sendToRemote } = params;
|
|
33
31
|
const isEnabled = isLogDNAEnabled(logDNAKey, sendToRemote);
|
|
34
32
|
const logger = createWinstonLogger(logLevel);
|
|
35
|
-
logdnaTransport = new logdnaWinston(logDNAOptions);
|
|
36
33
|
if (isEnabled) {
|
|
37
|
-
logger.add(
|
|
34
|
+
logger.add(new logdnaWinston(logDNAOptions));
|
|
38
35
|
} else {
|
|
39
36
|
logger.info(`LOGDNA is disabled`);
|
|
40
37
|
}
|
|
41
38
|
return logger;
|
|
42
39
|
};
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
export { getLogger, getLogdnaTransport };
|
|
41
|
+
export { getLogger };
|
package/src/params.ts
CHANGED
package/src/types.ts
CHANGED
package/src/util.ts
CHANGED
|
@@ -1,6 +1,89 @@
|
|
|
1
|
+
import { LambdaHandlerWithAsyncFunction, LambdaHandlerWithCallbackFunction, CallbackFunction } from './types';
|
|
2
|
+
|
|
3
|
+
const LogDNALogger = require('logdna');
|
|
4
|
+
const debug = require('util').debuglog('logger-adapter');
|
|
5
|
+
const { FALLBACK_TIMEOUT_AFTER_FLUSHING } = require('./consts');
|
|
6
|
+
const { getLogParams } = require('./params');
|
|
7
|
+
|
|
8
|
+
// timeout
|
|
9
|
+
let timeout: NodeJS.Timeout;
|
|
10
|
+
|
|
1
11
|
/**
|
|
2
12
|
* Handy function to check if we are sending to LogDNA
|
|
3
13
|
* @param logDNAKey
|
|
4
14
|
* @param sendToRemote
|
|
5
15
|
*/
|
|
6
|
-
|
|
16
|
+
const isLogDNAEnabled = (logDNAKey: string, sendToRemote: boolean): boolean => !!logDNAKey && sendToRemote;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* REturn if we need to suppress flushAll on a local machine
|
|
20
|
+
*/
|
|
21
|
+
const suppressFlushAll = (): boolean => getLogParams()?.logDNASuppressFlushAll || false;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Send all log messages to LogDNA before lambda is terminated
|
|
25
|
+
*/
|
|
26
|
+
const flushAll = async (): Promise<void> => {
|
|
27
|
+
return new Promise(resolve => {
|
|
28
|
+
debug('Flushing all logs...');
|
|
29
|
+
LogDNALogger.flushAll(() => {
|
|
30
|
+
debug('Completed flushing all log messages');
|
|
31
|
+
if (timeout) {
|
|
32
|
+
clearTimeout(timeout);
|
|
33
|
+
}
|
|
34
|
+
resolve();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Fallback promise just in
|
|
41
|
+
*/
|
|
42
|
+
const timeoutPromise = () => {
|
|
43
|
+
return new Promise(resolve => {
|
|
44
|
+
timeout = setTimeout(() => {
|
|
45
|
+
debug('Flushing timeout has reached. Ignore flushing messages...');
|
|
46
|
+
resolve();
|
|
47
|
+
}, FALLBACK_TIMEOUT_AFTER_FLUSHING);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Lambda wrapper to ensure we flush messages before returning the response
|
|
53
|
+
* @param lambdaHandler
|
|
54
|
+
*/
|
|
55
|
+
const ensureFlushAll = (lambdaHandler: LambdaHandlerWithAsyncFunction): LambdaHandlerWithAsyncFunction => {
|
|
56
|
+
return async (event: any, context: any): Promise<any> => {
|
|
57
|
+
try {
|
|
58
|
+
const result = await lambdaHandler(event, context);
|
|
59
|
+
if (!suppressFlushAll()) await Promise.race([flushAll(), timeoutPromise()]);
|
|
60
|
+
return result;
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (!suppressFlushAll()) await Promise.race([flushAll(), timeoutPromise()]);
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated
|
|
70
|
+
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed.
|
|
71
|
+
* This means after a callback function is called, a Lambda function still run
|
|
72
|
+
* and we can catch the final clean up.
|
|
73
|
+
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
|
|
74
|
+
*
|
|
75
|
+
* @param lambdaHandler
|
|
76
|
+
*/
|
|
77
|
+
const ensureFlushAllCallback = (
|
|
78
|
+
lambdaHandler: LambdaHandlerWithCallbackFunction
|
|
79
|
+
): LambdaHandlerWithCallbackFunction => {
|
|
80
|
+
return (event: any, context: any, callback: CallbackFunction): void => {
|
|
81
|
+
const thisCallback = (error: any, result: any): void => {
|
|
82
|
+
callback(error, result);
|
|
83
|
+
if (!suppressFlushAll()) flushAll();
|
|
84
|
+
};
|
|
85
|
+
lambdaHandler(event, context, thisCallback);
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export { isLogDNAEnabled, flushAll, ensureFlushAll, ensureFlushAllCallback };
|
package/src/flush.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { LambdaHandlerWithAsyncFunction, LambdaHandlerWithCallbackFunction, CallbackFunction } from './types';
|
|
2
|
-
import { logdnaTransport } from './logger';
|
|
3
|
-
|
|
4
|
-
const debug = require('util').debuglog('logger-adapter');
|
|
5
|
-
const { once } = require('events')
|
|
6
|
-
const { FALLBACK_TIMEOUT_AFTER_FLUSHING } = require('./consts');
|
|
7
|
-
const { getLogParams } = require('./params');
|
|
8
|
-
|
|
9
|
-
// timeout
|
|
10
|
-
let timeout: NodeJS.Timeout;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* REturn if we need to suppress flushAll on a local machine
|
|
14
|
-
*/
|
|
15
|
-
export const suppressFlushAll = (): boolean => getLogParams()?.logDNASuppressFlushAll || false;
|
|
16
|
-
|
|
17
|
-
export const sleep = (time: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, time));
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Send all log messages to LogDNA before lambda is terminated
|
|
21
|
-
*/
|
|
22
|
-
export const flushAll = (): Promise<void> => {
|
|
23
|
-
debug('Flushing all logs...');
|
|
24
|
-
return new Promise(resolve =>
|
|
25
|
-
logdnaTransport.Logger.flushAll(resolve)
|
|
26
|
-
);
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Fallback promise just in
|
|
31
|
-
*/
|
|
32
|
-
export const timeoutPromise = () => {
|
|
33
|
-
return new Promise(resolve => {
|
|
34
|
-
timeout = setTimeout(() => {
|
|
35
|
-
debug('Flushing timeout has reached. Ignore flushing messages...');
|
|
36
|
-
resolve();
|
|
37
|
-
}, FALLBACK_TIMEOUT_AFTER_FLUSHING);
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Lambda wrapper to ensure we flush messages before returning the response
|
|
43
|
-
* @param lambdaHandler
|
|
44
|
-
*/
|
|
45
|
-
export const ensureFlushAll = (lambdaHandler: LambdaHandlerWithAsyncFunction): LambdaHandlerWithAsyncFunction => {
|
|
46
|
-
return async (event: any, context: any): Promise<any> => {
|
|
47
|
-
try {
|
|
48
|
-
const result = await lambdaHandler(event, context);
|
|
49
|
-
if (!suppressFlushAll()) await Promise.race([flushAll(), timeoutPromise()]);
|
|
50
|
-
return result;
|
|
51
|
-
} catch (e) {
|
|
52
|
-
if (!suppressFlushAll()) await Promise.race([flushAll(), timeoutPromise()]);
|
|
53
|
-
throw e;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated
|
|
60
|
-
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed.
|
|
61
|
-
* This means after a callback function is called, a Lambda function still run
|
|
62
|
-
* and we can catch the final clean up.
|
|
63
|
-
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
|
|
64
|
-
*
|
|
65
|
-
* @param lambdaHandler
|
|
66
|
-
*/
|
|
67
|
-
export const ensureFlushAllCallback = (
|
|
68
|
-
lambdaHandler: LambdaHandlerWithCallbackFunction
|
|
69
|
-
): LambdaHandlerWithCallbackFunction => {
|
|
70
|
-
return (event: any, context: any, callback: CallbackFunction): void => {
|
|
71
|
-
const thisCallback = (error: any, result: any): void => {
|
|
72
|
-
callback(error, result);
|
|
73
|
-
if (!suppressFlushAll()) flushAll();
|
|
74
|
-
};
|
|
75
|
-
lambdaHandler(event, context, thisCallback);
|
|
76
|
-
};
|
|
77
|
-
};
|