-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add logger package for node server side apps (#6188)
* chore: Add logger as a package * chore: Add logger package for node server side apps * remove plane logger import in web * resolve pr reviews and add client logger with readme update * fix: transformation and added middleware for logging requests * chore: update readme * fix: env configurable max file size --------- Co-authored-by: sriram veeraghanta <[email protected]>
- Loading branch information
1 parent
9234f21
commit a8140a5
Showing
10 changed files
with
1,974 additions
and
1,613 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build/* | ||
dist/* | ||
out/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** @type {import("eslint").Linter.Config} */ | ||
module.exports = { | ||
root: true, | ||
extends: ["@plane/eslint-config/library.js"], | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
project: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"printWidth": 120, | ||
"tabWidth": 2, | ||
"trailingComma": "es5" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Logger Package | ||
|
||
This package provides a logger and a request logger utility built using [Winston](https://github.com/winstonjs/winston). It offers customizable log levels using env and supports structured logging for general application logs and HTTP requests. | ||
|
||
## Features. | ||
- Dynamic log level configuration using env. | ||
- Pre-configured winston logger for general usage (`logger`). | ||
- Request logger middleware that logs incoming request | ||
|
||
## Usage | ||
|
||
### Adding as a package | ||
Add this package as a dependency in package.json | ||
```typescript | ||
dependency: { | ||
... | ||
@plane/logger":"*", | ||
... | ||
} | ||
``` | ||
|
||
### Importing the Logger | ||
```typescript | ||
import { logger, requestLogger } from '@plane/logger' | ||
``` | ||
### Usage | ||
### `logger`: General Logger | ||
Use this for general application logs. | ||
|
||
```typescript | ||
logger.info("This is an info log"); | ||
logger.warn("This is a warning"); | ||
logger.error("This is an error"); | ||
``` | ||
|
||
### `requestLogger`: Request Logger Middleware | ||
Use this as a middleware for incoming requests | ||
|
||
```typescript | ||
const app = express() | ||
app.use(requestLogger) | ||
``` | ||
|
||
## Available Log Levels | ||
- `error` | ||
- `warn` | ||
- `info` (default) | ||
- `http` | ||
- `verbose` | ||
- `debug` | ||
- `silly` | ||
|
||
## Log file | ||
- Log files are stored in logs folder of current working directory. Error logs are stored in files with format `error-%DATE%.log` and combined logs are stored with format `combined-%DATE%.log`. | ||
- Log files have a 7 day rotation period defined. | ||
|
||
## Configuration | ||
- By default, the log level is set to `info`. | ||
- You can specify a log level by adding a LOG_LEVEL in .env. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "@plane/logger", | ||
"version": "0.24.1", | ||
"description": "Logger shared across multiple apps internally", | ||
"private": true, | ||
"main": "./src/index.ts", | ||
"types": "./src/index.ts", | ||
"scripts": { | ||
"lint": "eslint src --ext .ts,.tsx", | ||
"lint:errors": "eslint src --ext .ts,.tsx --quiet" | ||
}, | ||
"dependencies": { | ||
"winston": "^3.17.0", | ||
"winston-daily-rotate-file": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@plane/eslint-config": "*", | ||
"@types/node": "^22.5.4", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import winston from "winston"; | ||
import DailyRotateFile from "winston-daily-rotate-file"; | ||
import path from "path"; | ||
|
||
// Define log levels | ||
const levels = { | ||
error: 0, | ||
warn: 1, | ||
info: 2, | ||
http: 3, | ||
debug: 4, | ||
}; | ||
|
||
// Define colors for each level | ||
const colors = { | ||
error: "red", | ||
warn: "yellow", | ||
info: "green", | ||
http: "magenta", | ||
debug: "white", | ||
}; | ||
|
||
// Tell winston about our colors | ||
winston.addColors(colors); | ||
|
||
// Custom format for logging | ||
const format = winston.format.combine( | ||
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss:ms" }), | ||
winston.format.colorize({ all: true }), | ||
winston.format.printf( | ||
(info: winston.Logform.TransformableInfo) => `[${info?.timestamp}] ${info.level}: ${info.message}` | ||
) | ||
); | ||
|
||
// Define which transports to use | ||
const transports = [ | ||
// Console transport | ||
new winston.transports.Console(), | ||
|
||
// Rotating file transport for errors | ||
new DailyRotateFile({ | ||
filename: path.join(process.cwd(), "logs", "error-%DATE%.log"), | ||
datePattern: "YYYY-MM-DD", | ||
zippedArchive: true, | ||
maxSize: process.env.LOG_MAX_SIZE || "20m", | ||
maxFiles: process.env.LOG_RETENTION || "7d", | ||
level: "error", | ||
}), | ||
|
||
// Rotating file transport for all logs | ||
new DailyRotateFile({ | ||
filename: path.join(process.cwd(), "logs", "combined-%DATE%.log"), | ||
datePattern: "YYYY-MM-DD", | ||
zippedArchive: true, | ||
maxSize: process.env.LOG_MAX_SIZE || "20m", | ||
maxFiles: process.env.LOG_RETENTION || "7d", | ||
}), | ||
]; | ||
|
||
// Create the logger | ||
export const logger = winston.createLogger({ | ||
level: process.env.LOG_LEVEL || "info", | ||
levels, | ||
format, | ||
transports, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./config"; | ||
export * from "./middleware"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { logger } from "./config"; | ||
|
||
export const requestLogger = (req: Request, res: Response, next: NextFunction) => { | ||
// Log when the request starts | ||
const startTime = Date.now(); | ||
|
||
// Log request details | ||
logger.http(`Incoming ${req.method} request to ${req.url} from ${req.ip}`); | ||
|
||
// Log request body if present | ||
if (Object.keys(req.body).length > 0) { | ||
logger.debug("Request body:", req.body); | ||
} | ||
|
||
// Capture response | ||
res.on("finish", () => { | ||
const duration = Date.now() - startTime; | ||
logger.http(`Completed ${req.method} ${req.url} with status ${res.statusCode} in ${duration}ms`); | ||
}); | ||
|
||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": "@plane/typescript-config/base.json", | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"target": "ESNext", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"outDir": "./dist", | ||
"rootDir": "./src", | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
}, | ||
"experimentalDecorators": true, | ||
"sourceMap": true | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules"] | ||
} |
Oops, something went wrong.