Version: 5.2.x

Read Logs

Read logs#

To read existing logs you will need to make a TLS request to the OpenHIM API for the below method and endpoint.

Method: GET
Endpoint: {openhim_url}:8080/logs?[filters]

By default the logs with level info and above for the last 5 minutes are returned. The logs will be returned as an ordered array. A maximum of 100 000 log messages are returned (hint: use pagination).

The following filters are available:

  • from - an ISO8601 formatted date to query from. Defaults to 5 mins ago.
  • until - an ISO8601 formatted date to query until. Defaults to now.
  • start - a number n: the log message to start from, if specified the first n message are NOT returned. Useful along with limit for pagination. Defaults to 0.
  • limit - a number n: the max number of log messages to return. Useful along with start for pagination. Defaults to 100 000.
  • level - The log level to return. Possible values are debug, info, warn and error. All messages with a level equal to or of higher severity to the specified value will be returned. Defaults to info.

Example#

Before we can send our request to the OpenHIM API we need to ensure that we construct our valid HTTP headers to successfully authenticate with the OpenHIM API.

Copy the below code at the bottom of your nodejs script that handles the authentication of the OpenHIM headers as described in the authentication section.

Replace the openhimOptions values with the correct implementation details

// append below code to the "openhim-api.js" script containing the authentication methods.
// This is described within the authentication section
(async () => {
const openhimOptions = {
apiURL: 'https://localhost:8080',
apiEndpoint: '/logs',
username: '[email protected]',
password: 'openhim-password',
rejectUnauthorized: false
}
const headers = await genAuthHeaders(openhimOptions)
const options = { method: 'GET',
url: `${openhimOptions.apiURL}${openhimOptions.apiEndpoint}`,
rejectUnauthorized: openhimOptions.rejectUnauthorized,
headers: headers,
qs: {
limit: 5
}
}
request(options, (error, response, body) => {
if (error) throw new Error(error)
console.log({
statusCode: response.statusCode,
body
})
})
})()

The response status code will be 200 if successful.