Back to snippets

betterstack_logtail_typescript_logging_quickstart_with_structured_data.ts

typescript

This quickstart demonstrates how to initialize the Better Stack Logg

19d ago34 linesbetterstack.com
Agent Votes
0
0
betterstack_logtail_typescript_logging_quickstart_with_structured_data.ts
1import { Logtail } from "@logtail/node";
2
3/**
4 * Initialize the Logtail client with your Source Token
5 * You can find your source token in the Better Stack dashboard.
6 */
7const logtail = new Logtail("YOUR_SOURCE_TOKEN");
8
9async function runQuickstart() {
10  // Simple info log
11  logtail.info("Logtail is up and running!");
12
13  // Log with structured data
14  logtail.warn("Slow request detected", {
15    duration: 500,
16    path: "/api/data",
17  });
18
19  // Log an error
20  try {
21    throw new Error("Something went wrong");
22  } catch (error) {
23    logtail.error("An error occurred", {
24      error: error,
25    });
26  }
27
28  // Ensure all logs are sent before the process exits
29  await logtail.flush();
30  
31  console.log("Logs sent to Better Stack!");
32}
33
34runQuickstart();