Logging in JavaScript with Logtail

To help you get started with using Logtail in your javascript projects, here is an overview of how to use the Logtail logger in your javascript apps.

The logger instance we created in the installation section is used to send log messages to Logtail. It provides 4 logging methods for the 4 default log levels. The log levels and their method are:

  • DEBUG¬†- Send debug messages using the¬†debug()¬†method
  • INFO¬†- Send informative messages about the application progress using the¬†info()¬†method
  • WARN¬†- Report non-critical issues using the¬†Warn()¬†method
  • ERROR¬†- Send messages about serious problems using the¬†error()¬†method

Logging example

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

// Send debug level log using the debug() method
logger.debug("I am using Logtail!");

// Send error level log using the error() method
logger.error("Oops! An runtime ERROR occurred!");
Copied!

This will create the following JSON output:

{
   "dt":"2022-02-01 12:01:10.127 UTC",
   "context":{
      "runtime":{
         "column_integer":"21",
         "file_string":"node_modules/@logtail/core/dist/cjs/base.js",
         "line_integer":"141",
         "type_string":"Node",
         "function_string":"debug",
         "method_string":"debug"
      },
      "system":{
         "main_file_string":"/mnt/d/js_logtail/index.js",
         "pid_integer":"4193"
      }
   },
   "level_string":"debug",
   "message_string":"I am using Logtail!"
}

{
   "dt":"2022-02-01 12:01:10.127 UTC",
   "context":{
      "runtime":{
         "column_integer":"8",
         "file_string":"index.js",
         "line_integer":"40",
         "type_string":"Object"
      },
      "system":{
         "main_file_string":"/mnt/d/js_logtail/index.js",
         "pid_integer":"4193"
      }
   },
   "level_string":"error",
   "message_string":"Oops! An runtime ERROR occurred!"
}

Copied!

Logging structured data

You can log additional structured data to help you troubleshoot your application much quicker. Pass the structured data as the second argument to the select login method as shown in the example below:

logger.warn("Something is not quite right, better check on it.",{
    user:{
        username:"someuser",
        email:"[email protected]"
    },
    additional_info:{
        tried_accessing: "/url/of/error"
    }
});
Copied!

This will create the following JSON output:

{
   "dt":"2022-02-01 12:01:10.127 UTC",
   "context":{
      "runtime":{
         "column_integer":"8",
         "file_string":"index.js",
         "line_integer":"29",
         "type_string":"Object"
      },
      "system":{
         "main_file_string":"/mnt/d/js_logtail/index.js",
         "pid_integer":"4193"
      }
   },
   "level_string":"warn",
   "message_string":"Something is not quite right, better check on it.",
   "additional_info":{
      "tried_accessing_string":"/url/of/error"
   },
   "user":{
      "email_string":"[email protected]",
      "username_string":"someuser"
   }
}
Copied!

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:

// intercept the log and add userId to the content
async function enrichLogs(log) {
    return {
        ...log,
        userId: getCurrentUserId()
    };
}

// tell logtail to use the intercept function
logtail.use(enrichLogs);
Copied!