Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use new logger and ReanimatedError for lib warnings and errors #6387

Merged
merged 19 commits into from
Aug 26, 2024

Conversation

MatiPl01
Copy link
Member

@MatiPl01 MatiPl01 commented Aug 7, 2024

Summary

This PR replaces all console.warn, console.error and throw new Error statements with custom logger calls (logger.warn, logger.error and logger.newError respectively).

It declares the logger object as a global variable for easier usage and to prevent circular imports (as it imports runOnJS, which was the cause of circular dependencies).

Test plan

  • open the EmptyExample in the example app,
  • change the SHOW_EXAMPLE constant to see the respective error/warning,
  • see better error logs with code frame that was the culprit (if possible - sometimes we cannot get pretty error stack trace, especially when functions are called asynchronously)

@MatiPl01
Copy link
Member Author

MatiPl01 commented Aug 7, 2024

Logs comparison

(Based on 17 examples that I tested out)


Improved logs

1. Error: [Reanimated] Property text was whitelisted both as UI and native prop. Please remove it from one of the lists.

Before After
Simulator Screenshot - iPhone 15 Pro - 2024-08-07 at 17 53 25 simulator_screenshot_B88AFBF4-4F9A-4AAD-8661-63CB3F1D36EE

2. Warning: Error: [Reanimated] Function getViewProp requires a component to be passed as an argument on Fabric.

Before After
simulator_screenshot_9554B659-06FF-4587-A6ED-9F92621B8689 simulator_screenshot_314671F4-B2BC-4FA4-80FC-A65698579314

3. Warning: Error: [Reanimated] Invalid color space provided: INVALID. Supported values are: ['RGB', 'HSV'].

Before After
simulator_screenshot_FBBC89DE-0145-4AAC-BE45-419E92380E42 simulator_screenshot_F5F4F7E9-F638-461E-8E82-91E6875E1C7F

4. Warning: Error: [Reanimated] Unsupported value for "interpolate"

Before After
simulator_screenshot_7F209BED-7096-485E-8DDF-53B35BA23108 Simulator Screenshot - iPhone 15 Pro - 2024-08-07 at 18 18 03

5. (A bit different) Warning: Error: [Reanimated] Unsupported value for "interpolate"

Before After
simulator_screenshot_F0090E52-C43A-4B2F-A5BC-25D8EAA69832 simulator_screenshot_B201CC43-5747-49E0-8FE5-71DABA8DFEEF

6. Warning: Error: [Reanimated] Interpolation input and output ranges should contain at least two values.

Before After
simulator_screenshot_20FB94E6-2938-4C1B-8703-CBD59D57EAA8 simulator_screenshot_C04E81C5-C88B-4715-8E01-5A710768B520

7. Warning: Error: [Reanimated] The function passed to runOnRuntime is not a worklet.

Before After
simulator_screenshot_C0C4DD8A-78DD-4B0A-84CB-92C4FD91AC77 simulator_screenshot_C23C024E-E7E0-4EA0-86C3-CA41748F59C8

8. [Reanimated] No animation was provided for the sequence

Before After
simulator_screenshot_340975D7-6CDC-4D28-9FF5-9CBB2B40F2F2 simulator_screenshot_8EA02737-ED83-4E05-AF8E-3BD14A4E3101

9. Warning: Error: [Reanimated] The easing function is not a worklet. Please make sure you import Easing from react-native-reanimated.

Before After
simulator_screenshot_8BA18FA7-CA3B-4380-8DCE-32B364821C5D simulator_screenshot_3D7DEAD7-CA91-4180-B6AE-455EDED48E36

10. Warning: Error: [Reanimated] useAnimatedStyle has to return an object, found undefined instead.

Before After
simulator_screenshot_574721B0-B41B-4A15-9379-A16C79627A55 simulator_screenshot_F45E75D0-757B-4BC6-BDC2-C7C94DE0A6E2

11. [Reanimated] The view with tag -1 is not a valid argument for measure(). This may be because the view is not currently rendered, which may not be a bug (e.g. an off-screen FlatList item).

Before After
simulator_screenshot_8BD99879-693E-4AA8-AA3C-89F9E935BE8F simulator_screenshot_29B6A89D-72C2-40E6-BD39-B9F94E470A3D

12. [Reanimated] setNativeProps() can only be used on the UI runtime.

Before After
simulator_screenshot_4991113F-E89E-41D7-A80D-49134FF4B7B9 simulator_screenshot_463B1026-6AD5-42C3-BB42-B2081C141837

Not prettified logs

These logs were pointing to the log call location in reanimated (without user's code in the stack trace showing the real cause of the issue). Because of that I decided to remove code fragment from these logs as it was a bit misleading.

1. [Reanimated] Property "opacity" of AnimatedComponent(View) may be overwritten by a layout animation. Please wrap your component with an animated view and apply the layout animation on the wrapper.

Before After
Simulator Screenshot - iPhone 15 Pro - 2024-08-07 at 17 57 03 simulator_screenshot_871689E2-C3CB-4DEE-A748-9D0339840274

2. ReanimatedError: [Reanimated] Bezier x values must be in [0, 1] range., js engine: reanimated

Before After
simulator_screenshot_6D519664-8F46-4494-9192-E284CB9E6297 Simulator Screenshot - iPhone 15 Pro - 2024-08-07 at 18 16 22

3. Warning: Error: [Reanimated] Reading from value directly is not supported. Use worklet to read value from the animation state.

Before After
simulator_screenshot_888444B9-4B04-4BF0-A296-39966490B2D9 simulator_screenshot_B246B8CF-3821-4FD7-B160-91ECBF0E12E5

4. [Reanimated] Wrong config was provided to withClamp. Min value is bigger than max

Before After
simulator_screenshot_C7FE4CA0-B0A8-47F6-9D2A-3C561F9F06FB Simulator Screenshot - iPhone 15 Pro - 2024-08-07 at 18 23 40

5. [Reanimated] Invalid spring config, stiffness must be grater than zero but got 0, damping must be grater than zero but got 0

Before After
simulator_screenshot_4A1A80AF-D70E-4721-ADAA-A13878305BCC Simulator Screenshot - iPhone 15 Pro - 2024-08-07 at 18 26 13

@MatiPl01 MatiPl01 changed the title feat: Replace console with custom logger where possible feat: Replace console with custom logger Aug 7, 2024
@MatiPl01 MatiPl01 marked this pull request as ready for review August 8, 2024 09:37
@MatiPl01 MatiPl01 requested review from tomekzaw and tjzel August 8, 2024 09:48
apps/next-example/babel.config.js Show resolved Hide resolved
packages/react-native-reanimated/jest-setup.js Outdated Show resolved Hide resolved
packages/react-native-reanimated/jest.config.js Outdated Show resolved Hide resolved
packages/react-native-reanimated/src/Bezier.ts Outdated Show resolved Hide resolved
packages/react-native-reanimated/src/Bezier.ts Outdated Show resolved Hide resolved
packages/react-native-reanimated/src/logger/logger.ts Outdated Show resolved Hide resolved
packages/react-native-reanimated/src/mutables.ts Outdated Show resolved Hide resolved
packages/react-native-reanimated/src/publicGlobals.ts Outdated Show resolved Hide resolved
packages/react-native-reanimated/src/runtimes.ts Outdated Show resolved Hide resolved
@MatiPl01 MatiPl01 force-pushed the @matipl01/new-logger-console-logs-replacement branch from 775c87a to 3d68cac Compare August 9, 2024 10:51
@MatiPl01 MatiPl01 changed the title feat: Replace console with custom logger feat: Use new logger and ReanimatedError for lib warnings and errors Aug 9, 2024
@MatiPl01 MatiPl01 force-pushed the @matipl01/new-logger-console-logs-replacement branch 2 times, most recently from f7ab541 to 573192c Compare August 12, 2024 17:40
@MatiPl01 MatiPl01 requested a review from tjzel August 12, 2024 17:56
@MatiPl01 MatiPl01 force-pushed the @matipl01/new-logger-console-logs-replacement branch from c187063 to 7b1a40c Compare August 12, 2024 18:08
@MatiPl01 MatiPl01 force-pushed the @matipl01/new-logger-console-logs-replacement branch from 386b83a to fba8f74 Compare August 23, 2024 15:15
@MatiPl01 MatiPl01 requested a review from tjzel August 23, 2024 15:21
Copy link
Collaborator

@tjzel tjzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good!

Since now we want to enforce the use of ReanimatedError and logger in our source code, please add a CI step that ensures no plain errors (except places where we must) or no console.something is used.

I guess we'd need to make an ESLint plugin for that, so don't consider it a priority task.

packages/react-native-reanimated/jest.config.js Outdated Show resolved Hide resolved
packages/react-native-reanimated/plugin/src/globals.ts Outdated Show resolved Hide resolved
packages/react-native-reanimated/src/index.ts Outdated Show resolved Hide resolved
github-merge-queue bot pushed a commit that referenced this pull request Aug 26, 2024
## Summary

This PR is a much cleaner approach than proposed in #6364. It includes
metro-config modification which is essential to collapse logs from
reanimated source code, which aren't helpful to the user while tracking
down the issue. The previous approach was trimming logs from reanimated
source code completely - this approach just collapses them, so that they
are still available to the user and can be revealed above the presented
stack trace part.

## General idea

To get better logs, I had to implement the following 2 changes:
1. **metro config** - the `symbolicator` must have been added to
properly collapse stack frames that aren't meaningful to the user,
2. **logger object** - the new logger object uses `LogBox.addLog`
method, thanks to which we can get pretty stack traces when we log a
warning from the UI thread (before such warnings didn't include
meaningful stack trace as error stack was created inside `LogBox` after
`runOnJS` was called, so we were getting a bit limited JS stack - see
[example
11](#6387 (comment))
in the follow up PR).

## Example improvement
(tested on a real project to see if it works there as well)

- current logs either point to the reanimated source code or aren't
readable at all (if warning is logged from the UI thread as in the
example below)
- new logger shows correct destination of the issue culprit in the code
frame, collapses stack frames in the call stack that aren't interesting
to the user (reanimated source code) and focuses on the file where the
user included problematic code

| Before | After |
|-|-|
| <video
src="https://github.com/user-attachments/assets/a5302586-f4d0-4636-8bd8-6c406c9d8c73"
/> | <video
src="https://github.com/user-attachments/assets/3121636f-69a2-4b6f-8f38-b1889d4c62e1"
/> |

## Test plan

See the example in the next PR (#6387).

---------

Co-authored-by: Tomasz Żelawski <[email protected]>
Base automatically changed from @matipl01/better-logs to main August 26, 2024 10:17
@MatiPl01 MatiPl01 force-pushed the @matipl01/new-logger-console-logs-replacement branch from d5d9145 to 8986ccb Compare August 26, 2024 10:37
@MatiPl01 MatiPl01 enabled auto-merge August 26, 2024 10:45
@MatiPl01 MatiPl01 added this pull request to the merge queue Aug 26, 2024
Merged via the queue into main with commit afbad1e Aug 26, 2024
8 checks passed
@MatiPl01 MatiPl01 deleted the @matipl01/new-logger-console-logs-replacement branch August 26, 2024 12:02
Latropos pushed a commit that referenced this pull request Aug 26, 2024
## Summary

This PR is a much cleaner approach than proposed in #6364. It includes
metro-config modification which is essential to collapse logs from
reanimated source code, which aren't helpful to the user while tracking
down the issue. The previous approach was trimming logs from reanimated
source code completely - this approach just collapses them, so that they
are still available to the user and can be revealed above the presented
stack trace part.

## General idea

To get better logs, I had to implement the following 2 changes:
1. **metro config** - the `symbolicator` must have been added to
properly collapse stack frames that aren't meaningful to the user,
2. **logger object** - the new logger object uses `LogBox.addLog`
method, thanks to which we can get pretty stack traces when we log a
warning from the UI thread (before such warnings didn't include
meaningful stack trace as error stack was created inside `LogBox` after
`runOnJS` was called, so we were getting a bit limited JS stack - see
[example
11](#6387 (comment))
in the follow up PR).

## Example improvement
(tested on a real project to see if it works there as well)

- current logs either point to the reanimated source code or aren't
readable at all (if warning is logged from the UI thread as in the
example below)
- new logger shows correct destination of the issue culprit in the code
frame, collapses stack frames in the call stack that aren't interesting
to the user (reanimated source code) and focuses on the file where the
user included problematic code

| Before | After |
|-|-|
| <video
src="https://github.com/user-attachments/assets/a5302586-f4d0-4636-8bd8-6c406c9d8c73"
/> | <video
src="https://github.com/user-attachments/assets/3121636f-69a2-4b6f-8f38-b1889d4c62e1"
/> |

## Test plan

See the example in the next PR (#6387).

---------

Co-authored-by: Tomasz Żelawski <[email protected]>
tjzel added a commit that referenced this pull request Aug 28, 2024
This PR is a much cleaner approach than proposed in #6364. It includes
metro-config modification which is essential to collapse logs from
reanimated source code, which aren't helpful to the user while tracking
down the issue. The previous approach was trimming logs from reanimated
source code completely - this approach just collapses them, so that they
are still available to the user and can be revealed above the presented
stack trace part.

To get better logs, I had to implement the following 2 changes:
1. **metro config** - the `symbolicator` must have been added to
properly collapse stack frames that aren't meaningful to the user,
2. **logger object** - the new logger object uses `LogBox.addLog`
method, thanks to which we can get pretty stack traces when we log a
warning from the UI thread (before such warnings didn't include
meaningful stack trace as error stack was created inside `LogBox` after
`runOnJS` was called, so we were getting a bit limited JS stack - see
[example
11](#6387 (comment))
in the follow up PR).

(tested on a real project to see if it works there as well)

- current logs either point to the reanimated source code or aren't
readable at all (if warning is logged from the UI thread as in the
example below)
- new logger shows correct destination of the issue culprit in the code
frame, collapses stack frames in the call stack that aren't interesting
to the user (reanimated source code) and focuses on the file where the
user included problematic code

| Before | After |
|-|-|
| <video
src="https://github.com/user-attachments/assets/a5302586-f4d0-4636-8bd8-6c406c9d8c73"
/> | <video
src="https://github.com/user-attachments/assets/3121636f-69a2-4b6f-8f38-b1889d4c62e1"
/> |

See the example in the next PR (#6387).

---------

Co-authored-by: Tomasz Żelawski <[email protected]>
tjzel added a commit that referenced this pull request Aug 28, 2024
…6387)

## Summary

This PR replaces all `console.warn`, `console.error` and `throw new
Error` statements with custom logger calls (`logger.warn`,
`logger.error` and `logger.newError` respectively).

It declares the logger object as a global variable for easier usage and
to prevent circular imports (as it imports `runOnJS`, which was the
cause of circular dependencies).

## Test plan

- open the `EmptyExample` in the example app,
- change the `SHOW_EXAMPLE` constant to see the respective
error/warning,
- see better error logs with code frame that was the culprit (if
possible - sometimes we cannot get pretty error stack trace, especially
when functions are called asynchronously)

---------

Co-authored-by: Tomasz Żelawski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants