New to logging?
See the Intro guide to Node.js logging and the Best practices for logging in Node.js.
Not using Better Stack yet?
Check out the Quick start guide to install and set up the Better Stack JavaScript client.
Get started with Better Stack in your JavaScript application using this overview of the Better Stack logger.
See the Intro guide to Node.js logging and the Best practices for logging in Node.js.
Check out the Quick start guide to install and set up the Better Stack JavaScript client.
Logtail JavaScript client instance provides 4 logging methods for the 4 default log levels. The log levels and their methods are:
debug()info()warn()error()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:
The Better Stack client automatically adds context to the logs.
The following JSON output is sent to Better Stack:
Quickly troubleshoot your application by logging structured data. Pass an object as a second argument to any logging method, like in the example below:
This creates the following JSON output:
Already logging with console.log()? Call replaceConsoleMethods() once after setting up the client.
Every console.debug(), console.log(), console.info(), console.warn() and console.error() call is then sent to Better Stack as well, and the console keeps printing as before:
The arguments are mapped the same way as in logtail.info(message, context):
console.log("%s logged in", user.name).Error becomes the error field with its message and stack trace. Every error is also printed in the message.console.log() is sent with the info level, the other methods with their own. Call restoreConsoleMethods() to put the original console back.
Update @logtail/node, @logtail/browser or @logtail/js first.
You can intercept every logged item and modify it before it's pushed to Better Stack. This could be useful, for example, for adding the current user's ID to the log or for filtering:
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:
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.