Skip to content
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

refactor, remove winston, implement file-rotation instead #9584

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions components/legacy/logger/logger.spec.ts

This file was deleted.

33 changes: 3 additions & 30 deletions components/legacy/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/**
* leave the Winston for now to get the file-rotation we're missing from Pino and the "profile"
* functionality.
* also, Winston should start BEFORE Pino. otherwise, Pino starts creating the debug.log file first
* and it throws an error if the file doesn't exists on Docker/CI.
*/
import chalk from 'chalk';
import fs from 'fs-extra';
import path from 'path';
Expand All @@ -16,11 +10,11 @@ import pMapSeries from 'p-map-series';
import { Analytics } from '@teambit/legacy.analytics';
import { getConfig } from '@teambit/config-store';
import { defaultErrorHandler } from '@teambit/cli';
import { CFG_LOG_JSON_FORMAT, CFG_LOG_LEVEL, CFG_NO_WARNINGS } from '@teambit/legacy.constants';
import { getWinstonLogger } from './winston-logger';
import { CFG_LOG_JSON_FORMAT, CFG_LOG_LEVEL, CFG_NO_WARNINGS, DEBUG_LOG } from '@teambit/legacy.constants';
import { getPinoLogger } from './pino-logger';
import { Profiler } from './profiler';
import { loader } from '@teambit/legacy.loader';
import { rotateLogIfNeeded } from './rotate-log-file';

export { Level as LoggerLevel };

Expand All @@ -37,8 +31,7 @@ const DEFAULT_LEVEL = 'debug';

const logLevel = getLogLevel();

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { winstonLogger, createExtensionLogger } = getWinstonLogger(logLevel, jsonFormat);
rotateLogIfNeeded(DEBUG_LOG);

const { pinoLogger, pinoLoggerConsole, pinoSSELogger } = getPinoLogger(logLevel, jsonFormat);

Expand Down Expand Up @@ -370,26 +363,6 @@ export function writeLogToScreen(levelOrPrefix = '') {
if (levelOrPrefix === 'profile') {
logger.shouldConsoleProfiler = true;
}
// @todo: implement
// const prefixes = levelOrPrefix.split(',');
// const filterPrefix = winston.format((info) => {
// if (isLevel) return info;
// if (prefixes.some((prefix) => info.message.startsWith(prefix))) return info;
// return false;
// });
// logger.logger.add(
// new winston.transports.Console({
// level: isLevel ? levelOrPrefix : 'info',
// format: winston.format.combine(
// filterPrefix(),
// winston.format.metadata(),
// winston.format.errors({ stack: true }),
// winston.format.printf((info) => `${info.message} ${getMetadata(info)}`)
// ),
// })
// );
}

export { createExtensionLogger };

export default logger;
60 changes: 60 additions & 0 deletions components/legacy/logger/rotate-log-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as path from 'path';
import * as fs from 'fs-extra';

/**
* Rotates a log file if it exceeds a certain size (e.g. 10MB).
*
* - If "debug.log" is bigger than `maxSize`,
* then move older logs forward (debug9.log → debug10.log, etc.)
* rename debug.log → debug1.log,
* and finally create an empty debug.log.
*
* @param logPath Path to the main log file, e.g. "debug.log"
* @param maxSize Maximum size in bytes before rotation
* @param maxFiles Maximum number of rotated files to keep
*/
export function rotateLogIfNeeded(
logPath: string,
maxSize: number = 10 * 1024 * 1024, // 10 MB
maxFiles: number = 9
): void {
// Check if log file exists; if not, nothing to rotate
let stat;
try {
stat = fs.statSync(logPath);
} catch (err) {
// If file doesn't exist, create it and exit.
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
fs.ensureFileSync(logPath);
return;
}
// Else re-throw
throw err;
}

// If file size is below maxSize, no rotation needed
if (stat.size < maxSize) {
return;
}

// Otherwise, rotate the logs:
const dir = path.dirname(logPath);
const ext = path.extname(logPath);
const base = path.basename(logPath, ext);

// Shift older logs forward in ascending order
// i.e. debug9.log → debug10.log, debug8.log → debug9.log, ...
for (let i = maxFiles - 1; i > 0; i--) {
const oldFile = path.join(dir, `${base}${i}${ext}`);
const newFile = path.join(dir, `${base}${i + 1}${ext}`);
if (fs.pathExistsSync(oldFile)) {
fs.renameSync(oldFile, newFile);
}
}

// Move the current log to debug1.log
fs.renameSync(logPath, path.join(dir, `${base}1${ext}`));

// Create a fresh, empty debug.log
fs.ensureFileSync(logPath);
}
78 changes: 0 additions & 78 deletions components/legacy/logger/winston-logger.ts

This file was deleted.

1 change: 0 additions & 1 deletion workspace.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,6 @@
"webpack-dev-server": "4.15.2",
"webpack-manifest-plugin": "5.0.0",
"webpack-merge": "5.8.0",
"winston": "3.3.3",
"workbox-webpack-plugin": "7.1.0",
"write-file-atomic": "5.0.0",
"ws": "7.5.10",
Expand Down