Skip to content

feat: add logs to domain services#301

Open
7eliassen wants to merge 7 commits intochore/logs-and-optimizationfrom
domain-logs
Open

feat: add logs to domain services#301
7eliassen wants to merge 7 commits intochore/logs-and-optimizationfrom
domain-logs

Conversation

@7eliassen
Copy link
Copy Markdown

Add request-scoped business event logging to domain services.

  • Added new domain module name
  • Added DomainLoger interface to keep domain layer decoupled from infrastructure logging implementation. That preserve clean architecture.
  • Added PinoDomainLoggerAdapter that implements DomainLogger
  • Added logging for key business events:
    • Auth: Token issuance, expiration, session revocation
    • Notes: CRUD operations, relation changes, parent unlinks
    • Note settings: Creation, updates, cover changes, invitation regeneration
    • Team management: Member joins, role changes, removals
    • Files: Uploads and deletions
    • Editor tools: Tool creation
    • Note visits: User visit tracking
  • Updated DomainError handler to include reqId and domain module name in logs

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 85.78% (🎯 80%)
⬇️ -0.13%
9249 / 10782
🔵 Statements 85.78% (🎯 80%)
⬇️ -0.13%
9249 / 10782
🔵 Functions 79.55% (🎯 80%)
⬆️ +0.02%
284 / 357
🔵 Branches 84.82% (🎯 80%)
⬆️ +0.25%
464 / 547
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/domain/index.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/domain/service/auth.ts 95.48%
⬇️ -1.10%
90.9%
🟰 ±0%
100%
🟰 ±0%
95.48%
⬇️ -1.10%
114-119
src/domain/service/editorTools.ts 97.53%
⬆️ +0.57%
100%
🟰 ±0%
85.71%
🟰 ±0%
97.53%
⬆️ +0.57%
59-60
src/domain/service/fileUploader.service.ts 46.32%
⬇️ -1.04%
100%
🟰 ±0%
22.22%
🟰 ±0%
46.32%
⬇️ -1.04%
68-116, 125-126, 134-161, 168-195, 202-207, 214-215, 222-230
src/domain/service/note.ts 96.21%
⬆️ +0.31%
81.42%
⬇️ -1.18%
100%
🟰 ±0%
96.21%
⬆️ +0.31%
108-109, 141-142, 153-154, 187-188, 220-221, 322-323, 426-427, 443-444, 485-486, 535-536, 577-578
src/domain/service/noteSettings.ts 95.68%
⬇️ -1.50%
88%
🟰 ±0%
100%
🟰 ±0%
95.68%
⬇️ -1.50%
139-140, 146-153, 250-251
src/domain/service/noteVisits.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/domain/service/user.ts 71.96%
⬆️ +0.28%
80%
🟰 ±0%
57.14%
🟰 ±0%
71.96%
⬆️ +0.28%
54-55, 62-78, 94-95, 116-131
src/domain/service/shared/logger.ts 0% 0% 0% 0% 1-28
src/infrastructure/config/index.ts 99%
⬆️ +0.03%
50%
🟰 ±0%
100%
🟰 ±0%
99%
⬆️ +0.03%
191-192
src/infrastructure/logging/index.ts 95.71%
⬆️ +3.03%
80%
⬆️ +46.67%
100%
🟰 ±0%
95.71%
⬆️ +3.03%
8, 40-41
src/infrastructure/logging/pinoLoggerAdapter.ts 86.84% 100% 80% 86.84% 33-37
src/infrastructure/logging/reqId.context.ts 100% 100% 100% 100%
src/presentation/http/http-api.ts 96.1%
⬆️ +0.04%
90.32%
🟰 ±0%
93.75%
🟰 ±0%
96.1%
⬆️ +0.04%
103-111, 122-123, 325-326, 350-351
src/presentation/http/middlewares/common/reqIdContext.ts 100% 100% 100% 100%
src/presentation/http/middlewares/common/userIdResolver.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/presentation/http/middlewares/note/useNoteResolver.ts 97.26%
⬇️ -0.10%
85.71%
🟰 ±0%
100%
🟰 ±0%
97.26%
⬇️ -0.10%
60-61
src/presentation/http/middlewares/noteSettings/useMemberRoleResolver.ts 79.24%
⬇️ -7.29%
71.42%
🟰 ±0%
100%
🟰 ±0%
79.24%
⬇️ -7.29%
27-28, 42-50
src/presentation/http/middlewares/noteSettings/useNoteSettingsResolver.ts 95%
⬇️ -0.34%
80%
🟰 ±0%
100%
🟰 ±0%
95%
⬇️ -0.34%
25-26
src/presentation/http/policies/authRequired.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/presentation/http/policies/notePublicOrUserInTeam.ts 94.44%
⬇️ -3.33%
88.88%
⬆️ +1.38%
100%
🟰 ±0%
94.44%
⬇️ -3.33%
20-22
src/presentation/http/policies/userCanEdit.ts 93.61%
⬇️ -3.68%
85.71%
⬆️ +2.38%
100%
🟰 ±0%
93.61%
⬇️ -3.68%
29-31
src/repository/storage/postgres/orm/sequelize/index.ts 97.05%
⬆️ +0.39%
83.33%
⬆️ +3.33%
100%
🟰 ±0%
97.05%
⬆️ +0.39%
58-59
Generated in workflow #914 for commit 21ac065 by the Vitest Coverage Report Action

* Implementations are provided by the infrastructure layer
*/

export interface DomainLogger {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domain server shared space was designed as a place, where you declare methods of actual domain, that could be used by other domains

i think that the DomainLogger should be moved to infrastructure/logging/, since it does not really belongs here

(check readme inside of the domain/service/shared)

Comment on lines +27 to +32
const cachedLogger = loggerCache.get(moduleName);

if (cachedLogger) {
return cachedLogger;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like a part of different PR, should this branch be rebased?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the base branch to chore/logs-and-optimization

if (session.refreshTokenExpiresAt.getTime() < Date.now()) {
await this.userSessionRepository.removeUserSessionByRefreshToken(token);

this.logger.warn('Refresh token expired');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets print last 4 chars of the token in all logs about tokens

*/
export default async function authRequired(context: PolicyContext): Promise<void> {
const { request, reply } = context;
const logger = getRequestLogger('policies');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a policy name as a prefix of its logs?

Comment on lines +31 to +35
const logger = getRequestLogger('database');

logger.info(
{ durationMs: timing },
message
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comments please

@7eliassen 7eliassen marked this pull request as draft April 10, 2026 06:19
@7eliassen 7eliassen changed the base branch from main to chore/logs-and-optimization April 10, 2026 06:21
@codex-assistant codex-assistant bot marked this pull request as ready for review April 10, 2026 06:21
@codex-assistant
Copy link
Copy Markdown

Thanks for adding a description — the PR is now marked as Ready for Review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants