# custom-pino-logger

> Pino logger customized

Latest version **1.0.5** (published 2021-07-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install custom-pino-logger
pnpm add custom-pino-logger
yarn add custom-pino-logger
bun add custom-pino-logger
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2021-07-23 |
| First published | 2021-07-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 22.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | avn194 |
| Maintainers | brechtvalcke, thuurvdp, edbosscom, avn194 |
| Keywords | Pino, Logging, Logger, Interceptor, Tracing |

## Links

- npm: https://www.npmjs.com/package/custom-pino-logger
- npm.io page: https://npm.io/package/custom-pino-logger

## Dependencies (5)

- [pino](https://npm.io/package/pino.md) ^6.12.0
- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [express](https://npm.io/package/express.md) ^4.17.1
- [cls-hooked](https://npm.io/package/cls-hooked.md) ^4.2.2
- [pino-pretty](https://npm.io/package/pino-pretty.md) ^5.1.1

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.5 (latest) — 2021-07-23
- 1.0.4 — 2021-07-22
- 1.0.3 — 2021-07-22
- 1.0.2 — 2021-07-22
- 1.0.1 — 2021-07-20
- 1.0.0 — 2021-07-20

## README

# Customized Pino logger

Customized logging with Pino and includes HTTP middleware for request tracing + a request logging interceptor.

## Configuring the logger for Node Express

```
import { express } from 'express';
import { LoggerService } from 'custom-pino-logger';

const app = express();

LoggerService.register();
const logger = LoggerService.getLogger();

logger.info('Log message here', { context: 'MyApp' });

app.listen(3000, () => {
    logger.info('Example app listening at http://localhost:3000',
    { context: 'MyApp' });
});
```

**Outputs:**

```
[2021-07-20 07:49:03] INFO: [MyApp]: Log message here
    environment: "ENVIRONMENT NOT SET"
    context: "MyApp"
[2021-07-20 07:49:03] INFO: [MyApp]: Example app listening at http://localhost:3000
    environment: "ENVIRONMENT NOT SET"
    context: "MyApp"
```

#### Register method

The `register` method should be only called once and can contain a configuration parameter. The default values are:

```
{
    environment: 'ENVIRONMENT NOT SET',
    logLevel: 'trace',
    enableTraceId: false,
    masking: false,
    prettyPrint: true
}
```

**Note:** the logger should only get registered once. Afterwards you can simply use it by calling the `getLogger` method.

```
const logger = LoggerService.getLogger();
```

#### Log level

The `logLevel` setting can contain one of the values below:

```
type LogLevel =
    | 'silent'
    | 'trace'
    | 'debug'
    | 'info'
    | 'warn'
    | 'error'
    | 'fatal';
```

When you set a log level, only the logs with a more serious log level are displayed. For example, setting `warn` will only display the logs with levels `warn`, `error` and `fatal`. 

The log levels `trace`, `debug` or `info` are commonly used for development environments.
The log level `warn` is mostly used for production environments.

#### Enable trace ID

When enabled, the `enableTraceId` sets a unique `traceId` per request. Note, this is only possible when the tracing middleware is used.
The logger should be accessible from the request and the `traceId` as well.

```
import { LoggerService, httpTraceMiddleware, traceIdHeaderName } from 'custom-pino-logger';
...
const logger = LoggerService.getLogger();

app.use(httpTraceMiddleware);

app.get('/', (req, res, next) => {
    req.logger.info('Trace ID logging included', {
        context: 'MyApp',
        accessTraceId: req.headers[traceIdHeaderName]
    });

    next();
});
...
```

**Outputs:**

```
[2021-07-16 16:53:12] INFO: [MyApp]: Trace ID logging included
    environment: "ENVIRONMENT NOT SET"
    traceId: "2b9d637b-5c63-4c66-aa6c-df8828242977"
    context: "MyApp",
    accessTraceId: "2b9d637b-5c63-4c66-aa6c-df8828242977"
```

#### Masking

To redact/remove sensitive information from the logs you can set the `masking` setting to `true`.

Currently these nested properties will be removed from the logs:

```
[
    '*.password',
    '*.newPassword',
    '*.oldPassword',
    '*.accessToken',
    '*.refreshToken',
    '*.token',
    '*.jwtToken',
    '*.apiKey',
]
```

#### Pretty print

When turned on, pretty, multi-line logs will be output. When turned off, one-line stringified JSON will be output.
Advised to turn this off in staging or production environments.

### HTTP interceptor

HTTP request can be intercepted and logged automatically by simply adding it as middleware.

```
import { LoggerService, loggerInterceptor } from 'custom-pino-logger';
...
const logger = LoggerService.getLogger();

app.use(loggerInterceptor(logger));
...
```

**Outputs:**

```
[2021-07-16 16:53:12] INFO: [Request]: GET /
    environment: "ENVIRONMENT NOT SET"
    traceId: "2b9d637b-5c63-4c66-aa6c-df8828242977"
    context: "Request"
    status: 200
    duration: "1ms"
    params: {}
    query: {}
    host: "localhost"
```


## Sources

- https://itnext.io/step-by-step-building-and-publishing-an-npm-typescript-package-44fe7164964c
- https://www.npmjs.com/package/pino

---
_Source: https://npm.io/package/custom-pino-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
