@tinyhttp/logger
Advanced tools
| import { ServerResponse as Response, IncomingMessage as Request } from 'http'; | ||
| export interface LoggerOptions { | ||
| methods?: string[]; | ||
| output?: { | ||
| color: boolean; | ||
| callback: (string: string) => void; | ||
| }; | ||
| timestamp?: boolean | { | ||
| format?: string; | ||
| }; | ||
| emoji?: boolean; | ||
| ip?: boolean; | ||
| } | ||
| export declare const logger: (options?: LoggerOptions) => (req: Request, res: Response, next?: () => void) => void; |
| import { magenta, bold, red, cyan } from 'colorette'; | ||
| import statusEmoji from 'http-status-emojis'; | ||
| import dayjs from 'dayjs'; | ||
| import { METHODS } from 'http'; | ||
| const compileArgs = (args, req, res, options = {}, status, msg) => { | ||
| var _a, _b; | ||
| const { method } = req; | ||
| const { statusCode } = res; | ||
| const url = req.originalUrl || req.url; | ||
| const methods = (_a = options.methods) !== null && _a !== void 0 ? _a : METHODS; | ||
| const timestamp = (_b = options.timestamp) !== null && _b !== void 0 ? _b : false; | ||
| const emojiEnabled = options.emoji; | ||
| if (methods.includes(method) && timestamp) { | ||
| args.push(`${dayjs() | ||
| .format(typeof timestamp !== 'boolean' && timestamp.format ? timestamp.format : 'HH:mm:ss') | ||
| .toString()} - `); | ||
| } | ||
| if (options.ip) | ||
| args.push(req.ip); | ||
| if (emojiEnabled) | ||
| args.push(statusEmoji[statusCode]); | ||
| args.push(method); | ||
| args.push(status || res.statusCode); | ||
| args.push(msg || res.statusMessage); | ||
| args.push(url); | ||
| }; | ||
| const logger = (options = {}) => { | ||
| var _a, _b; | ||
| const methods = (_a = options.methods) !== null && _a !== void 0 ? _a : METHODS; | ||
| const output = (_b = options.output) !== null && _b !== void 0 ? _b : { callback: console.log, color: true }; | ||
| return (req, res, next) => { | ||
| res.on('finish', () => { | ||
| const args = []; | ||
| if (methods.includes(req.method)) { | ||
| const s = res.statusCode.toString(); | ||
| if (!output.color) { | ||
| compileArgs(args, req, res, options); | ||
| const m = args.join(' '); | ||
| output.callback(m); | ||
| } | ||
| else { | ||
| switch (s[0]) { | ||
| case '2': | ||
| compileArgs(args, req, res, options, cyan(bold(s)), cyan(res.statusMessage)); | ||
| output.callback(args.join(' ')); | ||
| break; | ||
| case '4': | ||
| compileArgs(args, req, res, options, red(bold(s)), red(res.statusMessage)); | ||
| output.callback(args.join(' ')); | ||
| break; | ||
| case '5': | ||
| compileArgs(args, req, res, options, magenta(bold(s)), magenta(res.statusMessage)); | ||
| output.callback(args.join(' ')); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| next === null || next === void 0 ? void 0 : next(); | ||
| }; | ||
| }; | ||
| export { logger }; |
+15
-15
| { | ||
| "name": "@tinyhttp/logger", | ||
| "version": "1.3.3", | ||
| "version": "1.3.4", | ||
| "type": "module", | ||
@@ -29,3 +29,3 @@ "description": "Minimal and flexible HTTP logger.", | ||
| "dependencies": { | ||
| "colorette": "^2.0.19", | ||
| "colorette": "^2.0.20", | ||
| "dayjs": "^1.11.7", | ||
@@ -35,20 +35,20 @@ "http-status-emojis": "^2.2.0" | ||
| "devDependencies": { | ||
| "@commitlint/cli": "17.4.4", | ||
| "@commitlint/config-conventional": "17.4.4", | ||
| "@rollup/plugin-typescript": "^11.0.0", | ||
| "@tinyhttp/app": "2.0.29", | ||
| "@types/node": "^18.14.6", | ||
| "@typescript-eslint/eslint-plugin": "^5.54.0", | ||
| "@typescript-eslint/parser": "^5.54.0", | ||
| "@commitlint/cli": "17.6.3", | ||
| "@commitlint/config-conventional": "17.6.3", | ||
| "@rollup/plugin-typescript": "^11.1.1", | ||
| "@tinyhttp/app": "2.0.32", | ||
| "@types/node": "^20.1.4", | ||
| "@typescript-eslint/eslint-plugin": "^5.59.5", | ||
| "@typescript-eslint/parser": "^5.59.5", | ||
| "c8": "^7.13.0", | ||
| "eslint": "^8.35.0", | ||
| "eslint-config-prettier": "^8.6.0", | ||
| "eslint": "^8.40.0", | ||
| "eslint-config-prettier": "^8.8.0", | ||
| "eslint-plugin-prettier": "^4.2.1", | ||
| "expect": "^29.4.3", | ||
| "expect": "^29.5.0", | ||
| "husky": "^8.0.3", | ||
| "prettier": "^2.8.4", | ||
| "rollup": "^3.18.0", | ||
| "prettier": "^2.8.8", | ||
| "rollup": "^3.21.7", | ||
| "supertest-fetch": "^1.5.0", | ||
| "tsm": "^2.3.0", | ||
| "typescript": "^4.9.5", | ||
| "typescript": "^5.0.4", | ||
| "uvu": "^0.5.6" | ||
@@ -55,0 +55,0 @@ }, |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
8336
57.25%5
66.67%76
Infinity%3
200%Updated