@avanlan/logger
v0.1.5
Published
A logger for Node.js
Maintainers
Readme
Logger
A logger for Node.js.
Install
pnpm i @avanlan/loggerUsage
import { Logger } from '@avanlan/logger';
const logger = new Logger({
projectName: "demo-app",
timezone: "America/New_York",
clean: {
type: CleanType.NODE, // default winston
maxFiles: 14, // default 14
maxSize: 1024 * 1024 * 100, // default 100m
},
transportsFile: {
maxsize: 1024 * 1024 * 400,
},
dailyRotateFile: {
maxFiles: 14,
},
});
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-26 23:05:18] [demo-app] [INFO]: access log
[2024-12-26 23:05:18] [demo-app] [INFO]: daily log
[2024-12-26 23:05:18] [demo-app] [ERROR]: error log Error Error
at Object.<anonymous> (/Users/avan/Code/personal/logger/demo.ts:21: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-26 23:05:18] [demo-app] [INFO]: debug log {"a":1}
[2024-12-26 23:05:18] [demo-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 logTimezone
The default timezone is Asia/Shanghai.
const logger = new Logger({
timezone: "America/New_York",
});Clean
interface CleanOptions {
type?: CleanType;
maxFiles?: number | string;
maxSize?: number;
}type: Clean type, default is CleanType.WINSTON.
maxFiles: Maximum number of logs to keep. If not set, no logs will be removed. This can be a number of files or number of days. If using days, add 'd' as the suffix. Work with CleanType.NODE. default is 14.
maxSize: Maximum size of the file after which it will rotate. unit is byte. Work with CleanType.NODE. default is 100 * 1024 * 1024(100m).
const logger = new Logger({
clean: {
type: CleanType.NODE,
maxFiles: 14,
maxSize: 1024 * 1024 * 100,
},
});Daily Rotate File
Work with CleanType.WINSTON.
interface DailyRotateFileConfig {
maxSize?: stringOrNumber;
maxFiles?: stringOrNumber;
}maxSize: Maximum size of the file after which it will rotate. This can be a number of bytes, or units of kb, mb, and gb. If using the units, add 'k', 'm', or 'g' as the suffix. The units need to directly follow the number.
maxFiles: Maximum number of logs to keep. If not set, no logs will be removed. This can be a number of files or number of days. If using days, add 'd' as the suffix.
const logger = new Logger({
dailyRotateFile: {
maxSize: 1024 * 1024 * 400, // '400m'
maxFiles: 14, // '14d'
},
});Transports File
Work with CleanType.WINSTON.
interface TransportsFileConfig {
maxsize?: number;
maxFiles?: number;
}const logger = new Logger({
transportsFile: {
maxsize: 1024 * 1024 * 400,
maxFiles: 14,
},
});Output Logger File
project
├── logger
│ ├── access
│ │ └── access.YYYY-MM-DD.log
│ ├── daily
│ │ └── daily.YYYY-MM-DD.log
│ ├── error
│ │ └── error.YYYY-MM-DD.log
│ └── debug.logLogger 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: {}Demo
pnpm run demoFeatures
- [x] Access log
- [x] Daily log
- [x] Error log
- [x] Debug log
- [x] Koa middleware
- [x] Express middleware
- [x] Console support color
- [x] Timezone support
- [x] Logger clear by winston
- [x] Logger clear by node

