Proper logging is essential for debugging, monitoring, and maintaining the Comserv system. This document outlines best practices for logging throughout the application, with a focus on the `log_with_details` method.
The preferred logging method in the Comserv system is `log_with_details`, which provides comprehensive context for each log entry:
$self->logging->log_with_details($c, 'info', __FILE__, __LINE__, 'method_name',
"Detailed message with relevant information");
Use appropriate log levels based on the importance and impact of the event:
$self->logging->log_with_details($c, 'info', __FILE__, __LINE__, 'action_name',
"Accessing action with parameters: " . join(', ', @parameters));
$self->logging->log_with_details($c, 'info', __FILE__, __LINE__, 'check_permission',
"User $username checking permission for $resource: " . ($has_permission ? "granted" : "denied"));
$self->logging->log_with_details($c, 'info', __FILE__, __LINE__, 'update_record',
"Updated record ID $id with values: " . $c->req->dump);
$self->logging->log_with_details($c, 'error', __FILE__, __LINE__, 'process_data',
"Error processing data: $error_message");
Use debug logging for detailed information that helps during development and troubleshooting:
$self->logging->log_with_details($c, 'debug', __FILE__, __LINE__, 'complex_calculation',
"Intermediate result: $result, inputs: $input1, $input2");
In addition to logging, provide appropriate feedback to users:
# Log the error
$self->logging->log_with_details($c, 'error', __FILE__, __LINE__, 'save_document',
"Failed to save document: $error_message");
# Add to stash for display
$c->stash(
error_msg => "Unable to save your document. Please try again.",
debug_msg => "Technical details: $error_message" # Only shown in debug mode
);
For successful operations, log the success and provide user feedback:
# Log the success
$self->logging->log_with_details($c, 'info', __FILE__, __LINE__, 'create_user',
"Successfully created user: $username");
# Add to stash for display
$c->stash(
success_msg => "User account created successfully."
);
Application logs are stored in:
To view recent logs, use:
tail -n 50 /home/shanta/PycharmProjects/comserv2/Comserv/logs/application.log
To search for specific log entries:
grep -i "error" /home/shanta/PycharmProjects/comserv2/Comserv/logs/application.log
The centralized AdminAuth system provides enhanced authentication logging capabilities:
# In controller methods return unless $self->admin_auth->require_admin_access($c, 'action_name'); # The AdminAuth utility automatically logs: # - Authentication attempts # - Session validation details # - Username resolution process # - Access granted/denied decisions # - Redirect actions for unauthorized users
sub action_name :Path('path') :Args(0) {
my ($self, $c) = @_;
# Log the action
$self->logging->log_with_details($c, 'info', __FILE__, __LINE__, 'action_name',
"Accessing action_name");
# Action logic...
# Log success
$self->logging->log_with_details($c, 'info', __FILE__, __LINE__, 'action_name',
"Successfully completed action_name");
}
eval {
# Code that might fail
};
if ($@) {
# Log the error
$self->logging->log_with_details($c, 'error', __FILE__, __LINE__, 'method_name',
"Error occurred: $@");
# Add to stash for display
$c->stash(
error_msg => "An error occurred: " . $self->_user_friendly_error($@)
);
}
Recent system enhancements have improved logging capabilities across the application:
All admin controllers have been updated with enhanced logging:
The Admin::Git controller (965+ lines) includes comprehensive logging for:
When migrating controllers to use AdminAuth, ensure proper logging integration:
# Import AdminAuth
use Comserv::Util::AdminAuth;
# Add admin_auth method
sub admin_auth {
my ($self) = @_;
return Comserv::Util::AdminAuth->new();
}
# Use in controller methods
sub some_action :Path('action') :Args(0) {
my ($self, $c) = @_;
# AdminAuth automatically handles authentication logging
return unless $self->admin_auth->require_admin_access($c, 'some_action');
# Continue with your action-specific logging
$self->logging->log_with_details($c, 'info', __FILE__, __LINE__, 'some_action',
"Performing some_action for user: " . ($c->session->{username} || 'unknown'));
}
Consistent and detailed logging is essential for maintaining and troubleshooting the Comserv system. Always use the `log_with_details` method to ensure comprehensive logging throughout the application. The recent AdminAuth integration provides enhanced authentication logging capabilities that should be leveraged in all admin controllers.