로깅 아키텍처

moon.kick·2025년 4월 17일

좋아, 이제 두 가지 주제 정리해줄게:


✅ 1. 로깅 아키텍처: ELK / Loki 연동, AsyncAppender, 환경별 설정

🌍 ① 로그를 로그 서버(ELK, Loki 등)로 보내는 구조

✅ ELK (Elasticsearch + Logstash + Kibana)

  • Logback + logstash-logback-encoder를 통해 JSON 형식으로 로그 전송
  • 구성 흐름:
Spring 애플리케이션
    ↓ (Logback + JSON Encoder)
Logstash (TCP/UDP 수신)
    ↓
Elasticsearch (검색 저장소)
    ↓
Kibana (웹 UI 대시보드)

✅ 필수 설정 예시 (logback-spring.xml)

<appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>127.0.0.1:5000</destination>
    <encoder class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>

<root level="INFO">
    <appender-ref ref="logstash"/>
</root>

✅ Loki + Grafana 연동

  • Loki는 로그 수집용 Prometheus 스타일 저장소
  • Spring Boot에서는 Promtail 또는 Fluent Bit으로 Loki로 전송 가능

⚡ ② 비동기 로그 처리: AsyncAppender

  • 동기 로깅은 로그 출력할 때마다 응답 지연 발생 가능
  • AsyncAppender는 내부 큐를 이용해 비동기 처리 → 애플리케이션 응답 속도 ↑

✅ 설정 예시

<appender name="asyncConsole" class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="console" />
    <queueSize>512</queueSize>
    <discardingThreshold>0</discardingThreshold>
</appender>

<root level="INFO">
    <appender-ref ref="asyncConsole"/>
</root>

🧩 ③ 환경별 로그 설정(dev/prod 분리)

logback-spring.xml + Spring profile 연동

<springProfile name="dev">
    <root level="DEBUG">
        <appender-ref ref="console" />
    </root>
</springProfile>

<springProfile name="prod">
    <root level="WARN">
        <appender-ref ref="rollingFile" />
    </root>
</springProfile>

✅ application.yml

spring:
  profiles:
    active: dev  # 또는 prod

✅ 2. Spring Security 설정 방식 두 가지

방법 ①: WebSecurityConfigurerAdapter (이전 방식, Spring Security 5까지)

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin();
    }
}

✅ 특징: 상속 구조 → 오버라이딩 방식. Spring Security 5.7 이후 deprecated


방법 ②: SecurityFilterChain Bean 방식 (현행 방식)

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().permitAll()
            )
            .formLogin(withDefaults());
        return http.build();
    }
}

✅ 특징: 함수형 구성 방식이며, Spring Boot 2.7+/Security 5.7+ 이후 권장


필요하면:

  • 🔐 JWT 기반 인증 또는 OAuth2 로그인 흐름
  • 🔄 Spring Security 필터 순서 구조도
  • 🔓 로그인/로그아웃 커스터마이징

도 함께 해줄 수 있어!

어떤 주제로 더 확장해볼까? 😊

profile
@mgkick

0개의 댓글