@avanlan/logger
v0.1.0
Published
A logger for Node.js
Downloads
428
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", new Error());
logger.debug.info("debug log", { a: 1 });
logger.access.info({
time: '32m',
method: "GET",
url: "/",
ip: "127.0.0.1",
body: "hello",
headers: {
"content-type": "application/json",
},
query: {
a: 1,
},
});
output
[2024-12-13 12:13:19] [main-app] [INFO]: access log
[2024-12-13 12:13:19] [main-app] [INFO]: daily log
[2024-12-13 12:13:19] [main-app] [ERROR]: error log Error Error
at Object.<anonymous> (/Users/avan/Code/personal/logger/demo.ts:7:33)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Module.m._compile (/Users/avan/Code/personal/logger/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1618:23)
at node:internal/modules/cjs/loader:1689:10
at Object.require.extensions.<computed> [as .ts] (/Users/avan/Code/personal/logger/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1621:12)
at Module.load (node:internal/modules/cjs/loader:1318:32)
at Function._load (node:internal/modules/cjs/loader:1128:12)
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
[2024-12-13 12:13:19] [main-app] [INFO]: debug log {"a":1}
[2024-12-13 12:13:19] [main-app] [INFO]: 32m GET / 127.0.0.1 headers: {"content-type":"application/json"} query: {"a":1} body: "hello"
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: {} body: {}
Features
- [x] Access log
- [x] Daily log
- [x] Error log
- [x] Debug log
- [x] Koa middleware
- [x] Express middleware
- [x] Console support color