> For the complete documentation index, see [llms.txt](https://docs.minded.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.minded.com/sdk/logging.md).

# Logging

MindedJS provides a structured logging system that helps you monitor, debug, and audit your agent's behavior. The logger is available throughout your agent code and provides consistent, contextual logging with proper PII handling.

## Using the Logger

Import the logger from MindedJS:

```ts
import { logger } from 'mindedjs';
```

The logger is also available in all tool contexts, so you can use it directly in your tools without importing.

## Log Levels

The logger supports standard log levels:

```ts
// Info level - general information
logger.info('Processing customer request');

// Warning level - potential issues
logger.warn('Customer tier not found, using default');

// Error level - errors and exceptions
logger.error('Failed to process payment');

// Debug level - detailed debugging information
logger.debug('Memory state updated');
```

## Structured Logging

Use structured logging with contextual data for better observability. Pass the message as the `msg` property:

```ts
// Basic structured logging
logger.info({
  msg: 'Order refunded',
  orderId: 'ORD-123',
  customerId: 'CUST-456',
  amount: 99.99,
});

// Include session context in tools
logger.info({
  msg: 'Tool execution started',
  sessionId: state.sessionId,
  toolName: 'refundOrder',
  orderId: input.orderId,
});
```

## Log Configuration

The logger can be configured through environment variables:

```env
# Set log level (debug, info, warn, error)
LOG_LEVEL=info

# In development, you might want debug level
LOG_LEVEL=debug
```

## Best Practices

1. **Always Include Session Context**: Include `sessionId` in all tool-related logs
2. **Use Appropriate Log Levels**: Don't use `info` for debugging information
3. **Structure Your Data**: Use objects for structured logging rather than strings
4. **Log State Changes**: Log important state transitions and memory updates
5. **Error Context**: Use `err` parameter for error objects to get proper stack traces
6. **Performance Metrics**: Log timing information for performance monitoring


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.minded.com/sdk/logging.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
