# koa-pretty-logger

> Pretty logger middleware for Koa.

Latest version **1.0.3** (published 2023-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-pretty-logger
pnpm add koa-pretty-logger
yarn add koa-pretty-logger
bun add koa-pretty-logger
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2023-09-26 |
| First published | 2021-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 102.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Giovanni Moscati |
| Maintainers | giovannimoscati |
| Keywords | koa, logger |

## Links

- npm: https://www.npmjs.com/package/koa-pretty-logger
- Repository: https://github.com/giovannimoscati/koa-pretty-logger
- Homepage: https://github.com/giovannimoscati/koa-pretty-logger#readme
- Issues: https://github.com/giovannimoscati/koa-pretty-logger/issues
- npm.io page: https://npm.io/package/koa-pretty-logger

## Dependencies (3)

- [bytes](https://npm.io/package/bytes.md) ^3.1.2
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [humanize-number](https://npm.io/package/humanize-number.md) ^0.0.2

## 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.3 (latest) — 2023-09-26
- 1.0.2 — 2021-11-02
- 1.0.1 — 2021-11-01
- 1.0.0 — 2021-11-01
- 0.0.0 — 2021-11-01

## README

# koa-pretty-logger

Koa simple middleware for logging the entire HTTP call flow.

* [Installation](#installation)
* [Usage](#usage)
    * [Simple Usage](#simple-usage)
    * [Log with custom fields](#log-with-custom-fields)
    * [Exclude log](#exclude-log)
* [Options](#options)

## Installation

```bash
yarn add koa-pretty-logger
```

or

```bash
npm install koa-pretty-logger
```

## Usage

### Simple Usage

```javascript
const koa = require('koa');
const logger = require('koa-pretty-logger');

const app = new koa()

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

Or, if you use ES6 Modules:

```javascript
import koa from 'koa';
import logger from 'koa-pretty-logger';

const app = new koa()

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

Output:
```bash
curl localhost

request
{
  time: '2021-10-31T09:41:27.981Z',
  timestamp: 1635586887981,
  name: 'my-app-package-name',
  version: '1.0.0',
  ip: '::1',
  method: 'GET',
  url: '/v1/users'
}

/* request processing... */

response
{
  time: '2021-10-31T09:41:28.556Z',
  timestamp: 1635586888556,
  name: 'my-app-package-name',
  version: '1.0.0',
  ip: '::1',
  method: 'GET',
  url: '/v1/users',
  status: 200,
  response_size: '105b',
  response_time: '577ms'
}
```

### Log with custom fields

```javascript
import koa from 'koa';
import logger from 'koa-pretty-logger';

const app = new koa()

// log with the additional fields "customer" and "auth_key"
app.use(
  logger({
    addFields: (ctx) => ({
      customer: 'my-customer',
      auth_key: ctx.request.headers['x-auth_key'] 
    })
  })
);
```

### Exclude log
```javascript
import koa from 'koa';
import logger from 'koa-pretty-logger';

const app = new koa()

// exlude healtcheck log
app.use(
    logger({
      exclude: (ctx) => ctx.path.includes('healthcheck'),
    })
  );
```

## Options
- `logger`: function to log info with, default is `console.log`
- `addFields`: function receiving `ctx` as argument and returning additional properties to log
- `exclude`: function receiving `ctx` as argument and returning `bool` indicating whether to not log request

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