Flexible HTTP logging middleware for otterhttp
npm install @otterhttp/logger[![npm][npm-img]][npm-url]
[![GitHub Workflow Status][gh-actions-img]][github-actions]
[![Coverage][cov-img]][cov-url]
Minimal and flexible HTTP logger
``sh`
pnpm i @otterhttp/logger
`ts`
import { logger } from '@otterhttp/logger'
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.
`ts
import { App } from '@otterhttp/app'
import { logger } from '@otterhttp/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 '@otterhttp/app'
import { logger, LogLevel } from '@otterhttp/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 '@otterhttp/app'
import { logger } from '@otterhttp/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)
``
- 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
[npm-url]: https://npmjs.com/package/@otterhttp/logger
[npm-img]: https://img.shields.io/npm/dt/@otterhttp/logger?style=for-the-badge&color=blueviolet
[github-actions]: https://github.com/OtterJS/otterhttp-logger/actions
[gh-actions-img]: https://img.shields.io/github/actions/workflow/status/OtterJS/otterhttp-logger/main.yml?branch=main&style=for-the-badge&color=blueviolet&label=&logo=github
[cov-url]: https://coveralls.io/github/OtterJS/otterhttp-logger
[cov-img]: https://img.shields.io/coveralls/github/OtterJS/otterhttp-logger?style=for-the-badge&color=blueviolet