open-index/open-npm huggingface dataset:
npm package: @pagopa/logger
Description: A tiny and pluggable logger for `fp-ts` based projects.
Latest version: 1.0.1

# @pagopa/logger

A tiny and pluggable logger for `fp-ts` based projects.

## How to use it

First of all declare your concrete implementation of the logger

```typescript
// my-logger.ts

import * as L from "@pagopa/logger";

const ConsoleLogger: L.Logger = {
  // log function should return a IO<void>
  log: (r) => () => console.log(r),
  // specify the log formatter function
  // @pagopa/logger includes two formatters
  // - json (for structured logging)
  // - simple (for debug in local machine)
  format: L.format.json,
};
```

Use it in your code

```typescript
import * as RTE from "fp-ts/ReaderTaskEither"
import { pipe } from "fp-ts/function"

import * as L from "@pagopa/logger"

import { ConsoleLogger } from "./my-logger";

const greet = (name: string) => pipe(
  RTE.right(name),
  RTE.chainFirstW((name) => L.infoRTE("greet called", { name }))
  RTE.map(name => `Hello ${name}`),
);

greet("robert")({
  logger: ConsoleLogger,
});
```

When executed this code prints

```
{"timestamp":"1995-07-14T11:16:13.000Z", "level": "info", "message": "greet called", "name": "robert"}
```

Or (with `L.format.simple` formatter)

```
[INFO] greet called

{
  "timestamp": "1995-07-14T11:16:13.000Z",
  "name": "robert"
}
```

See the unit tests for other examples
npm package: @pagopa/logger-express
Description: `@pagopa/logger` adapter for express, inspired by [morgan](https://github.com/expressjs/morgan)
Latest version: 1.1.0

# @pagopa/logger-express

`@pagopa/logger` adapter for express, inspired by [morgan](https://github.com/expressjs/morgan)

this package includes two middlewares:

- `logger` to decorate `express.Request` with a `log` function
- `access` to enable HTTP request logs

## How to use it

Declare your concrete implementation of the logger

```typescript
// my-logger.ts

import * as L from "@pagopa/logger";

const ConsoleLogger: L.Logger = {
  log: (r) => () => console.log(r),
  format: L.format.json,
};
```

Use it in your express app

```typescript
import express from "express";

import * as L from "@pagopa/logger";
import { logger, access } from "@pagopa/logger-express";

import { ConsoleLogger } from "./my-logger";

const app = express();

// decorate "req" with "log" function
app.use(logger(ConsoleLogger));

// enable HTTP request logging
app.use(access());

app.get("/", (req, res) => {
  // logs with "info" level and name in context
  req.log?.("info", "Hello!!", {
    name: req.query.name,
  });
  res.json({
    message: "it works!",
  });
});

app.listen(3000);
```

The access middleware supports a `skip` option

```typescript
app.use(
  access({
    // skip successful response
    skip: (req, res) => res.statusCode === 200,
  })
);
```