
For a more detailed example setup, please see the JavaScript + Logtail example project on GitHub .
New to logging? See the Intro guide to Node.js logging and the Best practices for logging in Node.js
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.
For a more detailed example setup, please see the JavaScript + Logtail example project on GitHub .
New to logging? See the Intro guide to Node.js logging and the Best practices for logging in Node.js
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()
methodinfo()
methodWarn()
methoderror()
methodTo 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!");
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!"
}
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"
}
});
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"
}
}
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);