#Enriching Logs in Baselime


Baselime enables you to enrich your logs with special fields to enable deeper insights into your application's performance and traceability.

The available fields are:

  • requestId
  • duration
  • traceId
  • error
  • namespace
  • service

You can add those fields to your logs to enable the Requests view in Baselime.


#Grouping your logs by request

Baselime enables you to group your logs by request by adding a requestId field to all the logs and events within the same request.

In an Express.js server:

const express = require('express'); const crypto = require('crypto'); const app = express(); app.use((req, res, next) => { // Generate a unique request ID const requestId = crypto.randomUUID(); // Attach the requestId to the request object req.requestId = requestId; // Continue to the next middleware or route next(); }); app.get('/example', (req, res) => { // Access the requestId from the request object const requestId = req.requestId; // Add the requestId to the log console.log(JSON.stringify({ message: `Hello, World!`, requestId })); // Continue your route logic });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#Measuring Request Duration

Add the duration field to at least one log or event from a request to measure its duration and enable analytics on request durations in Baselime.

For example, in an Express.js server:

const express = require('express'); const crypto = require('crypto'); const app = express(); app.use((req, res, next) => { const requestId = crypto.randomUUID(); req.requestId = requestId; next(); }); app.get('/example', (req, res) => { const startTime = Date.now(); // Your route logic here const requestId = req.requestId; const duration = Date.now() - startTime; console.log(JSON.stringify({ message: `End of the request`, requestId, duration })); });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#Capturing errors in your events

Add the error field to any event where an error occured in your application. The value of the error field must be a string. Any event with the error field will be captured by the Baselime automatic error-tracking.

For example:

function willThrow() { try { throw new Error("An error message"); } catch (error) { console.log(JSON.stringify({ message: "There was an error", error: error.message })); } }
1
2
3
4
5
6
7

#Grouping logs by namespace or path

Baselime enables you to group your logs by namespace or path by adding a namespace field to at least one log from a given request.

In an Express.js server:

const express = require('express'); const crypto = require('crypto'); const app = express(); app.use((req, res, next) => { const requestId = crypto.randomUUID(); req.requestId = requestId; next(); }); app.get('/example', (req, res) => { const requestId = req.requestId; // Add the namespace to the log console.log(JSON.stringify({ message: `Hello, World!`, requestId, namespace: "/example" })); // Continue your route logic });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#Grouping logs by service

Baselime enables you to group your logs by service by adding a service field to all logs from the same service.

For example:

const SERVICE = "queue-processor"; function backgrounJob(data) { console.log(JSON.stringify({ message: "Starting processing", service: SERVICE, namespace: data.id })) complexCalculation(); console.log(JSON.stringify({ message: "Processing completed", service: SERVICE, namespace: data.id })) }
1
2
3
4
5
6
7
8
9

#Correlating logs and traces

Refer to the Correlate Logs with Traces section.


#Usage

Once you have enriched your logs with the additional fields, your data will show in Baselime in the Requests view.

Enriched logs in Baselime
Enriched logs in Baselime