8.1 KiB
Logging Guidelines
Logging and system-log conventions for the library-management backend.
Overview
The project requires system maintenance/log support for key operation logs, data
backup support, and exception tracing. MySQL should include a system log table,
illustratively system_logs, for durable audit and troubleshooting records.
What to Log
Record key operations that affect security, data integrity, or inventory:
- Login success/failure and logout for administrator, librarian, and reader roles when applicable.
- Permission changes, role changes, and denied protected operations.
- Book create, update, delete, category maintenance, and warehousing/intake.
- Borrow, return, renew, overdue handling, and automatic inventory status updates.
- Reader profile, eligibility, and contact information changes.
- Data backup events and restore-related maintenance actions.
- Unhandled exceptions and failed database operations with safe context.
System Log Fields
When the schema is introduced, system_logs should preserve enough information
to trace actions without exposing sensitive data. Useful fields include an ID,
operator ID, operator role, operation type, target entity/table, target ID,
result status, message, timestamp, and request IP when available.
Sensitive Data
Do not log passwords, raw credentials, full request bodies, database connection strings, or unnecessary personal data. Prefer IDs and operation summaries over large before/after payloads.
Exception Tracing
Server logs may contain technical stack traces for developers. User-facing JSP pages should receive concise messages. Durable system logs should record the operation, actor, failure category, and correlation details needed to locate the server-side exception.
Scenario: Admin User Management Audit And System Log Viewer
1. Scope / Trigger
- Trigger: administrator user/account management writes durable audit rows, and
/admin/system-logsreads those rows through a Servlet -> Service -> DAO -> MySQL flow. - The viewer is read-only; mutation belongs to the business service that owns the operation being audited.
2. Signatures
- DB signature:
system_logs(id, operator_id, operator_role, operation_type, target_table, target_id, result_status, message, request_ip, created_at). - DAO signatures:
SystemLogDao.search(criteria),SystemLogDao.findOperationTypes(), andSystemLogDao.create(connection, log). - Service signatures:
SystemLogService.searchLogs(AuthenticatedUser actor, SystemLogSearchCriteria criteria)and user-management methods that acceptrequestIpfor audit rows. - Routes:
GET /admin/system-logs; user-management audit sources includePOST /admin/users,POST /admin/users/update, andPOST /admin/users/deactivate.
3. Contracts
/admin/system-logsrequiresVIEW_SYSTEM_LOGS; user-management write routes requireMANAGE_USERS.- Search criteria fields are
operationType,keyword,createdFrom, andcreatedTo; dates useYYYY-MM-DD. - System-log result rows are newest-first and may include missing joined user
names. Entity display helpers must render an operator label as display name,
username,
User #<id>, orSystemin that order. - JSPs must use safe JavaBean helpers such as
operatorLabel,operatorMetaText,resultStatusCode, andresultStatusName; do not put raw database status strings into CSS class names. - Audit messages must summarize actions with IDs/usernames/roles, never passwords, raw credentials, password hashes, or request bodies.
4. Validation & Error Matrix
- Unauthenticated or unauthorized viewer ->
You do not have permission to view system logs. operationTypelonger than 64 characters -> field error onoperationType.keywordlonger than 120 characters -> field error onkeyword.- Invalid date text ->
Start date must use YYYY-MM-DD.orEnd date must use YYYY-MM-DD. createdFromaftercreatedTo-> field error oncreatedTo.- DAO failure while loading logs -> server-side Java logger entry plus
System log service is temporarily unavailable. Please try again later. - Audit insert failure inside a user-management transaction -> rollback the mutating operation and return the user-management unavailable message.
5. Good/Base/Bad Cases
- Good: user creation and its
user.createaudit row commit in the same transaction, includingoperator_id,operator_role,target_table=users,target_id,result_status=success, andrequest_ip. - Base: a system log with
operator_idbut no joined user displays asUser #<id>, not asSystem. - Bad: a JSP renders
class="status-${log.resultStatus}"from an arbitrary database value.
6. Tests Required
- Service checks for permission denial, criteria validation, date-range validation, DAO failure fallback, newest-first/search DAO contract, and empty results.
- User-management service checks for audit log creation on create/update/ deactivate and rollback when audit creation fails inside the transaction.
- Entity/display checks for operator fallback labels and normalized result status CSS codes.
- JSP scan confirming no scriptlet, SQL, JDBC, password, or password-hash rendering in admin/log pages.
7. Wrong vs Correct
Wrong
<span class="status-${log.resultStatus}">${log.resultStatus}</span>
Correct
<span class="status-${log.resultStatusCode}">
<c:out value="${log.resultStatusName}" />
</span>
Scenario: Login Diagnostic Logging
1. Scope / Trigger
- Trigger: Windows deployment login failures need server-side diagnostics across
LoginServlet -> AuthServiceImpl -> JdbcUserDao -> JdbcUtilwithout changing the generic user-facing login messages.
2. Signatures
- Servlet route:
POST /loginwithusername,password, and optional same-applicationredirect. - Service signature:
AuthService.authenticate(String username, String password). - DAO signature:
UserDao.findActiveByUsername(String username). - DB config keys:
db.driver,db.url,db.username, anddb.password.
3. Contracts
- Login request logs may include remote address, context path, redirect presence, username presence/length, sanitized username, and whether normalization changed the username.
- Authentication logs must distinguish missing required fields, active user not found, password mismatch, service error, and success.
- JDBC logs must confirm
db.propertiesloading, required key resolution, connection attempts, successful connections, and driver/connection failures. - Logs must never include raw passwords, password hashes, salts, database passwords, or unredacted password-like JDBC URL parameters.
4. Validation & Error Matrix
- Missing username or password -> log missing-field category and return the existing required-field message.
- Unknown or inactive username -> log
active-user-not-foundand return the existing invalid-credentials message. - Existing user with bad password -> log
password-mismatchand return the existing invalid-credentials message. - Missing DB config or JDBC failure -> log server-side details with credentials redacted and return the existing service-unavailable message.
5. Good/Base/Bad Cases
- Good: a failed login shows whether the request reached the servlet, whether the username was normalized, whether the active user row was found, and whether password verification failed.
- Base: successful login keeps logging user ID and role only.
- Bad: a diagnostic log writes
password,password_hash, salt, or a JDBC URL containingpassword=secret.
6. Tests Required
- Run
mvn testor the documented Maven path to compile Servlet, service, DAO, and utility code. - Scan changed logs for password/hash/salt/database-password output before finishing.
- Keep
AuthServiceCheckbehavior expectations unchanged for required fields, invalid credentials, success, permission checks, and DAO failure fallback.
7. Wrong vs Correct
Wrong
LOGGER.info("Login failed password=" + password + " hash=" + user.getPasswordHash());
Correct
LOGGER.info("Login failed reason=password-mismatch userId=" + user.getId());