@beforeyoubid/logger-adapter 1.0.11 → 2.0.0

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 (50) hide show
  1. package/README.md +55 -127
  2. package/dist/console/logWrapper.d.ts +4 -0
  3. package/dist/index.d.ts +2 -1
  4. package/dist/logger/index.d.ts +1 -1
  5. package/dist/logger/logdnaClient.d.ts +19 -0
  6. package/dist/logger/logger.d.ts +12 -2
  7. package/dist/logger/util.d.ts +11 -0
  8. package/dist/params.d.ts +1 -1
  9. package/dist/src/__tests__/params.spec.js +84 -0
  10. package/dist/src/console/__tests__/consoleLogger.spec.js +18 -0
  11. package/dist/src/console/index.js +44 -0
  12. package/dist/src/console/logWrapper.js +99 -0
  13. package/dist/src/consts.js +13 -0
  14. package/dist/src/index.js +40 -0
  15. package/dist/src/logger/__tests__/index.spec.js +24 -0
  16. package/dist/src/logger/__tests__/logger.spec.js +139 -0
  17. package/dist/src/logger/__tests__/util.spec.js +81 -0
  18. package/dist/src/logger/index.js +17 -0
  19. package/dist/src/logger/logdnaClient.js +47 -0
  20. package/dist/src/logger/logger.js +61 -0
  21. package/dist/src/logger/util.js +57 -0
  22. package/dist/src/params.js +66 -0
  23. package/dist/src/transports/logdnaWinston.js +267 -0
  24. package/dist/src/types.js +6 -0
  25. package/dist/src/util.js +328 -0
  26. package/dist/transports/logdnaWinston.d.ts +28 -0
  27. package/dist/types.d.ts +5 -5
  28. package/dist/util.d.ts +1 -0
  29. package/package.json +35 -24
  30. package/dist/console/index.js +0 -29
  31. package/dist/console/logger.d.ts +0 -4
  32. package/dist/console/logger.js +0 -51
  33. package/dist/consts.js +0 -5
  34. package/dist/flush.d.ts +0 -29
  35. package/dist/flush.js +0 -80
  36. package/dist/index.js +0 -13
  37. package/dist/logger/index.js +0 -8
  38. package/dist/logger/logger.js +0 -32
  39. package/dist/params.js +0 -40
  40. package/dist/types.js +0 -2
  41. package/dist/util.js +0 -96
  42. package/src/console/index.ts +0 -33
  43. package/src/console/logger.ts +0 -62
  44. package/src/consts.ts +0 -3
  45. package/src/index.ts +0 -5
  46. package/src/logger/index.ts +0 -7
  47. package/src/logger/logger.ts +0 -41
  48. package/src/params.ts +0 -53
  49. package/src/types.ts +0 -23
  50. package/src/util.ts +0 -89
package/src/params.ts DELETED
@@ -1,53 +0,0 @@
1
- import { ILogDNAParams } from './types';
2
-
3
- const getEnvs = (env = process?.env || {}) => {
4
- const {
5
- LOG_LEVEL = 'info',
6
- LOGDNA_KEY = '',
7
- STAGE = 'n/a',
8
- AWS_LAMBDA_FUNCTION_NAME = 'n/a',
9
- LOGDNA_ENABLED = 'true',
10
- LOGDNA_SUPPRESS_FLUSH_ALL = 'false',
11
- } = env;
12
-
13
- return {
14
- LOG_LEVEL,
15
- LOGDNA_KEY,
16
- STAGE,
17
- AWS_LAMBDA_FUNCTION_NAME,
18
- LOGDNA_ENABLED,
19
- LOGDNA_SUPPRESS_FLUSH_ALL,
20
- };
21
- };
22
-
23
- const sendToRemote = (logDNAKey: string, logDNAEnabled: string): boolean => {
24
- return !!logDNAKey && logDNAEnabled === 'true';
25
- };
26
-
27
- const getLogParams = (env = process?.env || {}): ILogDNAParams => {
28
- const { LOG_LEVEL, LOGDNA_KEY, STAGE, AWS_LAMBDA_FUNCTION_NAME, LOGDNA_ENABLED, LOGDNA_SUPPRESS_FLUSH_ALL } = getEnvs(
29
- env
30
- );
31
-
32
- const logDNAOptions = {
33
- key: LOGDNA_KEY,
34
- env: STAGE,
35
- app: AWS_LAMBDA_FUNCTION_NAME,
36
- hostname: STAGE,
37
- index_meta: true,
38
- tags: [AWS_LAMBDA_FUNCTION_NAME, STAGE],
39
- level: LOG_LEVEL,
40
- handleExceptions: true,
41
- };
42
-
43
- return {
44
- logLevel: LOG_LEVEL,
45
- logDNAKey: LOGDNA_KEY,
46
- sendToRemote: sendToRemote(LOGDNA_KEY, LOGDNA_ENABLED),
47
- logDNASuppressFlushAll: LOGDNA_SUPPRESS_FLUSH_ALL === 'true',
48
- functionName: AWS_LAMBDA_FUNCTION_NAME,
49
- logDNAOptions,
50
- };
51
- };
52
-
53
- export { getLogParams, sendToRemote };
package/src/types.ts DELETED
@@ -1,23 +0,0 @@
1
- export type EmptyCBFunction = () => void;
2
- export type CallbackFunction = (err: any, data: any) => void;
3
- export type LambdaHandlerWithAsyncFunction = (event: any, context: any) => Promise<any>;
4
- export type LambdaHandlerWithCallbackFunction = (event: any, context: any, fn: CallbackFunction) => void;
5
- export interface ILogDNALogOptions {
6
- key: string;
7
- env: string;
8
- app: string;
9
- hostname: string;
10
- index_meta: boolean;
11
- tags: Array<string>;
12
- level: string;
13
- handleExceptions: boolean;
14
- }
15
-
16
- export interface ILogDNAParams {
17
- logLevel: string;
18
- logDNAKey: string;
19
- sendToRemote: boolean;
20
- logDNASuppressFlushAll: boolean;
21
- functionName: string;
22
- logDNAOptions: ILogDNALogOptions;
23
- }
package/src/util.ts DELETED
@@ -1,89 +0,0 @@
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
-
11
- /**
12
- * Handy function to check if we are sending to LogDNA
13
- * @param logDNAKey
14
- * @param sendToRemote
15
- */
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 };