-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (67 loc) · 2.65 KB
/
index.js
File metadata and controls
81 lines (67 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* This project showcases Better Stack Logs JavaScript integration for
* both backend and frontend code
*
* This code uses the @logtail/js package which can be used for both frontend and backend
*
* For more infromation visit https://github.com/logtail/logtail-js
*/
// Require Logtail Class for backend code (Node.js)
const { Node: Logtail } = require("@logtail/js");
// In frontend code use the following:
// import { Browser as Logtail } from "@logtail/js";
// Create a logger from a Logtail class
const logger = new Logtail(process.argv[2], {
sendLogsToConsoleOutput: true,
endpoint: `https://${process.argv[3]}`,
});
// Usage
// Send debug level log using the debug() method
const debugLog = logger.debug(`I am using Better Stack! (${process.title} v${process.versions?.[process.title]})`);
// Send info level log using the info() method
const infoLog = logger.info("An interesting event occurred!");
// Send warn level log using the warn() method
// You can add additional structured data to help you troubleshoot your code as shown below
const warningLog = logger.warn("Something is not quite right, better check on it.", {
user: {
username: "someuser",
email: "someuser@example.com",
},
additional_info: {
tried_accessing: "/url/of/error",
},
});
// Example of logging errors in catch clause
function callbackThatMightFail() {
throw new Error("Testing error");
}
let errorLog;
try {
callbackThatMightFail();
} catch (err) {
// Send error level log using the error() method
errorLog = logger.error("Oops! An runtime ERROR occurred!", err);
}
// Example of logging in a custom helper method, ensuring context.runtime still contains helpful info
function logWithNodeVersion(message) {
// The stackContextHint defines where in the call stack logger should look for the runtime context
// Now, context.runtime should contain information about where logWithNodeVersion() was called from
const stackContextHint = {
fileName: "index.js",
methodNames: ["logWithNodeVersion"],
};
const context = { nodeVersion: process.version };
return logger.log(message, "info", context, stackContextHint);
}
const customLog = logWithNodeVersion("Logging using custom helper function.");
// Logging methods are async function returning Promises
const logPromises = [debugLog, infoLog, warningLog, errorLog, customLog];
Promise.all(logPromises).then(function () {
console.info("All done! You can check your logs now.");
console.log("Logs created: ", logger.logged);
console.log("Successfully synced logs: ", logger.synced);
if (logger.logged !== logger.synced) {
console.error("Not all logs have been synced!");
process.exit(1);
}
});