> For the complete documentation index, see [llms.txt](https://docs.balena.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.balena.io/reference/sdk/node-sdk/latest/logs.md).

# Logs

`balena.logs` : `object`

**Kind**: static namespace

***

## history

`balena.logs.history(uuidOrId, [options])` ⇒ `Promise`

Get an array of the latest log messages for a given device.

**Kind**: static method of [`logs`](#balena.logs)\
**Summary**: Get device logs history\
**Access**: public\
**Fulfil**: `Object[]` - history lines

| Param            | Type                 | Default | Description                                                                  |
| ---------------- | -------------------- | ------- | ---------------------------------------------------------------------------- |
| uuidOrId         | `String` \| `Number` |         | device uuid (string) or id (number)                                          |
| \[options]       | `Object`             |         | options                                                                      |
| \[options.count] | `Number` \| `'all'`  | `1000`  | number of log messages to return (or 'all')                                  |
| \[options.start] | `Number` \| `String` |         | the timestamp or ISO Date string after which to retrieve historical messages |

**Example**

```js
balena.logs.history('7cf02a69e4d34c9da573914963cf54fd').then(function(lines) {
	lines.forEach(function(line) {
		console.log(line);
	});
});
```

**Example**

```js
balena.logs.history(123).then(function(lines) {
	lines.forEach(function(line) {
		console.log(line);
	});
});
```

**Example**

```js
const oneDayAgoTimestamp = Date.now() - 24*60*60*1000;
balena.logs.history('7cf02a69e4d34c9da573914963cf54fd', { start: oneDayAgoTimestamp }).then(function(lines) {
	lines.forEach(function(line) {
		console.log(line);
	});
});
```

**Example**

```js
const oneDayAgoIsoDateString = new Date(Date.now() - 24*60*60*1000).toISOString();
balena.logs.history('7cf02a69e4d34c9da573914963cf54fd', { start: oneDayAgoIsoDateString }).then(function(lines) {
	lines.forEach(function(line) {
		console.log(line);
	});
});
```

***

## subscribe

`balena.logs.subscribe(uuidOrId, [options])` ⇒ `Promise.<LogSubscription>`

Connects to the stream of devices logs, returning a LogSubscription, which can be used to listen for logs as they appear, line by line.

**Kind**: static method of [`logs`](#balena.logs)\
**Summary**: Subscribe to device logs\
**Access**: public\
**Fulfil**: [`LogSubscription`](#balena.logs.LogSubscription)

| Param            | Type                 | Default | Description                                                                                                                                  |
| ---------------- | -------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| uuidOrId         | `String` \| `Number` |         | device uuid (string) or id (number)                                                                                                          |
| \[options]       | `Object`             |         | options                                                                                                                                      |
| \[options.count] | `Number` \| `'all'`  | `0`     | number of historical messages to include (or 'all')                                                                                          |
| \[options.start] | `Number` \| `String` |         | the timestamp or ISO Date string after which to retrieve historical messages. When specified, the count parameter needs to also be provided. |

**Example**

```js
balena.logs.subscribe('7cf02a69e4d34c9da573914963cf54fd').then(function(logs) {
	logs.on('line', function(line) {
		console.log(line);
	});
});
```

**Example**

```js
balena.logs.subscribe(123).then(function(logs) {
	logs.on('line', function(line) {
		console.log(line);
	});
});
```

***

## LogSubscription

`balena.logs.LogSubscription` : `EventEmitter`

The log subscription emits events as log data arrives. You can get a LogSubscription for a given device by calling `balena.logs.subscribe(deviceId)`

**Kind**: static typedef of [`logs`](#balena.logs)

***

### unsubscribe

`balena.logs.LogSubscription.unsubscribe()`

Disconnect from the logs feed and stop receiving any future events on this emitter.

**Kind**: static method of [`LogSubscription`](#balena.logs.LogSubscription)\
**Summary**: Unsubscribe from device logs\
**Access**: public\
**Example**

```js
logs.unsubscribe();
```

***

### error

`balena.logs.LogSubscription.event:error`

**Kind**: event emitted by [`LogSubscription`](#balena.logs.LogSubscription)\
**Summary**: Event fired when an error has occured reading the device logs\
**Example**

```js
logs.on('error', function(error) {
	console.error(error);
});
```

***

### line

`balena.logs.LogSubscription.event:line`

**Kind**: event emitted by [`LogSubscription`](#balena.logs.LogSubscription)\
**Summary**: Event fired when a new line of log output is available\
**Example**

```js
logs.on('line', function(line) {
	console.log(line);
});
```

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.balena.io/reference/sdk/node-sdk/latest/logs.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
