first commit
This commit is contained in:
41
backend/src/utils/audit.ts
Normal file
41
backend/src/utils/audit.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { prisma } from '../config/database';
|
||||
|
||||
interface AuditLogData {
|
||||
userId: string | null;
|
||||
action: string;
|
||||
resource: string;
|
||||
resourceId?: string;
|
||||
details?: any;
|
||||
ipAddress?: string | null;
|
||||
userAgent?: string | null;
|
||||
}
|
||||
|
||||
export const createAuditLog = async (data: AuditLogData) => {
|
||||
try {
|
||||
const auditData: any = {
|
||||
userId: data.userId,
|
||||
action: data.action,
|
||||
resource: data.resource
|
||||
};
|
||||
|
||||
// 只添加非undefined的字段
|
||||
if (data.resourceId !== undefined) {
|
||||
auditData.resourceId = data.resourceId;
|
||||
}
|
||||
if (data.details !== undefined) {
|
||||
auditData.details = data.details;
|
||||
}
|
||||
if (data.ipAddress !== undefined) {
|
||||
auditData.ipAddress = data.ipAddress;
|
||||
}
|
||||
if (data.userAgent !== undefined) {
|
||||
auditData.userAgent = data.userAgent;
|
||||
}
|
||||
|
||||
await prisma.auditLog.create({
|
||||
data: auditData
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to create audit log:', error);
|
||||
}
|
||||
};
|
||||
26
backend/src/utils/logger.ts
Normal file
26
backend/src/utils/logger.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import winston from 'winston';
|
||||
|
||||
const logFormat = winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
winston.format.errors({ stack: true }),
|
||||
winston.format.json()
|
||||
);
|
||||
|
||||
export const logger = winston.createLogger({
|
||||
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
|
||||
format: logFormat,
|
||||
defaultMeta: { service: 'pandora-backend' },
|
||||
transports: [
|
||||
new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
|
||||
new winston.transports.File({ filename: 'logs/combined.log' }),
|
||||
],
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
logger.add(new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
winston.format.simple()
|
||||
)
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user