debug
It is helpful to debug your application’s logic.
Just pass any effector’s unit to debug
.
Motivation
Sometimes during development, it is necessary to display the value of the stores and payload of events, each time you write console.log
inside .watch
and copy/paste it is unpleasant. debug
allows you to speed up this process a lot by passing all the necessary units for a debugging into a method arguments.
Example
Traces
patronum/debug
supports computation traces logging, if { trace: true }
is set.
It is recommended to use this feature along with effector/babel-plugin
.
Custom names
Sometimes unit name in specific context may be different from the one it was initially created with. e.g., an unit may be exported under an alias for explicitness:
In this case, because of effector/babel-plugin
which provided productsPageModel.$open
store its name at the moment of its creation, public name in the debug
logs will be $open
instead of $productsListVisible
.
It can be fixed with custom name, which can be provided by using Record<string, Unit>
instead of a list of units:
This way $productsListVisible
name in the logs will be the same, as the one which was provided to debug
. The productAdded
event will be named customEventName
.
Fork API and Scope
Effector can run multiple “instances” of the app simultaniosly via Fork API - it is useful for tests and SSR. Usually you would also use scope on the client in the case of SSR. debug
will log “scoped” updates in such case:
By default detected scope will be given default name.
Scope registration
It is possible to explicitly register scope with given name to have a more explicit logs.
It can work like this:
This way scope will be given explicit name in the logs.
It is also possible to unregister scope to prevent memory leak, if scope is no longer needed:
Or unregister all scopes at once:
Initial store state
debug($store)
always immediately prints current state of the store, but this state can be different in different scopes.
It is recommended to register scopes explicitly, since debug
will print current state of the store in every known scope:
Customization
The patronum/debug
package extracts quite a lot of low-level effector data in a universal format that may be useful for other dev tools or monitoring tools, so there is a special API to add your own way of handling this data.
Custom handler
You can provide custom log handler in the config. It can be useful, if console.info
somehow doesn’t fit your use-case: e.g. you want advanced info from patronum/debug
to built your own dev-tools, debug especially hard case, etc.
Handler is called for each update with context object like this:
Common fields:
- logType -
initial | update
- log type for convenience. All provided stores current values are logged withinitial
for all known scopes right away. Same after new scope was registered. All other updates are ofupdate
type. - scope -
Scope | null
- effector’sScope
context object, which owns this particular update - scopeName -
string | null
- name of theScope
, if registered andnull
otherwise. - node -
Node
- effector’s internal node, which update is being logged. - name -
string | null
- node’s name for convenience.null
- if node doesn’t have own name (likesample
calls). - kind -
string
- node’s kind for convenience. It can be unit’s kind (e.g.store
orevent
) or operation kind (e.g.sample
,split
, etc). - value -
unknown
- value of the update. - loc -
{ file?: string; line: number; column: number; }
- location in the source code, if known - stackMeta -
{ Record<string, unknown> }
- effector’s internal stack metadata, available if provided. For example, effect calls provide anfxID
that is stable betweenfx
andfx.finally
updates - this allows you to associate afx.finally
update with a particularfx
call (available since effector@22.5.0)
Special field if trace: true
provided:
- trace -
Array<{ node: Node; name: string | null; kind: string; value: unknown; loc?: Loc; stackMeta?: Record<string, unknown> }>
- trace of updates.
The trace
array is always empty (i.e. trace is not collected), if debug
’s config does not have trace: true
.