DEV Community

JongHwa
JongHwa

Posted on

[Simple SNS Project] Step 4. Search Implementation & Logging Strategy

Introduction

In Step 3, I connected users via the Follow system. Now, in Step 4, I implemented a Search Feature to let users find content.
More importantly, I established a Logging Strategy. I'm not just printing logs to the console; I'm saving user search patterns to a file to analyze trends later.


1. Search Implementation: LIKE Query

For the search functionality, I used the standard SQL LIKE operator.

// FeedRepository.java
Page<Feed> findByContentContainingOrderByCreatedAtDesc(String keyword, Pageable pageable);

Enter fullscreen mode Exit fullscreen mode
  • How it works: It translates to SELECT * FROM feed WHERE content LIKE '%keyword%'.
  • Performance Note: Starting a query with a wildcard (%) prevents the database from using standard B-Tree indexes (Full Table Scan).
  • Future Improvement: For a production-level application with millions of rows, I would switch to Full-Text Search (MySQL) or a dedicated search engine like Elasticsearch. But for this project scope, LIKE is sufficient.

2. Logging Strategy: Why File Logging?

Console logs disappear when the server restarts. To analyze user behavior (e.g., "What are people searching for?"), logs must be persisted.

I configured logback in application.yml to write logs to a specific file (logs/simple-sns.log).

logging:
  file:
    name: logs/simple-sns.log
  logback:
    rollingpolicy:
      max-history: 7 # Keep logs for 7 days

Enter fullscreen mode Exit fullscreen mode

And in the Service layer, I added a structured log message:

log.info("[SEARCH] keyword: {}", keyword);

Enter fullscreen mode Exit fullscreen mode

This specific format [SEARCH] acts as a tag that will make parsing easier in Step 6 (Statistics).


3. Conclusion

This step was about preparing for the future. By logging user actions, I am laying the groundwork for data-driven features like "Trending Keywords".

Top comments (0)