Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@beforeyoubid/logger-adapter

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@beforeyoubid/logger-adapter - npm Package Compare versions

Comparing version 1.0.12 to 2.0.0

dist/console/logWrapper.d.ts

3

dist/index.d.ts
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",

@@ -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.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc