• 개발환경
  • 실행환경
  • 실행환경 예제
  • 공통컴포넌트
개발환경 실행환경 실행환경 예제 공통컴포넌트
  • 실행환경 소개
  • 공통기반 핵심
  • 공통기반
  • 화면처리
  • 업무처리
  • 데이터처리
  • 연계통합
  • 배치처리
실행환경 소개
  • OverView
공통기반 핵심
    IoC Container
    • Basics
    • Dependencies
    • Bean Scopes
    • Customizing
    • Profiles
    • Inheritance
    • Extension
    • The ApplicationContext
    • Annotation
    • Components Scan
    • JSR 330
    • JavaConfig
    • Environment Abstraction
    • Inversion of Control
    • Generic
    AOP
    • AspectJ
    • AOP XML
    • AOP 가이드라인
  • Resource
  • SpEL
공통기반
    Server Security
    • Server Security Architecture
    • Authentication
    • Authorization
    • 설정 간소화
    • Server Security 업그레이드
  • Session 방식 접근제어
  • Scheduling 서비스
  • Logging
    • Log4j2

        Log4j2

        • Log4j2 코드로 설정
        • Log4j2 파일로 설정
    • SLF4J
  • Id Generation 서비스
  • Property
    • Property Service
    • Property Source
  • Environment
  • Cache
    • EhCache
    • Abstraction
  • Marshalling/Unmarshallig
  • XML Manipulation
  • Object Pooling
  • Crypto
    • 암복호화 서비스
    • Crypto 간소화 서비스
  • FTP
  • Mail 서비스
  • Compress/Decompress
    • 압축 파일의 종류
    File Upload/Download
    • File Upload

        File Upload

        • Multipart
    • File Download

        File Download

        • Tomcat 한글
  • File Handling
  • Excel
  • String Util
화면처리
    Web Servlet
    • MVC
    • DispatcherServlet
    • HandlerMapping
    • Tag Configuration
    • Controller
    • Validation
    Bean Validation
    • View
    • @CommandMap
    Web Reactive
    • Reactive Core
    • DispatcherHandler
    • Annotated Controllers
    • Functional Endpoints
    • WebClient
  • Ajax 지원
  • Internationalization
  • Security
    • Jakarta Commons Validator
    • Validation Rule
  • UI Adaptor
  • UI 비동기요청
  • jQuery Ajax
  • WebSocket
    • STOMP
    • SockJS
  • Bootstrap
  • 모바일 UX/UI
  • JQuery Mobile
업무처리
  • Exception Handling
  • Spring Web Flow
    • SWF Getting Started

        SWF Getting Started

        • Hello world 예제
    • SWF Configuration

        SWF Configuration

        • 시스템 설정
        • MVC 연동
        • Securing Flows
        • Flow Managed Persistence
    • SWF Elements

        SWF Elements

        • Flow 정의
        • Expression Language
        • Rendering View
        • Executing Actions
        • Flow 상속
데이터처리
  • DataSource
  • IBatis
    • iBATIS Configuration
    • Spring-iBATIS Integration
    • Data Type
    • parameterMap
    • Inline Parameters
    • resultMap
    • Dynamic SQL
    MyBatis
    • 주요 변경사항
    • 시작하기
    • Configuration XML
    • Mapper XML Files
  • MyBatis 적용 가이드
  • JPA
    • JPA Repository
    • Query Method
    MongoDB
    • MongoDB support
    • Repositories
    ORM
    • Entities
    • Entity Operation
    • Association Mapping
    • Query Language
    • Native SQL
    • Concurrency
    • Cache Handling
    • Fetch Strategy
    • Spring Integration
    • JPA Configuration
    Transaction
    • Declarative
    • Programmatic
    Reactive
    • R2DBC
    • MongoDB
    • Cassandra
    • Redis
연계통합
  • Naming Service
  • Integration Service
    • Metadata
    • API
  • WebService
  • Restful
  • Cloud Data Stream
  • Swagger
배치처리
    배치 실행환경 소개
    • SQLite
    • Logback logging
    Job
    • Job Variable
    Step
    • Step Variable
  • ItemReader
  • ItemWriter
  • Resource Variable
  • JobRepository
  • JobLauncher
    • Remote JobLauncher
  • JobRunner
  • JobRegistry
  • JobExplorer
  • JobOperator
  • Skip/Retry/Repeat
  • MultiDataProcessing
  • History Management
  • 동기/비동기 처리 예제
  • Listener
  • 병행처리
  • CodeBaseException
  • 센터 컷(CenterCut)
  • 이벤트알림 템플릿 관리
  • Flow Control
페이지보기 페이지편집 문서에 이슈 생성 프로젝트에 이슈 생성

실행환경

    • OverView
      • Basics
      • Dependencies
      • Bean Scopes
      • Customizing
      • Profiles
      • Inheritance
      • Extension
      • The ApplicationContext
      • Annotation
      • Components Scan
      • JSR 330
      • JavaConfig
      • Environment Abstraction
      • Inversion of Control
      • Generic
      • AspectJ
      • AOP XML
      • AOP 가이드라인
    • Resource
    • SpEL
      • Server Security Architecture
      • Authentication
      • Authorization
      • 설정 간소화
      • Server Security 업그레이드
    • Session 방식 접근제어
    • Scheduling 서비스
      • Log4j2
        • Log4j2 코드로 설정
        • Log4j2 파일로 설정
      • SLF4J
    • Id Generation 서비스
      • Property Service
      • Property Source
    • Environment
      • EhCache
      • Abstraction
    • Marshalling/Unmarshallig
    • XML Manipulation
    • Object Pooling
      • 암복호화 서비스
      • Crypto 간소화 서비스
    • FTP
    • Mail 서비스
      • 압축 파일의 종류
      • File Upload
        • Multipart
      • File Download
        • Tomcat 한글
    • File Handling
    • Excel
    • String Util
      • MVC
      • DispatcherServlet
      • HandlerMapping
      • Tag Configuration
      • Controller
      • Validation
      • View
      • @CommandMap
      • Reactive Core
      • DispatcherHandler
      • Annotated Controllers
      • Functional Endpoints
      • WebClient
    • Ajax 지원
    • Internationalization
      • Jakarta Commons Validator
      • Validation Rule
    • UI Adaptor
    • UI 비동기요청
    • jQuery Ajax
      • STOMP
      • SockJS
    • Bootstrap
    • 모바일 UX/UI
    • JQuery Mobile
    • Exception Handling
      • SWF Getting Started
        • Hello world 예제
      • SWF Configuration
        • 시스템 설정
        • MVC 연동
        • Securing Flows
        • Flow Managed Persistence
      • SWF Elements
        • Flow 정의
        • Expression Language
        • Rendering View
        • Executing Actions
        • Flow 상속
    • DataSource
      • iBATIS Configuration
      • Spring-iBATIS Integration
      • Data Type
      • parameterMap
      • Inline Parameters
      • resultMap
      • Dynamic SQL
      • 주요 변경사항
      • 시작하기
      • Configuration XML
      • Mapper XML Files
    • MyBatis 적용 가이드
      • JPA Repository
      • Query Method
      • MongoDB support
      • Repositories
      • Entities
      • Entity Operation
      • Association Mapping
      • Query Language
      • Native SQL
      • Concurrency
      • Cache Handling
      • Fetch Strategy
      • Spring Integration
      • JPA Configuration
      • Declarative
      • Programmatic
      • R2DBC
      • MongoDB
      • Cassandra
      • Redis
    • Naming Service
      • Metadata
      • API
    • WebService
    • Restful
    • Cloud Data Stream
    • Swagger
      • SQLite
      • Logback logging
      • Job Variable
      • Step Variable
    • ItemReader
    • ItemWriter
    • Resource Variable
    • JobRepository
      • Remote JobLauncher
    • JobRunner
    • JobRegistry
    • JobExplorer
    • JobOperator
    • Skip/Retry/Repeat
    • MultiDataProcessing
    • History Management
    • 동기/비동기 처리 예제
    • Listener
    • 병행처리
    • CodeBaseException
    • 센터 컷(CenterCut)
    • 이벤트알림 템플릿 관리
    • Flow Control
  1. Home
  2. 실행환경
  3. 공통기반
  4. Logging
  • 페이지 보기
  • 페이지 편집
  • 문서에 이슈 생성
  • 프로젝트에 이슈 생성

이 페이지의 구성

SLF4J

  • SLF4J
    • Getting Started
    • Migration to SLF4J from Legacy APIs
  • 참고 자료

SLF4J

Getting Started

SLF4J(Simple Logging Facade For Java)는 특정 Logging 서비스 구현체에 종속되지 않도록 추상화 계층을 제공하며,
Jakarta Commons Logging(JCL), Log4j, Logback 등과 함께 사용할 수 있다.
다음은 SLF4J 샘플 예제이다.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Slf4JLoggerTest {

   // SLF4J를 이용한 Logger 오브젝트 생성
   private static final Logger LOGGER = LoggerFactory.getLogger(Slf4JLoggerTest.class);

   // Parameterized logging - String 타입
   String message = "Hello, eGovFrame 3.0";
   String message2 = "Welcome to eGovFrame 3.0";

   LOGGER.debug("SLF4J Logger - {}", message); // 출력결과 - SLF4J Logger - Hello, eGovFrame 3.0
   LOGGER.debug("SLF4J Logger - {} and {}", message, message2); // 출력결과 - SLF4J Logger - Hello, eGovFrame 3.0 and Welcome to eGovFrame 3.0

   // Parameterized logging - Object 타입
   Object[] args = new Object[3];
   args[0] = "1";
   args[1] = Integer.valueOf("2");
   args[2] = new Date().toString();

   LOGGER.debug("SLF4J Logger - {}, {}, {}", args); // 출력결과 - SLF4J Logger - 1, 2, Fri Mar 23 11:08:28 KST 2014
}

1. SLF4J 기본 설정

  1. SLF4J API를 사용하기 위해 slf4j-api.jar를 추가한다.
<!-- SLF4J -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>x.x.x</version>
</dependency>
  1. Logging 충돌 방지를 위해 Spring의 Default Logging Framework인 commons-logging.jar를 제거하되,
    기존 Commons Logging API가 적절하게 변환되어 처리될 수 있도록 SLF4j JCL Bridge인 jcl-over-slf4j.jar를 추가한다.
<!-- Exclude Commons Logging in favor of SLF4J -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.maven.artifact.version}</version>
  <exclusions>
    <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<!-- SLF4J JCL Bridge -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>x.x.x</version>
</dependency>

2. Logging 구현체 설정

  1. SLF4J가 컴파일 시에 Logging 구현체를 사용할 수 있도록 구현체별 SLF4J Binding jar와 Implementation jar를 추가한다.
Logging 구현체SLF4J Binding jar
Log4j 2log4j-slf4j-impl.jar
Log4j 1.2slf4j-log4j12.jar
JDK 1.x Loggingslf4j-jdk14.jar
NOPslf4j-nop.jar
JCLslf4j-jcl.jar
Logbacklogback-classic.jar, logback-core.jar
  • Log4j 1.2 구현체 사용 시
<!--  SLF4J Log4j1.2 Binding -->
<dependency> 
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>x.x.x</version>
</dependency>

<!-- Log4j 1.2 -->
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2</version>
</dependency>
  • Log4j 2 구현체 사용 시
<!-- Log4j2 SLF4J Binding -->
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>x.x.x</version>
</dependency>

<!-- Log4j 2 -->
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>x.x.x</version>
</dependency>

3. SLF4J Logger 객체 생성과 메서드 사용

  1. Logger 객체 생성
  private static final Logger LOGGER = LoggerFactory.getLogger(Slf4JLoggerTest.class);
  1. 로깅 메서드 호출
  // {}-placeholder를 이용한 Parameterized Logging
  String message = "Hello, eGovFrame 3.0";

  LOGGER.debug("SLF4J Logger - {}", message);

Migration to SLF4J from Legacy APIs

기존 Legacy API을 유지한 채 SLF4J를 함께 사용하려면, SLF4J와 레거시 API를 연결할 수 있는 Bridge jar가 필요하다.
아래에서는 Log4j 1.x와 JCL 레거시를 기준으로 설명한다.

1. Logging 구현체 jar를 SLF4J Bridge jar로 대체

이는 각 구현체의 Logging 제어권을 SLF4J로 넘긴다는 것을 의미하며, 레거시 API를 유지하기 위해서 필요한 작업이다.

  1. Log4j 1.x 유지 시, log4j.jar를 log4j-over-slf4j.jar로 대체
<!-- Log4j 1.x -->
<!-- 
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>x.x.x</version>
</dependency>
-->

<!-- SLF4J Log4j 1.x Bridge -->  
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>log4j-over-slf4j</artifactId>
  <version>x.x.x</version>
</dependency>

(주의) log4j-over-slf4j.jar는 slf4j-log4j12.jar(SLF4j Binding)과 동시에 사용할 수 없다.

  1. JCL(Jakarta Commons Logging) 유지 시, commons-logging.jar를 jcl-over-slf4j.jar로 대체
<!-- Commons Logging -->
<!--
<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.1.1</version>
</dependency>
-->

<!-- SLF4j JCL Bridge -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>x.x.x</version>
</dependency>

2. 환경설정 파일을 logback으로 변경

log4j 환경설정 파일은 SLF4J가 인식할 수 없기 때문에, 기존 환경설정을 logback으로 변경해야한다.
log4j properties file translator 를 이용하거나 logback manual 을 참조하여 변경할 수 있다.

다음은 logback.xml 샘플이다.

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

참고 자료

  • SLF4J
  • Logback
  • 대표전화  1566-3598 (070-4448-2678)
  • 대표문의메일  egovframesupport@gmail.com
전자정부 표준프레임워크 Git
이용 안내 라이센스

COPYRIGHT (C) 2024 eGovFramework. ALL RIGHTS RESERVED.