# @marblejs/middleware-logger

**Simple** middleware for request logging inside your console. It displays the outgoing request events using the following format:

```
{HTTP_METHOD} {PATH} {HTTP_STATUS} {TIME}
```

```
POST /api/v1/user 200 1ms
```

### Installation

```bash
$ yarn add @marblejs/middleware-logger
```

Requires `@marblejs/core` to be installed.

### Importing

```typescript
import { logger$ } from '@marblejs/middleware-logger';
```

### Type declaration

```
logger$ :: LoggerOptions -> HttpMiddlewareEffect
```

### **Parameters**

| *parameter* | definition                  |
| ----------- | --------------------------- |
| *options*   | \<optional> `LoggerOptions` |

#### ***LoggerOptions***

| *parameter* | definition                           |
| ----------- | ------------------------------------ |
| *silent*    | \<optional> `boolean`                |
| *filter*    | \<optional> `HttpRequest -> boolean` |

### Usage

1. Default behavior. Log every response to *process*.*stdout*:

```typescript
import { httpListener } from '@marblejs/core';
import { logger$ } from '@marblejs/middleware-logger';

const middlewares = [
  logger$(),
  ...
];

export const listener = httpListener({
  middlewares,
  effects: [],
});
```

2. Customized logging behavior:

```typescript
import { httpListener } from '@marblejs/core';
import { logger$ } from '@marblejs/middleware-logger';
import { isTestEnv } from './util';

const middlewares = [
  logger$({
    silent: isTestEnv(),
    filter: req => req.response.statusCode >= 400;
  }),
  ...
];

export const listener = httpListener({
  middlewares,
  effects: [],
});
```

* **silent** - When `true` the logging is turned off (usually useful during testing),
* **filter** - Filter outgoing responses or incoming requests based on given predicate. For example we can log only HTTP status codes above *400*.


---

# Agent Instructions: 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:

```
GET https://marblejs.gitbook.io/docs/other/api-reference/middleware-logger.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
