@beforeyoubid/logger-adapter
Advanced tools
Comparing version 0.0.3 to 0.0.4
declare const consoleLogger: { | ||
init: (params?: ILogDNAParams) => void; | ||
flushAll: (cb?: EmptyCBFunction | undefined) => void; | ||
}; | ||
export { consoleLogger }; |
@@ -22,12 +22,5 @@ "use strict"; | ||
}; | ||
const flushAll = (cb) => { | ||
// If available, means we can flush log message | ||
if (!!loggerObject.flushAll) { | ||
loggerObject.flushAll(cb); | ||
} | ||
}; | ||
const consoleLogger = { | ||
init, | ||
flushAll, | ||
}; | ||
exports.consoleLogger = consoleLogger; |
import { consoleLogger } from './console'; | ||
import { logger } from './logger'; | ||
import { getLogParams } from './params'; | ||
import { ensureFlushAll } from './util'; | ||
export { consoleLogger, logger, getLogParams, ensureFlushAll }; | ||
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './util'; | ||
export { consoleLogger, logger, getLogParams, flushAll, ensureFlushAll, ensureFlushAllCallback }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ensureFlushAll = exports.getLogParams = exports.logger = exports.consoleLogger = void 0; | ||
exports.ensureFlushAllCallback = exports.ensureFlushAll = exports.flushAll = exports.getLogParams = exports.logger = exports.consoleLogger = void 0; | ||
const console_1 = require("./console"); | ||
@@ -11,2 +11,4 @@ Object.defineProperty(exports, "consoleLogger", { enumerable: true, get: function () { return console_1.consoleLogger; } }); | ||
const util_1 = require("./util"); | ||
Object.defineProperty(exports, "flushAll", { enumerable: true, get: function () { return util_1.flushAll; } }); | ||
Object.defineProperty(exports, "ensureFlushAll", { enumerable: true, get: function () { return util_1.ensureFlushAll; } }); | ||
Object.defineProperty(exports, "ensureFlushAllCallback", { enumerable: true, get: function () { return util_1.ensureFlushAllCallback; } }); |
@@ -12,6 +12,16 @@ /** | ||
/** | ||
* Lambda wrapper to ensure we flush message before returning the response | ||
* Lambda wrapper to ensure we flush messages before returning the response | ||
* @param lambdaHandler | ||
*/ | ||
declare const ensureFlushAll: (lambdaHandler: any) => (event: any, context: any) => Promise<any>; | ||
export { isLogDNAEnabled, flushAll, ensureFlushAll }; | ||
/** | ||
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated | ||
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed. | ||
* This means after a callback function is called, a Lambda function still run | ||
* and we can catch the final clean up. | ||
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html | ||
* | ||
* @param lambdaHandler | ||
*/ | ||
declare const ensureFlushAllCallback: (lambdaHandler: any) => (event: any, context: any, callback: any) => void; | ||
export { isLogDNAEnabled, flushAll, ensureFlushAll, ensureFlushAllCallback }; |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ensureFlushAll = exports.flushAll = exports.isLogDNAEnabled = void 0; | ||
exports.ensureFlushAllCallback = exports.ensureFlushAll = exports.flushAll = exports.isLogDNAEnabled = void 0; | ||
const LogDNALogger = require('logdna'); | ||
@@ -37,3 +37,3 @@ const debug = require('util').debuglog('logger-adapter'); | ||
/** | ||
* Lambda wrapper to ensure we flush message before returning the response | ||
* Lambda wrapper to ensure we flush messages before returning the response | ||
* @param lambdaHandler | ||
@@ -55,1 +55,21 @@ */ | ||
exports.ensureFlushAll = ensureFlushAll; | ||
/** | ||
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated | ||
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed. | ||
* This means after a callback function is called, a Lambda function still run | ||
* and we can catch the final clean up. | ||
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html | ||
* | ||
* @param lambdaHandler | ||
*/ | ||
const ensureFlushAllCallback = (lambdaHandler) => { | ||
return (event, context, callback) => { | ||
const thisCallback = (error, result) => { | ||
callback(error, result); | ||
flushAll(); | ||
return; | ||
}; | ||
return lambdaHandler(event, context, thisCallback); | ||
}; | ||
}; | ||
exports.ensureFlushAllCallback = ensureFlushAllCallback; |
{ | ||
"name": "@beforeyoubid/logger-adapter", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A platform logger module to send the log messages to LogDNA.", | ||
@@ -8,3 +8,4 @@ "main": "dist/index.js", | ||
"files": [ | ||
"dist/**/*" | ||
"dist/**/*", | ||
"src/**/*" | ||
], | ||
@@ -17,3 +18,3 @@ "scripts": { | ||
"build": "tsc", | ||
"build-and-publish": "yarn build && npm publish --access public" | ||
"publish": "npm publish --access public" | ||
}, | ||
@@ -20,0 +21,0 @@ "repository": { |
@@ -8,4 +8,4 @@ # LogDNA Logger Adapter | ||
This module is designed to work on normal runtime and Lambda environment. For lambda, please see the `Flushall Messages` | ||
section. | ||
This module is designed to work on a native node runtime and in a Lambda environment. For Lambda, please see the | ||
[Flush All Messages](#Flush-All-Messages) section below. | ||
@@ -50,3 +50,3 @@ ## Console Logger vs. Winston Logger | ||
``` | ||
import { consoleLogger } from '@beforeyoubid/console-logger-adapter'; | ||
import { consoleLogger } from '@beforeyoubid/logger-adapter'; | ||
consoleLogger.init(); | ||
@@ -60,3 +60,3 @@ | ||
``` | ||
import { consoleLogger } from '@beforeyoubid/console-logger-adapter'; | ||
import { consoleLogger, flushAll } from '@beforeyoubid/logger-adapter'; | ||
consoleLogger.init(); | ||
@@ -71,3 +71,3 @@ | ||
await consoleLogger.flushAll(); // Trigger to flush all messages | ||
await flushAll(); // Trigger to flush all messages | ||
@@ -77,3 +77,3 @@ return result; | ||
// handle error | ||
await consoleLogger.flushAll(); // Trigger to flush all messages | ||
await flushAll(); // Trigger to flush all messages | ||
@@ -93,3 +93,3 @@ // Throw or send result as usual | ||
``` | ||
import { consoleLogger } from '@beforeyoubid/console-logger-adapter'; | ||
import { consoleLogger, flushAll } from '@beforeyoubid/logger-adapter'; | ||
consoleLogger.init(); | ||
@@ -103,9 +103,9 @@ | ||
await consoleLogger.flushAll(); // Trigger to flush all messages | ||
callback(null, yourNormalResponseObject); | ||
callback(null, yourNormalResponseObject); | ||
flushAll(); // Trigger to flush all messages | ||
} catch (e) { | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
await consoleLogger.flushAll(); // Trigger to flush all messages | ||
flushAll(); // Trigger to flush all messages | ||
@@ -123,2 +123,31 @@ // Call a callback with error | ||
### Lambda: Use a handler wrapper for Serverless Express | ||
``` | ||
import { consoleLogger, ensureFlushAll } from '@beforeyoubid/logger-adapter'; | ||
consoleLogger.init(); | ||
export default ensureFlushAll(graphqlHandler); | ||
``` | ||
### Lambda: Use with a Callback Handler (Apollo GraphQL Handler) | ||
``` | ||
import { consoleLogger, ensureFlushAllCallback } from '@beforeyoubid/logger-adapter'; | ||
consoleLogger.init(); | ||
// Existing graphql hander - it uses callback signature | ||
const graphqlHandler = server.createHandler({ | ||
cors: { | ||
origin: '*', | ||
allowedHeaders: ['x-api-token', 'Authorization'], | ||
methods: ['GET', 'POST'], | ||
}, | ||
}); | ||
export default ensureFlushAllCallback(graphqlHandler); | ||
``` | ||
## 2) Use Winston Logger | ||
@@ -154,3 +183,3 @@ | ||
``` | ||
import { logger } from '@beforeyoubid/logger-adapter'; | ||
import { logger, flushAll } from '@beforeyoubid/logger-adapter'; | ||
@@ -164,3 +193,3 @@ const yourLambdaHandler = async (event, context) => { | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
await logger.flushAll(); | ||
await flushAll(); | ||
@@ -170,3 +199,3 @@ return result; | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
await logger.flushAll(); // Trigger to flush all messages | ||
await flushAll(); // Trigger to flush all messages | ||
@@ -186,3 +215,3 @@ // Throw or send result as usual | ||
``` | ||
import { winstoneLogger as logger } from '@beforeyoubid/logger-adapter'; | ||
import { logger, flushAll } from '@beforeyoubid/logger-adapter'; | ||
@@ -195,10 +224,9 @@ const yourLambdaHandler = (event, context, callback) => { | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
await logger.flushAll(); // Trigger to flush all messages | ||
callback(null, yourNormalResponseObject); | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
flushAll(); // Trigger to flush all messages | ||
} catch (e) { | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
await logger.flushAll(); // Trigger to flush all messages | ||
flushAll(); // Trigger to flush all messages | ||
@@ -215,1 +243,16 @@ // Call a callback with error | ||
``` | ||
## Flush All Messages | ||
LogDNA puts messages in the buffer and push out every 250ms. If Lambda is terminated before some of these intervals, | ||
it's likely that some of message logs are not sent to LogDNA. | ||
We just need to ensure that after we've completed the business logic and before the Lambda function is terminated, we | ||
flush all messages. We can just call a LogDNA `flushAll()` function and this module export this logic for us to use. | ||
Depending on how your Lambda handler is set up, but there are three variances we have observed. | ||
1. Async handler | ||
2. Async handler but use Serverless Express framework | ||
3. Apollo non-async handler which is created by a `createHandler()` function | ||
Please see usage examples from the above sections |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26054
24
477
245