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

E2471: Reimplement logger #128

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
aa84e46
Added initial logging
lgjenqui Oct 16, 2024
5e9dde9
Merge pull request #1 from camhimes/lgjenqui/CSC517_P2
camhimes Oct 24, 2024
1e0ee7a
Added old expertiza_logger file for formatting, updated logging file …
Oct 24, 2024
01ccf9d
Added the LoggerMessage model, which is used by the new ExpertizaLogF…
Oct 24, 2024
ceee53b
Added the logging to AppRecord.
bjiang9 Oct 29, 2024
a31d1aa
Merge pull request #9 from camhimes/model-logging
camhimes Oct 29, 2024
2c0db51
Reimplemented misc logging statements from old Expertiza controllers
Oct 29, 2024
6d34b6a
Changed invitations_controller logging statement to log w/ inviting s…
Oct 29, 2024
6634e30
First attempt Rspec log tests
bjiang9 Oct 29, 2024
4fddae5
Added comments to describe Logger functionality
lgjenqui Oct 30, 2024
cc29ff9
Merge pull request #11 from camhimes/dev/lgj/comments
camhimes Oct 30, 2024
5e5fc14
Fixed Rspec describe to get logging user test working
lgjenqui Oct 30, 2024
0d2155a
Added auth back to the users controller, removed it by mistake earlie…
Oct 30, 2024
59a4e15
Non-working test for Cam to work on
lgjenqui Oct 30, 2024
d8be0ae
Removed errant test for warning logs
lgjenqui Oct 30, 2024
5e2e16f
Removed line to disable authorization
lgjenqui Oct 30, 2024
df54da0
Removed logging line used for manual testing
lgjenqui Oct 30, 2024
3afd763
Improved logging to use LoggerMessages, consistent user identificatio…
lgjenqui Nov 11, 2024
6a74294
Merge pull request #13 from camhimes/dev/lgj/improve_logging
camhimes Nov 19, 2024
db69acd
Added tests for ExpertizaLogger and ExpertizaLogFormatter
lgjenqui Nov 19, 2024
56d3882
Merge pull request #12 from camhimes/testing
camhimes Nov 25, 2024
187f08c
Added some new logging statements to the account requests controller
Nov 26, 2024
8c3dbaa
Added some logging to questions_controller
lgjenqui Nov 26, 2024
ae4e4b0
Added Log Messages to Course Controller.
bjiang9 Nov 30, 2024
2223389
Merge pull request #14 from camhimes/dev/lgj/questions_controller_log…
camhimes Nov 30, 2024
e8629b0
Removed extra space around LoggerMessage.new(...) params for consistency
Nov 30, 2024
70d7964
Merge pull request #15 from camhimes/dev/bjiang9/adding-log-msg
camhimes Nov 30, 2024
ed18dfe
Added more log messages for the account request controller
Nov 30, 2024
c6e47f5
Added logging statements to the assignments controller
Nov 30, 2024
4524b94
Added logging for the bookmarks controller
Nov 30, 2024
150d67a
Added logging for the institutions controller
Nov 30, 2024
49f932b
Added logging for the invitations controller
Nov 30, 2024
905d428
Added logging for the join team requests controller
Nov 30, 2024
448f633
Added logging for the questionnaires controller
Dec 1, 2024
ad43a48
Added logging for the questions controller
Dec 1, 2024
735f28b
Added logging for the roles controller
Dec 1, 2024
e7afdda
Added logging for the sign up topics controller
Dec 1, 2024
fe5988b
Added logging for the signed up teams controller
Dec 1, 2024
b015193
Added logging for the student tasks controller
Dec 1, 2024
bc50758
Added logging for the users controller
Dec 1, 2024
a6b6b95
Added more comments to expertiza_logger.rb to explain functionality
Dec 1, 2024
8cc2ab7
Merge pull request #21 from camhimes/dev/csh/adding_controller_logs
bjiang0089 Dec 2, 2024
c84e222
Removed logging concern from application_record and expanded comments
lgjenqui Dec 3, 2024
f2e0ede
Deleted logging concern
lgjenqui Dec 3, 2024
9b69851
Merge pull request #22 from camhimes/dev/lgj/remove_logging_concern_d…
camhimes Dec 3, 2024
3aa7874
Replaced session references in logging statements with the correct re…
Dec 3, 2024
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
26 changes: 25 additions & 1 deletion app/controllers/api/v1/account_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ class Api::V1::AccountRequestsController < ApplicationController
# GET /account_requests/pending
def pending_requests
@account_requests = AccountRequest.where(status: 'Under Review').order('created_at DESC')
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched #{@account_requests.count} pending account requests.", request)
render json: @account_requests, status: :ok
end

# GET /account_requests/processed
def processed_requests
@account_requests = AccountRequest.where.not(status: 'Under Review').order('updated_at DESC')
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched #{@account_requests.count} processed account requests.", request)
render json: @account_requests, status: :ok
end

Expand All @@ -18,25 +20,36 @@ def create
if @account_request.save
response = { account_request: @account_request }
if User.find_by(email: @account_request.email)
# Logging a warning if a user with the same email already exists
ExpertizaLogger.warn LoggerMessage.new(controller_name, @current_user.name, "Account requested with duplicate email: #{@account_request.email}", request)
response[:warnings] = 'WARNING: User with this email already exists!'
end

# Logging the successful creation of the account request
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Successfully created account request with ID: #{@account_request.id}.", request)
render json: response, status: :created
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, $ERROR_INFO, request)
render json: @account_request.errors, status: :unprocessable_entity
end
rescue ActiveRecord::RecordNotFound => e
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Record not found: #{e.message}", request)
render json: { error: e.message }, status: :not_found
rescue ActionController::ParameterMissing => e
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Parameter missing: #{e.message}", request)
render json: { error: e.message }, status: :parameter_missing
rescue StandardError => e
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "An error occurred: #{e.message}", request)
render json: { error: e.message }, status: :unprocessable_entity
end

# GET /account_requests/:id
def show
@account_request = AccountRequest.find(params[:id])
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched account request with ID: #{@account_request.id}.", request)
render json: @account_request, status: :ok
rescue ActiveRecord::RecordNotFound => e
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Account request not found: #{e.message}", request)
render json: { error: e.message }, status: :not_found
end

Expand All @@ -45,24 +58,31 @@ def show
def update
@account_request = AccountRequest.find(params[:id])
@account_request.update(account_request_params)

ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated account request with ID: #{@account_request.id}, Status: #{@account_request.status}.", request)

if @account_request.status == 'Approved'
create_approved_user
else
render json: @account_request, status: :ok
end
rescue ActiveRecord::RecordNotFound => e
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Account request not found for update: #{e.message}", request)
render json: { error: e.message }, status: :not_found
rescue StandardError => e
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "An error occurred while updating account request: #{e.message}", request)
render json: { error: e.message }, status: :unprocessable_entity
end

# DELETE /account_requests/:id
def destroy
@account_request = AccountRequest.find(params[:id])
@account_request.destroy
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted account request with ID: #{@account_request.id}.", request)
render json: { message: 'Account Request deleted' }, status: :no_content
rescue ActiveRecord::RecordNotFound => e
render json: { error: e.message }, status: :not_found
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Account request not found for deletion: #{e.message}", request)
render json: { error: e.message }, status: :not_found
end

private
Expand All @@ -74,6 +94,7 @@ def account_request_params
params[:account_request][:status] = 'Under Review'
# For Approval or Rejection of an existing request, raise error if user sends a status other than Approved or Rejected
elsif !['Approved', 'Rejected'].include?(params[:account_request][:status])
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Invalid status provided: #{params[:account_request][:status]}", request)
raise StandardError, 'Status can only be Approved or Rejected'
end
params.require(:account_request).permit(:username, :full_name, :email, :status, :introduction, :role_id, :institution_id)
Expand All @@ -83,8 +104,11 @@ def account_request_params
def create_approved_user
@new_user = User.new(name: @account_request.username, role_id: @account_request.role_id, institution_id: @account_request.institution_id, fullname: @account_request.full_name, email: @account_request.email, password: 'password')
if @new_user.save
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Approved account request and created user with ID: #{@new_user.id}.", request)
render json: { success: 'Account Request Approved and User successfully created.', user: @new_user}, status: :ok
else
errors = @new_user.errors.full_messages.join(', ')
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create user from approved account request ID: #{@account_request.id}. Errors: #{errors}", request)
render json: @new_user.errors, status: :unprocessable_entity
end
end
Expand Down
54 changes: 49 additions & 5 deletions app/controllers/api/v1/assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ class Api::V1::AssignmentsController < ApplicationController
# GET /api/v1/assignments
def index
assignments = Assignment.all
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all assignments.", request)
render json: assignments
end

# GET /api/v1/assignments/:id
def show
assignment = Assignment.find(params[:id])
# For now, just logging success - if error checking is added in the future, please add a log message for that with
# ExpertizaLogger.error
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched assignment with ID: #{assignment.id}.", request)
render json: assignment
end

# POST /api/v1/assignments
def create
assignment = Assignment.new(assignment_params)
if assignment.save
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Assignment created: #{assignment.as_json}", request)
render json: assignment, status: :created
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create assignment: #{assignment.errors.full_messages.join(', ')}", request)
render json: assignment.errors, status: :unprocessable_entity
end
end
Expand All @@ -26,8 +32,10 @@ def create
def update
assignment = Assignment.find(params[:id])
if assignment.update(assignment_params)
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated assignment with ID: #{assignment.id}.", request)
render json: assignment, status: :ok
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request)
render json: assignment.errors, status: :unprocessable_entity
end
end
Expand All @@ -37,11 +45,14 @@ def destroy
assignment = Assignment.find_by(id: params[:id])
if assignment
if assignment.destroy
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Assignment #{assignment.id} was deleted.", request)
render json: { message: "Assignment deleted successfully!" }, status: :ok
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to delete assignment #{$ERROR_INFO}", request)
render json: { error: "Failed to delete assignment", details: assignment.errors.full_messages }, status: :unprocessable_entity
end
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for deletion with ID: #{params[:id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
end
end
Expand All @@ -50,12 +61,15 @@ def destroy
def add_participant
assignment = Assignment.find_by(id: params[:assignment_id])
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for adding participant. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
new_participant = assignment.add_participant(params[:user_id])
if new_participant.save
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Added participant with User ID: #{params[:user_id]} to assignment ID: #{assignment.id}.", request)
render json: new_participant, status: :ok
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to add participant to assignment ID: #{assignment.id}. Errors: #{new_participant.errors.full_messages.join(', ')}", request)
render json: new_participant.errors, status: :unprocessable_entity
end
end
Expand All @@ -68,12 +82,15 @@ def remove_participant
if user && assignment
assignment.remove_participant(user.id)
if assignment.save
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Removed participant with User ID: #{user.id} from assignment ID: #{assignment.id}.", request)
render json: { message: "Participant removed successfully!" }, status: :ok
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to remove participant from assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request)
render json: assignment.errors, status: :unprocessable_entity
end
else
not_found_message = user ? "Assignment not found" : "User not found"
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "#{not_found_message} while removing participant.", request)
render json: { error: not_found_message }, status: :not_found
end
end
Expand All @@ -83,12 +100,15 @@ def remove_participant
def remove_assignment_from_course
assignment = Assignment.find(params[:assignment_id])
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for removing from course. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
assignment = assignment.remove_assignment_from_course
if assignment.save
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Removed assignment ID: #{assignment.id} from its course.", request)
render json: assignment , status: :ok
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to remove assignment ID: #{assignment.id} from course. Errors: #{assignment.errors.full_messages.join(', ')}", request)
render json: assignment.errors, status: :unprocessable_entity
end
end
Expand All @@ -102,12 +122,15 @@ def assign_course
if assignment && course
assignment = assignment.assign_course(course.id)
if assignment.save
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Assigned course ID: #{course.id} to assignment ID: #{assignment.id}.", request)
render json: assignment, status: :ok
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to assign course to assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request)
render json: assignment.errors, status: :unprocessable_entity
end
else
not_found_message = course ? "Assignment not found" : "Course not found"
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "#{not_found_message} while assigning course.", request)
render json: { error: not_found_message }, status: :not_found
end
end
Expand All @@ -116,12 +139,15 @@ def assign_course
def copy_assignment
assignment = Assignment.find_by(id: params[:assignment_id])
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for copying. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
new_assignment = assignment.copy
if new_assignment.save
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Copied assignment ID: #{assignment.id} to new assignment ID: #{new_assignment.id}.", request)
render json: new_assignment, status: :ok
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to copy assignment ID: #{assignment.id}. Errors: #{new_assignment.errors.full_messages.join(', ')}", request)
render json :new_assignment.errors, status: :unprocessable_entity
end
end
Expand All @@ -132,8 +158,10 @@ def copy_assignment
def show_assignment_details
assignment = Assignment.find_by(id: params[:assignment_id])
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for showing details. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched details for assignment ID: #{assignment.id}.", request)
render json: {
id: assignment.id,
name: assignment.name,
Expand All @@ -150,9 +178,12 @@ def show_assignment_details
def has_topics
assignment = Assignment.find_by(id: params[:assignment_id])
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking topics. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
render json: assignment.topics?, status: :ok
result = assignment.topics?
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked topics for assignment ID: #{assignment.id}. Has topics: #{result}", request)
render json: result, status: :ok
end
end

Expand All @@ -161,9 +192,12 @@ def has_topics
def team_assignment
assignment = Assignment.find_by(id: params[:assignment_id])
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking team assignment. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
render json: assignment.team_assignment?, status: :ok
result = assignment.team_assignment?
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked if assignment ID: #{assignment.id} is a team assignment. Result: #{result}", request)
render json: result, status: :ok
end
end

Expand All @@ -173,9 +207,12 @@ def valid_num_review
assignment = Assignment.find_by(id: params[:assignment_id])
review_type = params[:review_type]
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking valid number of reviews. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
render json: assignment.valid_num_review(review_type), status: :ok
result = assignment.valid_num_review(review_type)
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked valid number of reviews for assignment ID: #{assignment.id}, Review type: #{review_type}. Result: #{result}", request)
render json: result, status: :ok
end
end

Expand All @@ -184,9 +221,12 @@ def valid_num_review
def has_teams
assignment = Assignment.find_by(id: params[:assignment_id])
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking teams. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
render json: assignment.teams?, status: :ok
result = assignment.teams?
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked teams for assignment ID: #{assignment.id}. Has teams: #{result}", request)
render json: result, status: :ok
end
end

Expand All @@ -195,11 +235,15 @@ def has_teams
def varying_rubrics_by_round?
assignment = Assignment.find_by(id: params[:assignment_id])
if assignment.nil?
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking varying rubrics. ID: #{params[:assignment_id]}", request)
render json: { error: "Assignment not found" }, status: :not_found
else
if AssignmentQuestionnaire.exists?(assignment_id: assignment.id)
render json: assignment.varying_rubrics_by_round?, status: :ok
result = assignment.varying_rubrics_by_round?
ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked varying rubrics by round for assignment ID: #{assignment.id}. Result: #{result}", request)
render json: result, status: :ok
else
ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "No questionnaire exists for assignment ID: #{assignment.id}.", request)
render json: { error: "No questionnaire/rubric exists for this assignment." }, status: :not_found
end
end
Expand Down
Loading