feat: add TypeORM, logger, schemas, hooks and stores
- Add TypeORM integration with data-source, entities and repositories - Add logger service for structured logging - Add Zod validation schemas for auth, cleaner and extractor - Add custom React hooks (useAuth, useCleaner, useExtractor, useValidation) - Add Zustand stores (useAppStore, useUserStore) - Add UI components (Button, Modal, Toast) - Add error types and ErpBrowserManager - Refactor IPC handlers and services - Add unit tests for new modules Co-Authored-By: Claude (glm-5) <noreply@anthropic.com>
This commit is contained in:
97
src/main/services/logger/index.ts
Normal file
97
src/main/services/logger/index.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Unified logging system using Winston
|
||||
* Console + File transports with daily rotation
|
||||
*/
|
||||
|
||||
import winston from 'winston'
|
||||
import DailyRotateFile from 'winston-daily-rotate-file'
|
||||
import path from 'path'
|
||||
import { app } from 'electron'
|
||||
import fs from 'fs'
|
||||
|
||||
// Get log directory - use app.getPath('logs') in production, or local logs dir in development
|
||||
function getLogDir(): string {
|
||||
if (app && app.isReady()) {
|
||||
return app.getPath('logs')
|
||||
}
|
||||
// Fallback for development or before app is ready
|
||||
const devLogDir = path.join(process.cwd(), 'logs')
|
||||
if (!fs.existsSync(devLogDir)) {
|
||||
fs.mkdirSync(devLogDir, { recursive: true })
|
||||
}
|
||||
return devLogDir
|
||||
}
|
||||
|
||||
// Custom format for console output
|
||||
const consoleFormat = winston.format.combine(
|
||||
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
||||
winston.format.colorize(),
|
||||
winston.format.printf(({ timestamp, level, message, context, ...meta }) => {
|
||||
const contextStr = context ? `[${context}]` : ''
|
||||
const metaStr = Object.keys(meta).length > 0 ? ` ${JSON.stringify(meta)}` : ''
|
||||
return `${timestamp} [${level}]${contextStr} ${message}${metaStr}`
|
||||
})
|
||||
)
|
||||
|
||||
// Custom format for file output
|
||||
const fileFormat = winston.format.combine(
|
||||
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
||||
winston.format.json()
|
||||
)
|
||||
|
||||
// Daily rotate file transport configuration
|
||||
const createFileTransport = (level?: string): DailyRotateFile => {
|
||||
return new DailyRotateFile({
|
||||
filename: path.join(getLogDir(), 'app-%DATE%.log'),
|
||||
datePattern: 'YYYY-MM-DD',
|
||||
zippedArchive: true,
|
||||
maxSize: '20m',
|
||||
maxFiles: '14d',
|
||||
level,
|
||||
format: fileFormat
|
||||
})
|
||||
}
|
||||
|
||||
// Create the logger instance
|
||||
const logger = winston.createLogger({
|
||||
level: process.env.LOG_LEVEL || 'info',
|
||||
defaultMeta: { service: 'erpauto' },
|
||||
transports: [
|
||||
// Console transport - always enabled
|
||||
new winston.transports.Console({
|
||||
format: consoleFormat
|
||||
}),
|
||||
// File transport for all levels
|
||||
createFileTransport()
|
||||
]
|
||||
})
|
||||
|
||||
// Add error-specific file transport in production
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
logger.add(
|
||||
new DailyRotateFile({
|
||||
filename: path.join(getLogDir(), 'error-%DATE%.log'),
|
||||
datePattern: 'YYYY-MM-DD',
|
||||
zippedArchive: true,
|
||||
maxSize: '20m',
|
||||
maxFiles: '14d',
|
||||
level: 'error',
|
||||
format: fileFormat
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a child logger with a specific context
|
||||
* @param context - The context/module name for the logger
|
||||
* @returns A child logger instance
|
||||
*/
|
||||
export function createLogger(context: string): winston.Logger {
|
||||
return logger.child({ context })
|
||||
}
|
||||
|
||||
// Export the main logger for direct use
|
||||
export default logger
|
||||
|
||||
// Export log level types for convenience
|
||||
export type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'verbose'
|
||||
Reference in New Issue
Block a user