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. On Android, adb logcat shows it under the tag lynx. To read logs without Lynx DevTool, use LynxLogger.
- Lynx's
consolehas fewer methods.log,info,warn,error,debug,time,timeEnd,group,groupEnd,tableandcountwork. There's notrace,assertordir, so calling one throws. console.error()also reports an error. Lynx passes each call to the app as an error, as it does an uncaught one.
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. The app sends each line to the dev server it loaded from, over your network, so it arrives wherever the app can load. It works from any code, including event handlers.
- The file. Lines go to
.dev-logsin the folder you ranng servefrom.ng serveprints its full path when it starts, and each run starts the file fresh.ng addadds.dev-logsto your.gitignore. - The methods.
log(),info(),warn(),error()anddebug(). The level shows in each line. - Objects and errors. Objects and arrays print as indented JSON, and an
Errorprints with its stack. Other values print asString()makes them. - Development only.
LynxLoggerdoes nothing in production builds, so you can leave the calls in. A shipped app sends nothing.
.dev-logs?
Check that ng serve builds a development build. ng serve -c production, or a build target without a development configuration, serves a production build, where LynxLogger does nothing.
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 lets the dev server reload your app when you save a change it can't apply in place.
- 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 --platform builds, with ng build or ng serve, 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
