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

# Logging

## Overview

`@marblejs/core` defines a pluggable **Logger** dependency registered by default to the context of every server factory. Having that you can inspect things like, registered context dependencies, mapped HTTP routes, request/response logs, I/O event traces, etc.

![](/files/-M0O8WSt6CgpR9kVX5bg)

## Logger

The interface allows you to define the log tag, type, main message and an optional level, which if not defined defaults to `LoggerLevel.INFO`.

```typescript
{
  tag: 'event_bus',
  type: 'saveOfferDocument$',
  message: 'Saving offer document...',
  level: LoggerLevel.INFO
}
```

```typescript
enum LoggerLevel { INFO, WARN, ERROR, DEBUG, VERBOSE };

interface LoggerOptions = {
  tag: string;
  type: string;
  message: string;
  level?: LoggerLevel;
};
```

Marble.js logger is an instance of IO monad which represents a synchronous effectful computation. In order to execute the logger you have to "run the `IO` action".

```haskell
Logger :: LoggerOptions -> IO<void>;
```

```typescript
import { LoggerTag, LoggerToken, LoggerLevel, useContext } from '@marblejs/core';
import { r } from '@marblejs/http';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { tap } from 'rxjs/operators';
import { UserDto } from './user.dto';

const foo$ = r.pipe(
  r.matchPath('/foo'),
  r.matchType('GET'),
  r.useEffect((req$, ctx) => {
    const logger = useContext(LoggerToken)(ctx.ask);

    const logMagic = logger({
      tag: LoggerTag.HTTP
      level: LoggerLevel.INFO,
      type: 'foo$',
      message: 'Here the magic happens...',
    });
            
    return req$.pipe(
      tap(logMagic),
      // ..
    ));
  });
```

## Custom logger

You can override the binding and map the stdout IO operations to a different destination (e.g. file or external service) when needed.

{% tabs %}
{% tab title="logger.reader.ts" %}

```typescript
import * as O from 'fp-ts/lib/Option';
import { pipe } from 'fp-ts/lib/function';
import { Logger, LoggerLevel, createReader } from '@marblejs/core';
import { CustomLogger } from './customLogger';

export const CustomLoggerReader = createReader<Logger>(() => opts => {
  const tag = opts.tag;
  const level = opts.level ?? LoggerLevel.INFO;
  const message = `${opts.type} ${opts.message}`;
  const log = pipe(
    O.fromNullable({
      [LoggerLevel.ERROR]: CustomLogger.error,
      [LoggerLevel.INFO]: CustomLogger.log,
      [LoggerLevel.WARN]: CustomLogger.warn,
    }[level),
    O.getOrElse(() => CustomLogger.log),
  );

  return () => log({ tag, message });
});
```

{% endtab %}
{% endtabs %}

Then in your server definition you have to override the binding.

```typescript
import { bindTo, LoggerToken } from '@marblejs/core';
import { createServer } from '@marblejs/http';
import { CustomeLoggerReader } from './logger.reader.ts';

const server = createServer({
  // ...
  dependencies: [
    bindTo(LoggerToken)(CustomLoggerReader),
  ],
});
```


---

# 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://marblejs.gitbook.io/docs/http/advanced/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.
