fix[Logger]: update ESLint configuration (#8269)

* chore: update ESLint configuration and LoggerService formatting

- Refactored ESLint configuration to include custom rules for LoggerService, applying them specifically to the src directory while ignoring test and mock files.
- Commented out date formatting options in LoggerService for improved clarity in development logging.

* fix: add eslint-disable comments for restricted syntax in LoggerService

- Added eslint-disable comments to suppress warnings for restricted syntax in LoggerService for both main and renderer services.
- Improved error handling by logging messages when the window source is not initialized in the renderer LoggerService.
This commit is contained in:
fullex 2025-07-18 14:22:55 +08:00 committed by GitHub
parent 1a1c7bb604
commit f9c5ca258a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 26 deletions

View File

@ -30,28 +30,41 @@ export default defineConfig([
} }
}, },
// Configuration for ensuring compatibility with the original ESLint(8.x) rules // Configuration for ensuring compatibility with the original ESLint(8.x) rules
...[ {
{ rules: {
rules: { '@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-require-imports': 'off', '@typescript-eslint/no-unused-vars': ['error', { caughtErrors: 'none' }],
'@typescript-eslint/no-unused-vars': ['error', { caughtErrors: 'none' }], '@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/no-unused-expressions': 'off', '@typescript-eslint/no-empty-object-type': 'off',
'@typescript-eslint/no-empty-object-type': 'off', '@eslint-react/hooks-extra/no-direct-set-state-in-use-effect': 'off',
'@eslint-react/hooks-extra/no-direct-set-state-in-use-effect': 'off', '@eslint-react/web-api/no-leaked-event-listener': 'off',
'@eslint-react/web-api/no-leaked-event-listener': 'off', '@eslint-react/web-api/no-leaked-timeout': 'off',
'@eslint-react/web-api/no-leaked-timeout': 'off', '@eslint-react/no-unknown-property': 'off',
'@eslint-react/no-unknown-property': 'off', '@eslint-react/no-nested-component-definitions': 'off',
'@eslint-react/no-nested-component-definitions': 'off', '@eslint-react/dom/no-dangerously-set-innerhtml': 'off',
'@eslint-react/dom/no-dangerously-set-innerhtml': 'off', '@eslint-react/no-array-index-key': 'off',
'@eslint-react/no-array-index-key': 'off', '@eslint-react/no-unstable-default-props': 'off',
'@eslint-react/no-unstable-default-props': 'off', '@eslint-react/no-unstable-context-value': 'off',
'@eslint-react/no-unstable-context-value': 'off', '@eslint-react/hooks-extra/prefer-use-state-lazy-initialization': 'off',
'@eslint-react/hooks-extra/prefer-use-state-lazy-initialization': 'off', '@eslint-react/hooks-extra/no-unnecessary-use-prefix': 'off',
'@eslint-react/hooks-extra/no-unnecessary-use-prefix': 'off', '@eslint-react/no-children-to-array': 'off'
'@eslint-react/no-children-to-array': 'off'
}
} }
], },
{
// LoggerService Custom Rules - only apply to src directory
files: ['src/**/*.{ts,tsx,js,jsx}'],
ignores: ['src/**/__tests__/**', 'src/**/__mocks__/**', 'src/**/*.test.*'],
rules: {
'no-restricted-syntax': [
'warn',
{
selector: 'CallExpression[callee.object.name="console"]',
message:
'❗CherryStudio uses unified LoggerService: 📖 docs/technical/how-to-use-logger-en.md\n❗CherryStudio 使用统一的日志服务:📖 docs/technical/how-to-use-logger-zh.md\n\n'
}
]
}
},
{ {
ignores: [ ignores: [
'node_modules/**', 'node_modules/**',

View File

@ -95,6 +95,7 @@ export class LoggerService {
// Handle transport events // Handle transport events
this.logger.on('error', (error) => { this.logger.on('error', (error) => {
// eslint-disable-next-line no-restricted-syntax
console.error('LoggerService fatal error:', error) console.error('LoggerService fatal error:', error)
}) })
@ -128,9 +129,9 @@ export class LoggerService {
if (isDev) { if (isDev) {
const datetimeColored = colorText( const datetimeColored = colorText(
new Date().toLocaleString('zh-CN', { new Date().toLocaleString('zh-CN', {
year: 'numeric', // year: 'numeric',
month: '2-digit', // month: '2-digit',
day: '2-digit', // day: '2-digit',
hour: '2-digit', hour: '2-digit',
minute: '2-digit', minute: '2-digit',
second: '2-digit', second: '2-digit',
@ -140,8 +141,6 @@ export class LoggerService {
'CYAN' 'CYAN'
) )
console.log('processLog', source.process, this.module, this.context)
let moduleString = '' let moduleString = ''
if (source.process === 'main') { if (source.process === 'main') {
moduleString = this.module ? ` [${colorText(this.module, 'UNDERLINE')}] ` : ' ' moduleString = this.module ? ` [${colorText(this.module, 'UNDERLINE')}] ` : ' '
@ -152,33 +151,39 @@ export class LoggerService {
switch (level) { switch (level) {
case 'error': case 'error':
// eslint-disable-next-line no-restricted-syntax
console.error( console.error(
`${datetimeColored} ${colorText(colorText('<ERROR>', 'RED'), 'BOLD')}${moduleString}${message}`, `${datetimeColored} ${colorText(colorText('<ERROR>', 'RED'), 'BOLD')}${moduleString}${message}`,
...meta ...meta
) )
break break
case 'warn': case 'warn':
// eslint-disable-next-line no-restricted-syntax
console.warn( console.warn(
`${datetimeColored} ${colorText(colorText('<WARN>', 'YELLOW'), 'BOLD')}${moduleString}${message}`, `${datetimeColored} ${colorText(colorText('<WARN>', 'YELLOW'), 'BOLD')}${moduleString}${message}`,
...meta ...meta
) )
break break
case 'info': case 'info':
// eslint-disable-next-line no-restricted-syntax
console.info( console.info(
`${datetimeColored} ${colorText(colorText('<INFO>', 'GREEN'), 'BOLD')}${moduleString}${message}`, `${datetimeColored} ${colorText(colorText('<INFO>', 'GREEN'), 'BOLD')}${moduleString}${message}`,
...meta ...meta
) )
break break
case 'debug': case 'debug':
// eslint-disable-next-line no-restricted-syntax
console.debug( console.debug(
`${datetimeColored} ${colorText(colorText('<DEBUG>', 'BLUE'), 'BOLD')}${moduleString}${message}`, `${datetimeColored} ${colorText(colorText('<DEBUG>', 'BLUE'), 'BOLD')}${moduleString}${message}`,
...meta ...meta
) )
break break
case 'verbose': case 'verbose':
// eslint-disable-next-line no-restricted-syntax
console.log(`${datetimeColored} ${colorText('<VERBOSE>', 'BOLD')}${moduleString}${message}`, ...meta) console.log(`${datetimeColored} ${colorText('<VERBOSE>', 'BOLD')}${moduleString}${message}`, ...meta)
break break
case 'silly': case 'silly':
// eslint-disable-next-line no-restricted-syntax
console.log(`${datetimeColored} ${colorText('<SILLY>', 'BOLD')}${moduleString}${message}`, ...meta) console.log(`${datetimeColored} ${colorText('<SILLY>', 'BOLD')}${moduleString}${message}`, ...meta)
break break
} }

View File

@ -69,6 +69,7 @@ export class LoggerService {
private processLog(level: LogLevel, message: string, data: any[]): void { private processLog(level: LogLevel, message: string, data: any[]): void {
if (!this.window) { if (!this.window) {
// eslint-disable-next-line no-restricted-syntax
console.error('LoggerService: window source not initialized, please initialize window source first') console.error('LoggerService: window source not initialized, please initialize window source first')
return return
} }
@ -83,21 +84,27 @@ export class LoggerService {
switch (level) { switch (level) {
case 'error': case 'error':
// eslint-disable-next-line no-restricted-syntax
console.error(logMessage, ...data) console.error(logMessage, ...data)
break break
case 'warn': case 'warn':
// eslint-disable-next-line no-restricted-syntax
console.warn(logMessage, ...data) console.warn(logMessage, ...data)
break break
case 'info': case 'info':
// eslint-disable-next-line no-restricted-syntax
console.info(logMessage, ...data) console.info(logMessage, ...data)
break break
case 'verbose': case 'verbose':
// eslint-disable-next-line no-restricted-syntax
console.log(logMessage, ...data) console.log(logMessage, ...data)
break break
case 'debug': case 'debug':
// eslint-disable-next-line no-restricted-syntax
console.debug(logMessage, ...data) console.debug(logMessage, ...data)
break break
case 'silly': case 'silly':
// eslint-disable-next-line no-restricted-syntax
console.log(logMessage, ...data) console.log(logMessage, ...data)
break break
} }