🪵 Minimal and flexible HTTP logger
Go to file
v1rtl bdff8eb152
Merge pull request #4 from aarontravass/patch-1
Remove `@tinyhttp/logger` from `Alternatives`
2023-11-06 23:47:58 +02:00
.github feat: move to bun + tsc, raise target to 14.18+ and 16.20+ 2023-10-27 16:36:56 +03:00
.husky chore: use bun for husky 2023-10-27 16:41:23 +03:00
src feat: move to bun + tsc, raise target to 14.18+ and 16.20+ 2023-10-27 16:36:56 +03:00
tests feat: move to bun + tsc, raise target to 14.18+ and 16.20+ 2023-10-27 16:36:56 +03:00
.eslintrc feat: move to bun + tsc, raise target to 14.18+ and 16.20+ 2023-10-27 16:36:56 +03:00
.gitattributes fix: remived existSync and used accessSync instead 2023-05-27 17:27:15 -04:00
.gitignore feat: move to bun + tsc, raise target to 14.18+ and 16.20+ 2023-10-27 16:36:56 +03:00
.prettierrc Initial commit 2021-07-07 15:26:01 +03:00
LICENSE Initial commit 2021-07-07 15:26:01 +03:00
README.md Update README.md 2023-11-06 00:11:29 -05:00
bun.lockb fix: versions 2023-10-27 16:37:40 +03:00
commitlint.config.cjs Initial commit 2021-07-07 15:26:01 +03:00
package.json 2.0.0 2023-10-27 16:43:29 +03:00
pnpm-lock.yaml test: fix hanging tests 2023-05-30 19:32:50 -04:00
tsconfig.eslint.json feat: move to bun + tsc, raise target to 14.18+ and 16.20+ 2023-10-27 16:36:56 +03:00
tsconfig.json feat: move to bun + tsc, raise target to 14.18+ and 16.20+ 2023-10-27 16:36:56 +03:00

README.md

@tinyhttp/logger

npm GitHub Workflow Status Coverage

Minimal and flexible HTTP logger

Install

pnpm i @tinyhttp/logger

API

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 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 for a full list.
  • ip: log IP address.

Example

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

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

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 - high-speed HTTP logger for Node.js
  • chrona - Simple HTTP request logger middleware for express.js inspired from koa-logger, written in typescript.