tslog icon indicating copy to clipboard operation
tslog copied to clipboard

Bug: [BUG] logger with JSON type does not respect log settings

Open rafaelmarques7 opened this issue 3 years ago • 2 comments

Describe the bug

  • as far as I can tell, when we create a new Logger with type: 'json', and pass it some settings, the Logger does not respect the settings
  • To further add to this, the json logs currently include a lot of unecessary clutter. For example, we don't care about the hostname , the log levelId, the line number, etc. The real thing we care about is the argumentsArray, but even this is not being logged as JSON, but is logged as a string representation of a JSON object instead.

To Reproduce Steps to reproduce the behavior:

  • Configure the log settings:
  logSettings = {
    displayFilePath: 'hidden',
    displayFunctionName: false,
  };
  • Instantiate the logger:
export const logJson = new Logger({ ...logSettings, type: 'json' });
  • Log out something
  logJson.info('sqsEvent with object wrapping', { sqsEvent });

Expected behavior

  • I'd expect to see a log where filePath/fullFilePath and functionName are not set. Instead, I get
{
    "hostname": "169.254.203.73",
    "date": "2022-06-29T10:43:36.300Z",
    "logLevel": "info",
    "logLevelId": 3,
    "filePath": "src/handlers/reports/webpack:/src/handlers/reports/conversions-manager.ts",
    "fullFilePath": "/var/task/src/handlers/reports/webpack:/src/handlers/reports/conversions-manager.ts",
    "fileName": "conversions-manager.ts",
    "lineNumber": 96,
    "columnNumber": 11,
    "isConstructor": false,
    "functionName": "sqsProcessor",
    "typeName": "Object",
    "argumentsArray": [
        "Starting lambda ",
        "{isProd: false,\n  offlineMode: false,\n  snowflakeConnect: true,\n  sqsEvent: { key1: 'value1', key2: 'value2', key3: 'value3' } }"
    ]
}

Screenshots

Additional context

Node.js Version v14.18.1

OS incl. Version macOS 11.6.2

rafaelmarques7 avatar Jun 29 '22 11:06 rafaelmarques7

Would be great if our argumentsArray json object could be spreaded in json root based on some setting. To do so, I had to overwrite the methods.

But argumentsArray not logging json object as string but logging as documented (see "I am a warn log with a json object" line) would be great as well.

magarrettdias avatar Jul 29 '22 17:07 magarrettdias

I agree. This is going to be the biggest breaking change in V4, I am working on right now.

terehov avatar Aug 23 '22 19:08 terehov

I've been working on a new, completely rewritten version (4.0). Can you give it a go and let me know if that solves your problem.

  • npm i tslog@next

  • and run it with node --enable-source-maps or for TypeScript node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm

Child loggers are now called sub loggers. You can find some examples on how to use them in this test file: https://github.com/fullstack-build/tslog/blob/v4/tests/Nodejs/12_SubLoggers_and_Prefixes.test.ts

terehov avatar Sep 29 '22 10:09 terehov

V4 is released now, so I'm going to close this issue. Feel free to open a new one if V4 didn't solve it for you.

Here are the docs: tslog.js.org

terehov avatar Nov 15 '22 18:11 terehov