@avanlan/logger
v0.0.4
Published
A logger for Node.js
Downloads
10
Maintainers
Readme
Logger
A logger for Node.js.
Install
pnpm i @avanlan/logger
Usage
import { Logger } from '@avanlan/logger';
const logger = new Logger();
logger.access.info("access log")
logger.daily.info("daily log")
logger.error.error("error log")
logger.debug.info("debug log")
// output
// [2024-08-08 16:37:24] [main-app] [INFO]: access log
// [2024-08-08 16:37:24] [main-app] [INFO]: daily log
// [2024-08-08 16:37:24] [main-app] [ERROR]: error log
// [2024-08-08 16:37:24] [main-app] [INFO]: debug log
Configuration
Project
Specify the project name.
import { Logger } from '@avanlan/logger';
const logger = new Logger({
projectName: "auth-service",
});
logger.daily.info("info log")
// output daily log
// [2024-08-08 16:37:24] [auth-service] [INFO]: info log
Output Logger File
project
├── logger
│ ├── access
│ │ └── access.YYYY-MM-DD.log
│ ├── daily
│ │ └── daily.YYYY-MM-DD.log
│ ├── error
│ │ └── error.YYYY-MM-DD.log
│ └── debug.log
Logger Middleware
Koa
import { Logger, koaHttpLogger } from "@avanlan/logger";
import { bodyParser } from '@koa/bodyparser';
const logger = new Logger();
app.use(bodyparser());
app.use(koaHttpLogger(logger));
// output access log
// [2024-08-08 17:34:20] [main-app] [INFO]: 2ms GET / ::1 {"headers":{"host":"localhost:8044","user-agent":"curl/8.6.0","accept":"*/*"},"query":{},"body":{}}
Express
import { Logger, expressHttpLogger } from "@avanlan/logger";
import bodyParser from 'body-parser';
const logger = new Logger();
app.use(bodyParser.json());
app.use(expressHttpLogger(logger));
// output access log
// [2024-08-08 17:47:55] [main-app] [INFO]: 0ms GET / ::1 {"headers":{"host":"localhost:5834","user-agent":"curl/8.6.0","accept":"*/*"},"query":{}}