npm package: @inderes/logger
Description: Shared package library to be used in our backend services, and possibly in the frontend also.
Latest version: 1.5.4

# @inderes/logger

Shared package library to be used in our backend services, and possibly in the frontend also.

Bases on Pino https://getpino.io

## Quick start

Create a new logger instance with proper configuration:

```ts
const logger = new Logger({ context: 'MySweetShadow' });

logger.info('Fueled, these new shores burn');

logger.info('Dark past lies cold', { lyrics: 'Shadow, my sweet shadow' });

const error = new Error('To you, I look no more...');
logger.error('The end', error);
```

```json
[
  {
    "level": "info",
    "time": "2023-02-02T07:16:16.209Z",
    "context": "MySweetShadow",
    "message": "Fueled, these new shores burn"
  },
  {
    "level": "info",
    "time": "2023-02-02T07:16:16.209Z",
    "context": "MySweetShadow",
    "data": { "lyrics": "Shadow, my sweet shadow" },
    "message": "Dark past lies cold"
  },
  {
    "level": "error",
    "time": "2023-02-02T07:16:16.209Z",
    "context": "MySweetShadow",
    "data": {
      "error": {
        "type": "Error",
        "message": "To you, I look no more...",
        "stack": "Error: To you, I look no more...\n    at Object.<anonymous> (file:///Users/pitkane/code/inderes/videosync-monorepo/apps/videosync-api/src/index.ts:87:19)"
      }
    },
    "message": "The end"
  }
]
```

## Logger options

### name

Assign name for the logger. Name is printed on every log line. This helps to identify the source of the logging. In example API might have integration modules or classes, which can identify themselves by assigning name for the class.

```ts
const logger = new Logger({ name: 'HulabaloozaIntegration' });
```

**prettyPrint**

Default: `false`. Normally the output is JSON. Setting this `true` will change to output into string, and removes some other normally outputted information. Useful for local development.

**requestId**

```ts
const logger = new Logger({ requestId: uuid() });
```

Useful if you need to track the logs. Eg. in fastify/express server, start of the request you create a logger, set the tracindId, and assign the logger to request context. All of the processing and log outputs of the single request (request context) will include the same requestId. So with one id you will get the whole trace of the request from start to finish.

Fastify does this automatically. The example below is one GET request to /api/v1/examples, and it has the same `requestId` the whole way of the request.

```json
{"level":"info","time":"2022-07-27T08:16:45.654Z","name":"MySweetShadow","requestId":"ifK-HtSbgf9ZjQkMnA7W7","requestId":{"method":"GET","url":"/api/v1/examples","hostname":"localhost:3001","remoteAddress":"127.0.0.1","remotePort":55547},"message":"incoming request"}
{"level":"info","time":"2022-07-27T08:16:45.718Z","name":"MySweetShadow","requestId":"ifK-HtSbgf9ZjQkMnA7W7","moro":"jorma","message":"just testing logging"}
{"level":"info","time":"2022-07-27T08:16:45.718Z","name":"MySweetShadow","requestId":"ifK-HtSbgf9ZjQkMnA7W7","res":{"statusCode":200},"responseTime":64.16495898365974,"message":"request completed"}
```

**defaultData**

Object which is spread into every log object/line.

```ts
const logger = new Logger({ defaultData: { some: 'data' } });
logger.info('Hello world');
```

```json
{
  "level": "info",
  "time": "2022-07-27T08:19:49.428Z",
  "payload": {
    "some": "data"
  },
  "message": "Hello world"
}
```

**redactFields**

Removes wanted or sensitive fields from the output log object. For example we don't want to log out passwords, which are automatically redacted.

```ts
const logger = new Logger({ redactFields: ['secretString'] });
logger.info('secrets', { username: 'super', secretString: 'secret' });
```

```json
{
  "level": "info",
  "time": "2022-07-27T08:22:11.868Z",
  "payload": {
    "username": "super",
    "secretString": "[Redacted]"
  }
}
```

## Publishing to npm

When doing changes to the package, you need to publish the package to npm. This is done with changesets. Changesets are used to create a changelog, and to publish the package to npm.

### Create a changeset

```sh
npx changeset
```

If will ask you a series of questions, and then it will create a changeset file. This file is used to create the changelog, and to publish the package to npm.

### Publish the package

After the changeset file is created, you need to commit it to git. Then you can publish the package to npm.

To bump the version:

```sh
npx changeset version
```

This will bump the version.

And finally publish the package to npm:

```sh
npx changeset publish
```