Activity Logging System Documentation
Overview
The activity logging system is designed to track and record various events across the EDURange platform. It provides comprehensive logging for user actions, challenge interactions, group management, and system events.
Database Schema
ActivityLog Model
model ActivityLog {
id String @id @default(cuid())
eventType ActivityEventType
severity LogSeverity @default(INFO)
userId String
challengeId String?
groupId String?
challengeInstanceId String?
accessCodeId String?
metadata Json
timestamp DateTime @default(now())
// Relations
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
group CompetitionGroup? @relation(fields: [groupId], references: [id], onDelete: SetNull)
challenge Challenges? @relation(fields: [challengeId], references: [id], onDelete: SetNull)
challengeInstance ChallengeInstance? @relation(fields: [challengeInstanceId], references: [id], onDelete: SetNull)
accessCode CompetitionAccessCode? @relation(fields: [accessCodeId], references: [id], onDelete: SetNull)
}
Event Types
All possible event types are defined in the ActivityEventType
enum:
User Events
USER_REGISTERED
- New user registrationUSER_LOGGED_IN
- User loginUSER_ROLE_CHANGED
- User role modificationUSER_UPDATED
- User profile updatesUSER_DELETED
- User account deletion
Challenge Events
CHALLENGE_STARTED
- User starts a challengeCHALLENGE_COMPLETED
- User completes a challengeCHALLENGE_INSTANCE_CREATED
- New challenge instance creationCHALLENGE_INSTANCE_DELETED
- Challenge instance termination
Group Events
GROUP_CREATED
- New competition group creationGROUP_JOINED
- User joins a groupGROUP_LEFT
- User leaves a groupGROUP_UPDATED
- Group details modificationGROUP_DELETED
- Group deletionGROUP_MEMBER_REMOVED
- Member removal from group
Access Code Events
ACCESS_CODE_GENERATED
- New access code generationACCESS_CODE_USED
- Access code usage for joiningACCESS_CODE_EXPIRED
- Access code expirationACCESS_CODE_DELETED
- Access code manual deletion
Question Events
QUESTION_ATTEMPTED
- Question answer attemptQUESTION_COMPLETED
- Question completion
System Events
SYSTEM_ERROR
- System-level errors
Severity Levels
enum LogSeverity {
INFO // Normal operations
WARNING // Potential issues
ERROR // Operation failures
CRITICAL // System-critical issues
}
Implementation Details
ActivityLogger Service
The ActivityLogger
class provides centralized logging functionality with specialized methods for different event types:
- Core Logging Method:
static async logActivity({
eventType,
userId,
severity = 'INFO',
challengeId,
groupId,
challengeInstanceId,
accessCodeId,
metadata
})
- Specialized Logging Methods:
logChallengeEvent()
- Challenge-related eventslogGroupEvent()
- Group management eventslogAccessCodeEvent()
- Access code lifecycle eventslogUserEvent()
- User-related eventslogQuestionEvent()
- Question attempts and completionslogSystemError()
- System errors
Metadata Structure
Each event type includes specific metadata:
- Challenge Events:
{
challengeName: string,
challengeType: string,
startTime: string,
completionTime?: string,
pointsEarned?: number
}
- Group Events:
{
groupName: string,
action: string,
timestamp: string,
affectedUsers?: string[]
}
- Question Events:
{
questionId: string,
answer: string,
isCorrect: boolean,
attemptNumber: number,
pointsEarned?: number
}
Usage Examples
Logging Challenge Start
await ActivityLogger.logChallengeEvent(
'CHALLENGE_STARTED',
userId,
challengeId,
instanceId,
{
startTime: new Date().toISOString(),
challengeName: challenge.name
}
);
Logging Group Creation
await ActivityLogger.logGroupEvent(
'GROUP_CREATED',
userId,
groupId,
{
groupName: group.name,
createdAt: new Date().toISOString()
}
);
Logging Question Attempts
await ActivityLogger.logQuestionAttempt(
userId,
challengeId,
groupId,
{
questionId,
answer,
isCorrect,
attemptNumber
}
);
Integration Points
-
User Management:
- Registration
- Login
- Profile updates
- Role changes
-
Challenge System:
- Instance creation/deletion
- Challenge starts/completions
- Question attempts/completions
-
Competition Groups:
- Group creation/deletion
- Member management
- Access code operations
-
WebOS Integration:
- Question attempts logging
- Challenge interaction tracking
Viewing Logs
Logs can be viewed through:
- Admin Dashboard (/dashboard/logs)
- Filtered by:
- Event type
- User
- Time frame
- Severity level
Best Practices
- Always include relevant IDs (userId, challengeId, etc.)
- Use appropriate severity levels
- Include detailed metadata for context
- Handle logging errors gracefully
- Use consistent timestamp formats (ISO 8601)