Debugging
A phone has no browser console to open. This page shows where your logs go, how to stream them to your computer, and how to inspect a running app.
Where console.log goes
console.log(), console.warn() and the rest work on Lynx. Where you read the output depends on where the app runs:
On a device, console.* goes to Lynx's native logging, not to a panel on screen. To read logs without Lynx DevTool, use LynxLogger.
Stream logs to your computer
LynxLogger sends log lines from the device to a file on your computer while ng serve runs.
In a second terminal, in the folder where you ran ng serve, follow the log file:
Each call becomes one line with the time and the level:
- How it reaches you.
ng servealso starts a small log server on the next port up (3001 when the dev server uses 3000). The app sends each line there over your network. It works from any code, including event handlers. - The file. Lines go to
.dev-logsin the folder you ranng servefrom. Eachng serverun starts the file fresh. Add.dev-logsto your.gitignore. - The methods.
log(),info(),warn(),error()anddebug(). The level shows in each line. - Objects. Each argument is turned into a string with
String(), so an object prints as[object Object]. PassJSON.stringify(value)instead. - Logged twice? Code that runs in both copies of your app, such as a constructor, logs once per copy. See Threads.
- Development only.
LynxLoggerdoes nothing in production builds, so you can leave the calls in. A shipped app sends nothing.
.dev-logs?
The phone must reach your computer on the log server's port as well as the dev server's. Check that both are on the same network, and that a firewall doesn't block the port.
Inspect the app with Lynx DevTool
Lynx DevTool is a desktop app from the Lynx team: browser DevTools for an app running on a phone. Its panels are:
- Elements: inspect and change the elements on screen.
- Console: read logs and run JavaScript.
- Sources: debug your JavaScript.
- Layers: look at the app's layers in 3D to find rendering problems.
- Trace, in the top bar: record a performance trace. See Profiling performance.
To connect it:
- Download Lynx DevTool from its GitHub releases page.
- In Lynx Explorer, open the Switch page and turn on Lynx Debug and Lynx DevTool. The same switch makes Lynx Explorer reload your app when you save a file.
- Connect the phone to your computer with a USB cable. Lynx DevTool finds it.
Lynx's DevTool guide covers each panel in depth.
Show errors on screen
When nothing is connected, put the latest error on screen. This helps most in the apps ng build --platform makes, which show no error screen of their own.
Extend LynxErrorHandler so it copies each error into a signal:
Register it after provideRenderer(), which registers the default handler:
Then show it in your root component:
- Why a signal. Under zoneless change detection, Angular re-renders only when a signal changes. A template that reads a plain variable never updates.
- Errors outside Angular. An unhandled promise rejection, for example, skips the error handler. AngularLynx still saves it, as text in
globalThis.__lynxLastError. That's a plain string, so read it on demand, e.g. from a tap handler that copies it intoerrors.last. - Before you ship, remove the display. It's a development aid, not something to show users.
Related
- Handling errors: what happens when code throws, and
@boundary. - Profiling performance
LynxLoggerandLynxErrorHandlerAPI- Lynx DevTool guide on lynxjs.org
