mowispace / react-native-logs

Performance-aware simple logger for React-Native and Expo with namespaces, custom levels and custom transports (colored console, file writing, etc.)
MIT License
467 stars 33 forks source link

nested object does not show #97

Closed chanphiromsok closed 1 month ago

schmidal commented 6 months ago

Same issue here, example output:

 LOG  13:35:15 | DEBUG : myAsyncNotificationReceivedHandler - Push Notification Content:  
{
  "body": "Test",
  "data": {}, // **IN FACT, DATA DOES CONTAIN ELEMENTS**
  "title": "Title",
  "deeplinkUrl": "myapp://"
}
GninninwokyOuattara commented 6 months ago

Same issue still. image

GninninwokyOuattara commented 6 months ago

Wrapping the content inside JSON.stringify works.

logger.info(JSON.stringify(message, null, 2));

image

callaars commented 6 months ago

I think this is done because otherwise the logs might be getting ridiculously long. You can patch it though if you want in src/index.ts:53

Norcy commented 3 months ago

same issue.

Thanks to @callaars

This patch code works for me

diff --git a/node_modules/react-native-logs/src/index.ts b/node_modules/react-native-logs/src/index.ts
index 8b82148..0fc7c35 100644
--- a/node_modules/react-native-logs/src/index.ts
+++ b/node_modules/react-native-logs/src/index.ts
@@ -50,7 +50,7 @@ let stringifyFunc = (msg: any): string => {
   } else {
     try {
       stringMsg =
-        "\n" + JSON.stringify(msg, Object.getOwnPropertyNames(msg), 2) + "\n";
+        "\n" + JSON.stringify(msg, null, 2) + "\n";
     } catch (error) {
       stringMsg += "Undefined Message";
     }
MJRT commented 2 months ago

it don't need patch, just custom a stringify, use this cover more cases:

export function objectsReplacer(_key: string, value: any): any {
  if (typeof value in ['undefined', 'symbol']) {
    return undefined;
  } else if (typeof value === 'bigint') {
    return value.toString();
  } else if (value instanceof Map) {
    return Array.from(value.entries());
  } else if (value instanceof Set) {
    return Array.from(value.values());
  }

  return value;
}

const logConfig: configLoggerType = {
  ...

  stringifyFunc: ((msg) => JSON.stringify(msg, objectsReplacer, 2)),

  ...
};
alessandro-bottamedi commented 1 month ago

In the patch method described above, the non-enumerable properties of an object (which is common when logging error objects) are not printed. This issue is resolved with a new stringify method in version 5.2.0.

Thanaen commented 1 month ago

@alessandro-bottamedi I'm trying 5.2.0, and it doesn't seems to fully work:

Logger.debug("NESTED OBJECT", {
  nested: {
    key: "value",
  },
});

⬇️

 LOG  2024-10-18T11:53:42.496Z | DEBUG : NESTED OBJECT 
{
  "nested": {}
}
alessandro-bottamedi commented 1 month ago

Can you share your configuration?

Thanaen commented 1 month ago

Sure, here it is:

let fileLoggingStatus: "unitialized" | "enabled" | "disabled" = "unitialized";

const skippedLogsSet = new Set<Parameters<transportFunctionType>[0]>();

const Logger = logger.createLogger<"debug" | "info" | "warn" | "error">({
  transport: (props) => {
    if (__DEV__) {
      consoleTransport(props);
    }
    switch (fileLoggingStatus) {
      case "unitialized":
        skippedLogsSet.add(props);
        break;
      case "enabled":
        fileTransport(props);
        break;
      case "disabled":
        break;
    }

    sentryTransport(props);
  },
  dateFormat: "iso",
});

There are some extra logic to initialize the file transport, but it shouldn't have any impact on this issue

alessandro-bottamedi commented 1 month ago

Sorry, I found the bug, now with version 5.2.1 it should work correctly. Thanks for reporting it!

Thanaen commented 1 month ago

Great, thanks for your reactivity!

Thanaen commented 1 month ago

Since the version that fixed this issue (5.2.1) has been reverted, maybe we should reopen this issue? @alessandro-bottamedi

alessandro-bottamedi commented 1 month ago

Since the version that fixed this issue (5.2.1) has been reverted, maybe we should reopen this issue? @alessandro-bottamedi

We didn’t roll back the entire version, only the config merge function. This issue is resolved in version v 5.2.2.

Thanaen commented 1 month ago

Oh okay, awesome! I should have tested before commenting, it indeed works fine! 😁