-
Notifications
You must be signed in to change notification settings - Fork 3.6k
chore: Add logger package for node server side apps #6188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aea213b
chore: Add logger as a package
Saurabhkmr98 821a994
chore: Add logger package for node server side apps
Saurabhkmr98 e503599
remove plane logger import in web
Saurabhkmr98 df0fc65
resolve pr reviews and add client logger with readme update
Saurabhkmr98 62d1a51
Merge branch 'preview' of github.com:makeplane/plane into chore-logge…
sriramveeraghanta c9d0a02
fix: transformation and added middleware for logging requests
sriramveeraghanta df66e92
chore: update readme
Saurabhkmr98 9047adf
fix: env configurable max file size
sriramveeraghanta 739e15f
Merge branch 'chore-logger-package' of github.com:makeplane/plane int…
sriramveeraghanta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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, | ||
| }); | ||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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); | ||
| } | ||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Capture response | ||
| res.on("finish", () => { | ||
| const duration = Date.now() - startTime; | ||
| logger.http(`Completed ${req.method} ${req.url} with status ${res.statusCode} in ${duration}ms`); | ||
| }); | ||
|
|
||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| next(); | ||
| }; | ||
sriramveeraghanta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.