Minimal and flexible HTTP logger
pnpm i @tinyhttp/logger
import { logger } from '@tinyhttp/logger'
Returns the middleware for logging HTTP requests.
methods
: a list of HTTP methods to log. Defaults tohttp
'sMETHODS
.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.
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)