JavaScript advanced usage | Better Stack Documentation

Explore documentation

Get started with Logtail in your JavaScript application using this overview of the Logtail logger.

Log levels

Logtail JavaScript client instance provides 4 logging methods for the 4 default log levels. The log levels and their methods are:

  • DEBUG - Send debug messages with debug()
  • INFO - Keep track of your app's progress with info()
  • WARN - Report non-critical issues with warn()
  • ERROR - Log serious problems with error()

Logging example

To send a log message of selected log level, use the corresponding method. In this example, we send the DEBUG level log and ERROR level log:

Send logs to Logtail

Copied!

The Logtail client automatically adds context to the logs.
The following JSON output is sent to Logtail:

Logging structured data

Quickly troubleshoot your application by logging structured data. Pass an object as a second argument to any logging method, like in the example below:

Send structured logs to Logtail

Copied!

This creates the following JSON output:

Middleware

You can intercept every logged item and modify it before it's pushed to Logtail. This could be useful, for example, for adding the current user's ID to the log or for filtering:

Enrich logs Filter logs

Copied!

TypeScript

If you're using Logtail in a TypeScript codebase, you can take advantage of our types. You could write the enriching middleware function with types like this:

Enrich logs in TypeScript

Copied!