# @tinyhttp/logger

> Minimal and flexible HTTP logger.

Latest version **2.1.0** (published 2025-03-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tinyhttp/logger
pnpm add @tinyhttp/logger
yarn add @tinyhttp/logger
bun add @tinyhttp/logger
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2025-03-03 |
| First published | 2020-06-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=14.18 \|\| >=16.20 |
| Dependencies | 3 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | v1rtl |
| Maintainers | dropthebeatbro |
| Keywords | tinyhttp, node.js, web framework, web, backend |

## Links

- npm: https://www.npmjs.com/package/@tinyhttp/logger
- Repository: https://github.com/tinyhttp/logger
- Homepage: https://github.com/tinyhttp/logger#readme
- npm.io page: https://npm.io/package/@tinyhttp/logger

## Dependencies (3)

- [dayjs](https://npm.io/package/dayjs.md) ^1.11.13
- [colorette](https://npm.io/package/colorette.md) ^2.0.20
- [http-status-emojis](https://npm.io/package/http-status-emojis.md) ^2.2.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2025-03-03
- 0.3.16 (pnpm-temp) — 2020-08-17
- 2.0.0 — 2023-10-27
- 1.3.4 — 2023-05-14
- 1.3.3 — 2023-03-19
- 1.3.2 — 2023-03-05
- 1.3.0 — 2021-04-26
- 1.2.4 — 2021-04-14
- 1.2.3 — 2021-04-02
- 1.2.2 — 2021-03-26
- 1.2.1 — 2021-02-27
- 1.2.0 — 2021-02-21
- 1.1.14 — 2021-02-09
- 1.1.13 — 2021-01-27
- 1.1.12 — 2021-01-27
- … 145 more at https://npm.io/package/@tinyhttp/logger/versions

## README

<div align="center">

# @tinyhttp/logger

[![npm][npm-img]][npm-url] [![GitHub Workflow Status][gh-actions-img]][github-actions] [![Coverage][cov-img]][cov-url]

</div>

Minimal and flexible HTTP logger

## Install

```sh
pnpm i @tinyhttp/logger
```

## API

```ts
import { logger } from '@tinyhttp/logger'
```

### `logger(options)`

Returns the middleware for logging HTTP requests.

#### Options

- `methods`: a list of HTTP methods to log. Defaults to `http`'s `METHODS`.
- `timestamp.format`: timestamp format. It is consumed by the [dayjs](https://day.js.org) library. If a string is specified, it is used as a format; otherwise just enabled.
- `output.callback`: a function that receives the log generated by the logger.
- `output.color`: a property that determines whether the logger will generate a message with color. Useful for logging into the console; disable if logging into a file or other colorless environments.
- `emoji`: enable emojis for HTTP status codes. See [http-status-emojis](https://github.com/bendrucker/http-status-emojis/blob/master/index.js) for a full list.
- `ip`: log IP address.

## Example

```ts
import { App } from '@tinyhttp/app'
import { logger } from '@tinyhttp/logger'

new App()
  .use(
    logger({
      methods: ['GET', 'POST'],
      timestamp: { format: 'HH:mm:ss' },
      output: { callback: console.log, color: false }
    })
  )
  .get('/', (req, res) => res.send('Hello world'))
  .post('/', (req, res) => res.send('Sent POST'))
  .listen(3000)
```

To Log a level, use the enum `LogLevel`

```ts
import { App } from '@tinyhttp/app'
import { logger, LogLevel } from '@tinyhttp/logger'

new App()
  .use(
    logger({
      methods: ['GET', 'POST'],
      timestamp: { format: 'HH:mm:ss' },
      output: { callback: console.log, color: false, level: LogLevel.warn }
    })
  )
  .get('/', (req, res) => res.send('Hello world'))
  .listen(3000)
```

This also includes a simple file logger. To stream to a file, simply supply the filename in the options. Supported file names innclude
`./file.log` or `./log/tiny.log`

```ts
import { App } from '@tinyhttp/app'
import { logger } from '@tinyhttp/logger'

new App()
  .use(
    logger({
      methods: ['GET', 'POST'],
      timestamp: { format: 'HH:mm:ss' },
      output: { callback: console.log, color: false, filename: './log/tiny.log' }
    })
  )
  .get('/', (req, res) => res.send('Hello world'))
  .listen(3000)
```

## Alternatives

- [Pino HTTP](https://github.com/pinojs/pino-http) - high-speed HTTP logger for Node.js
- [chrona](https://github.com/xambassador/chrona) - Simple HTTP request logger middleware for express.js inspired from koa-logger, written in typescript.

[npm-url]: https://npmjs.com/package/@tinyhttp/logger
[github-actions]: https://github.com/tinyhttp/logger/actions
[gh-actions-img]: https://img.shields.io/github/actions/workflow/status/tinyhttp/logger/ci.yml?branch=master&style=for-the-badge&color=hotpink&label=&logo=github
[cov-img]: https://img.shields.io/coveralls/github/tinyhttp/logger?style=for-the-badge&color=hotpink
[cov-url]: https://coveralls.io/github/tinyhttp/logger
[npm-img]: https://img.shields.io/npm/dt/@tinyhttp/logger?style=for-the-badge&color=hotpink

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