@beforeyoubid/logger-adapter
Advanced tools
Comparing version 1.0.12 to 2.0.0
import { consoleLogger } from './console'; | ||
import { logger } from './logger'; | ||
import { getLoggerObject } from './logger/util'; | ||
import { getLogParams } from './params'; | ||
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './util'; | ||
export { consoleLogger, logger, getLogParams, flushAll, ensureFlushAll, ensureFlushAllCallback }; | ||
export { consoleLogger, logger, getLoggerObject, getLogParams, flushAll, ensureFlushAll, ensureFlushAllCallback }; |
@@ -1,2 +0,2 @@ | ||
declare const logger: any; | ||
declare const logger: import("winston").Logger; | ||
export { logger }; |
import { ILogDNAParams } from '../types'; | ||
declare const getLogger: (params: ILogDNAParams) => any; | ||
export { getLogger }; | ||
import winston from 'winston'; | ||
/** | ||
* Create a Winston logger object so that we can get a few benefits | ||
* 1. We can use the same logger to output logs to console. If we use logger from @logdna/logger, it won't show up locally | ||
* 2. We can also format the colour of the log lines so that it's easier to read in the local docker container | ||
* 3. We can use the same logger object and send logs to logDNA using transports mechanism | ||
* | ||
* @param params | ||
* @returns | ||
*/ | ||
declare const setupWinstonLogger: (params: ILogDNAParams) => winston.Logger; | ||
export { setupWinstonLogger }; |
/// <reference types="node" /> | ||
import { ILogDNAParams } from './types'; | ||
import { type ILogDNAParams } from './types'; | ||
declare const sendToRemote: (logDNAKey: string, logDNAEnabled: string) => boolean; | ||
declare const getLogParams: (env?: NodeJS.ProcessEnv) => ILogDNAParams; | ||
export { getLogParams, sendToRemote }; |
@@ -10,3 +10,3 @@ export type EmptyCBFunction = () => void; | ||
hostname: string; | ||
index_meta: boolean; | ||
indexMeta: boolean; | ||
tags: Array<string>; | ||
@@ -13,0 +13,0 @@ level: string; |
@@ -10,2 +10,3 @@ import { LambdaHandlerWithAsyncFunction, LambdaHandlerWithCallbackFunction } from './types'; | ||
* Send all log messages to LogDNA before lambda is terminated | ||
* Support an async function | ||
*/ | ||
@@ -12,0 +13,0 @@ declare const flushAll: () => Promise<void>; |
{ | ||
"name": "@beforeyoubid/logger-adapter", | ||
"version": "1.0.12", | ||
"description": "A platform logger module to send the log messages to LogDNA.", | ||
"version": "2.0.0", | ||
"description": "A platform logger module to send the log messages to Mezmo (formerly LogDNA).", | ||
"main": "dist/index.js", | ||
@@ -37,11 +37,11 @@ "types": "dist/index.d.ts", | ||
"dependencies": { | ||
"logdna": "3.5.3", | ||
"logdna-winston": "^2.3.2", | ||
"winston": "3.8.2" | ||
"@logdna/logger": "^2.6.9", | ||
"winston": "^3.13.0", | ||
"winston-transport": "^4.7.0" | ||
}, | ||
"devDependencies": { | ||
"@swc/cli": "0.1.61", | ||
"@swc/core": "1.3.35", | ||
"@swc/jest": "0.2.24", | ||
"@types/jest": "29.4.0", | ||
"@swc/cli": "^0.4.0", | ||
"@swc/core": "^1.6.6", | ||
"@swc/jest": "^0.2.36", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "18.13.0", | ||
@@ -55,3 +55,3 @@ "@typescript-eslint/eslint-plugin": "5.52.0", | ||
"eslint-plugin-prettier": "4.2.1", | ||
"jest": "29.4.2", | ||
"jest": "^29.7.0", | ||
"jest-junit": "15.0.0", | ||
@@ -58,0 +58,0 @@ "prettier": "2.8.4", |
182
README.md
@@ -1,8 +0,17 @@ | ||
# LogDNA Logger Adapter | ||
# Mezmo (Formerly LogDNA) Logger Adapter | ||
This is a platform logger module that is able to support multiple logging scenarios. | ||
Notes - LogDNA made a decision not to rename the library to Mezmo just yet. To avoid having two names, we decided to | ||
keep the variable, class and etc as LogDNA for now. | ||
- A `console` log wrapper so it can send all logs from the native `console` object to LogDNA. | ||
- A Winston log wrapper logger which output better colour and we can send log messages to LogNDA. | ||
--- | ||
This is our BYB logger module that is designed to support multiple two logging scenarios. | ||
- Some repos may heavily rely on console.log() or similar to output log on the console. We can utilise this module to | ||
wrap around the original `console` so all logs can be sent to Mezmo as well. The service need to add | ||
`consoleLogger.init()` in the handler. | ||
- A Winston logger - Winston creates a generic logger objec that allows us to perform various operations on the log e.g. | ||
change the colour of the log line, format message, can customise/add a custom code (using a transport mechanism) to | ||
send log messages to Mezmo or store in the database if we want. | ||
This module is designed to work on a native node runtime and in a Lambda environment. For Lambda, please see the | ||
@@ -28,2 +37,20 @@ [Flush All Messages](#Flush-All-Messages) section below. | ||
### Versions | ||
- **v1.x.x (first version)** | ||
- Originally created in 2020 but the `logdna` module is no longer maintained (https://www.npmjs.com/package/logdna). | ||
We need to move away from it. | ||
- **v2.x.x** | ||
- Utilise the latest module actively maintained by Mezmo team (https://www.npmjs.com/package/@logdna/logger) | ||
- Fix a missing some log line issue occured due to the the fact that `logdna-winston` module creates a separate logdna | ||
client instance and it doesn't get a chance to fully flush messages before Lambda terminates. We decided to | ||
customise this module in this project and allow us to utilise a single instance of a LogDNA logger object so that | ||
`ensureFlushAll()` can flush those messages before we terminate Lambda function. This single LogDNA is shared and | ||
used by both console log wrapper and LogDNA transport as part of Winston log system. | ||
- Although it is not a major change, users of this module would like an explicably upgraded version. Just update to | ||
the most recent v2 version; no API modifications are necessary on the consumer side. | ||
- Also moved `createLoggerObject()` into this shared module so it's easier to use and prefix message before returning | ||
the actual logger object. e.g. `createLoggerObject(`[LOG-PREFIX]`);` | ||
- ![V2 Diagram](./docs/v2-diagram.png) | ||
## 1) Use Console Logger | ||
@@ -59,63 +86,7 @@ | ||
### Lambda: Use with an Aync Handler | ||
## Flush All Messages | ||
``` | ||
import { consoleLogger, flushAll } from '@beforeyoubid/logger-adapter'; | ||
consoleLogger.init(); | ||
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. Use `ensureFlushAll()` to flush messages automatically. | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
const yourLambdaHandler = async (event, context) => { | ||
try { | ||
console.debug('Sample debug log - this message only show up when LOG_LEVEL is set to "debug"') | ||
const result = await yourNormalProcess(); | ||
await flushAll(); // Trigger to flush all messages | ||
return result; | ||
} catch (e) { | ||
// handle error | ||
await flushAll(); // Trigger to flush all messages | ||
// Throw or send result as usual | ||
throw e; | ||
// Or handle it nicely | ||
// return unsuccessfulResponse; | ||
} | ||
} | ||
``` | ||
### Lambda: Use with a Callback Handler | ||
``` | ||
import { consoleLogger, flushAll } from '@beforeyoubid/logger-adapter'; | ||
consoleLogger.init(); | ||
const yourLambdaHandler = (event, context, callback) => { | ||
try { | ||
console.debug('Sample debug log - this message only show up when LOG_LEVEL is set to "debug"') | ||
const result = yourNormalProcess(); | ||
callback(null, yourNormalResponseObject); | ||
flushAll(); // Trigger to flush all messages | ||
} catch (e) { | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
flushAll(); // Trigger to flush all messages | ||
// Call a callback with error | ||
callback(yourErrorObject); | ||
// Or return unsuccessful response | ||
// https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format | ||
// callback(null, unsuccessfulResponse); | ||
} | ||
} | ||
``` | ||
### Lambda: Use a handler wrapper for Serverless Express | ||
@@ -127,7 +98,7 @@ | ||
export default ensureFlushAll(graphqlHandler); | ||
export default ensureFlushAll(yourLambdaHandler); | ||
``` | ||
### Lambda: Use with a Callback Handler (Apollo GraphQL Handler) | ||
### Lambda: Use with a Callback Handler | ||
@@ -138,13 +109,4 @@ ``` | ||
// Existing graphql hander - it uses callback signature | ||
const graphqlHandler = server.createHandler({ | ||
cors: { | ||
origin: '*', | ||
allowedHeaders: ['x-api-token', 'Authorization'], | ||
methods: ['GET', 'POST'], | ||
}, | ||
}); | ||
export default ensureFlushAllCallback(yourLambdaHandlerCallback); | ||
export default ensureFlushAllCallback(graphqlHandler); | ||
``` | ||
@@ -179,29 +141,11 @@ | ||
### Lambda: Use with an Aync Handler | ||
### Lambda: Use a handler wrapper for Serverless Express | ||
``` | ||
import { logger, flushAll } from '@beforeyoubid/logger-adapter'; | ||
import { logger, ensureFlushAll } from '@beforeyoubid/logger-adapter'; | ||
const yourLambdaHandler = async (event, context) => { | ||
try { | ||
logger.debug('Sample debug log - this message only show up when LOG_LEVEL is set to "debug"') | ||
// you can use logger.debug('log text in a string format'); | ||
const result = await yourNormalProcess(); | ||
export default ensureFlushAll(yourLambdaHandler); | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
await flushAll(); | ||
return result; | ||
} catch (e) { | ||
// Ensure the we flush all messages before Lambda function gets terminated | ||
await flushAll(); // Trigger to flush all messages | ||
// Throw or send result as usual | ||
throw e; | ||
// Or handle it nicely | ||
// return unsuccessfulResponse; | ||
} | ||
} | ||
``` | ||
@@ -212,42 +156,26 @@ | ||
``` | ||
import { logger, flushAll } from '@beforeyoubid/logger-adapter'; | ||
import { logger, ensureFlushAllCallback } from '@beforeyoubid/logger-adapter'; | ||
const yourLambdaHandler = (event, context, callback) => { | ||
try { | ||
logger.debug('Sample debug log - this message only show up when LOG_LEVEL is set to "debug"') | ||
// you can use logger.debug('log text in a string format'); | ||
const result = yourNormalProcess(); | ||
export default ensureFlushAllCallback(yourLambdaHandlerCallback); | ||
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 | ||
flushAll(); // Trigger to flush all messages | ||
``` | ||
// Call a callback with error | ||
callback(yourErrorObject); | ||
### Prefixing your log message | ||
// Or return unsuccessful response | ||
// https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format | ||
// callback(null, unsuccessfulResponse); | ||
} | ||
} | ||
In some cases, you may want to have a clearer log message by prefixing them with a predefined string e.g. name of your | ||
module so they look like this. ![Log prefix](./docs/log-prefix.png) | ||
``` | ||
import { getLoggerObject } from '@beforeyoubid/logger-adapter'; | ||
## Flush All Messages | ||
const logger = getLoggerObject('[CoreLogicListingProvider]'); | ||
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. | ||
logger.info('the usual log string...') | ||
``` | ||
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 | ||
**Notes that** this function just wraps the original Winston logger instance your normally access from this line. | ||
`import { logger } from '@beforeyoubid/logger-adapter';`. It only wraps and add the prefix the log line for you and | ||
returns with debug, info, warn, error functions. |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
118884
31
1506
176
4
+ Added@logdna/logger@^2.6.9
+ Addedwinston-transport@^4.7.0
+ Added@logdna/logger@2.6.11(transitive)
+ Added@logdna/stdlib@1.2.3(transitive)
+ Addedagent-base@7.1.3(transitive)
+ Addedagentkeepalive@4.6.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaxios@1.7.9(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedhttps-proxy-agent@7.0.6(transitive)
+ Addedhumanize-ms@1.2.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
+ Addedwinston@3.17.0(transitive)
- Removedlogdna@3.5.3
- Removedlogdna-winston@^2.3.2
- Removed@colors/colors@1.5.0(transitive)
- Removed@eslint-community/eslint-utils@4.4.1(transitive)
- Removed@eslint-community/regexpp@4.12.1(transitive)
- Removed@eslint/config-array@0.19.1(transitive)
- Removed@eslint/core@0.9.1(transitive)
- Removed@eslint/eslintrc@3.2.0(transitive)
- Removed@eslint/js@9.17.0(transitive)
- Removed@eslint/object-schema@2.1.5(transitive)
- Removed@eslint/plugin-kit@0.2.4(transitive)
- Removed@humanfs/core@0.19.1(transitive)
- Removed@humanfs/node@0.16.6(transitive)
- Removed@humanwhocodes/module-importer@1.0.1(transitive)
- Removed@humanwhocodes/retry@0.3.10.4.1(transitive)
- Removed@types/estree@1.0.6(transitive)
- Removed@types/json-schema@7.0.15(transitive)
- Removedacorn@8.14.0(transitive)
- Removedacorn-jsx@5.3.2(transitive)
- Removedagentkeepalive@2.2.0(transitive)
- Removedajv@6.12.6(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedargparse@2.0.1(transitive)
- Removedaxios@0.25.0(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedcallsites@3.1.0(transitive)
- Removedchalk@4.1.2(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedcross-spawn@7.0.6(transitive)
- Removeddeep-is@0.1.4(transitive)
- Removedes6-promise@4.2.8(transitive)
- Removedescape-string-regexp@4.0.0(transitive)
- Removedeslint@9.17.0(transitive)
- Removedeslint-scope@8.2.0(transitive)
- Removedeslint-visitor-keys@3.4.34.2.0(transitive)
- Removedespree@10.3.0(transitive)
- Removedesquery@1.6.0(transitive)
- Removedesrecurse@4.3.0(transitive)
- Removedestraverse@5.3.0(transitive)
- Removedesutils@2.0.3(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfast-levenshtein@2.0.6(transitive)
- Removedfile-entry-cache@8.0.0(transitive)
- Removedfind-up@5.0.0(transitive)
- Removedflat-cache@4.0.1(transitive)
- Removedflatted@3.3.2(transitive)
- Removedglob-parent@6.0.2(transitive)
- Removedglobals@14.0.0(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedieee754@1.2.1(transitive)
- Removedignore@5.3.2(transitive)
- Removedimport-fresh@3.3.0(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removedis-extglob@2.1.1(transitive)
- Removedis-glob@4.0.3(transitive)
- Removedisexe@2.0.0(transitive)
- Removedjs-yaml@4.1.0(transitive)
- Removedjson-buffer@3.0.1(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stable-stringify-without-jsonify@1.0.1(transitive)
- Removedkeyv@4.5.4(transitive)
- Removedlevn@0.4.1(transitive)
- Removedlocate-path@6.0.0(transitive)
- Removedlodash.bind@4.2.1(transitive)
- Removedlodash.clonedeep@4.5.0(transitive)
- Removedlodash.foreach@4.5.0(transitive)
- Removedlodash.isequal@4.5.0(transitive)
- Removedlodash.merge@4.6.2(transitive)
- Removedlogdna@3.5.3(transitive)
- Removedlogdna-winston@2.3.2(transitive)
- Removedminimatch@3.1.2(transitive)
- Removednatural-compare@1.4.0(transitive)
- Removedobject-sizeof@1.6.3(transitive)
- Removedoptionator@0.9.4(transitive)
- Removedp-limit@3.1.0(transitive)
- Removedp-locate@5.0.0(transitive)
- Removedparent-module@1.0.1(transitive)
- Removedpath-exists@4.0.0(transitive)
- Removedpath-key@3.1.1(transitive)
- Removedprelude-ls@1.2.1(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedresolve-from@4.0.0(transitive)
- Removedshebang-command@2.0.0(transitive)
- Removedshebang-regex@3.0.0(transitive)
- Removedstrip-json-comments@3.1.1(transitive)
- Removedsupports-color@7.2.0(transitive)
- Removedtype-check@0.4.0(transitive)
- Removeduri-js@4.4.1(transitive)
- Removedvalid-url@1.0.9(transitive)
- Removedwhich@2.0.2(transitive)
- Removedwinston@3.8.2(transitive)
- Removedword-wrap@1.2.5(transitive)
- Removedyocto-queue@0.1.0(transitive)
Updatedwinston@^3.13.0