반응형

 

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.17.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.17.1</version>
</dependency>

 

 

<!-- 경로 : main/resources/log4j2.xml -->
<?xml version="1.0" encoding="UTF-8"?>

<configuration status="DEBUG">

	<!-- 콜솔 -->
	<Console name="console" target="SYSTEM_OUT">
		<PatternLayout
			pattern="%d{yyyy-MM-dd hh:mm:ss} %5p [%c] %m%n" />
	</Console>

	<Appenders>
		<!-- 파일 -->
		<RollingFile name="as_api_param">
			<FileName>/home/Search/logs/search/search_param.log</FileName>
					<FilePattern>/home/Search/logs/search/search_param_%d{yyyy-MM-dd}.log
			</FilePattern>
			<PatternLayout>
				<Pattern>%d{yyyy-MM-dd HH:mm:ss} %5p [%c] %m%n</Pattern>
			</PatternLayout>
			<Policies>
				<TimeBasedTriggeringPolicy interval="1"
					modulate="true" />
			</Policies>
		</RollingFile>

		<RollingFile name="as_api_uri">
			<FileName>/home/Search/logs/search/search_uri.log</FileName>
			<FilePattern>/home/Search/logs/search/search_uri_%d{yyyy-MM-dd}.log</FilePattern>
			<PatternLayout>
				<Pattern>%d{yyyy-MM-dd HH:mm:ss} %5p [%c] %m%n</Pattern>
			</PatternLayout>
			<Policies>
				<TimeBasedTriggeringPolicy interval="1"
					modulate="true" />
			</Policies>
		</RollingFile>
	</Appenders>

	<loggers>
		<Logger name="AS_API_PARAM" level="DEBUG" additivity="false">
			<AppenderRef ref="as_api_param" />

		</Logger>
		<Logger name="AS_API_URI" level="DEBUG" additivity="false">
			<AppenderRef ref="as_api_uri" />
		</Logger>
		<Logger name="CONSOLE" level="DEBUG" additivity="false">
			<AppenderRef ref="console"/>
		</Logger>
	</loggers>


</configuration>

Console → Console에 출력

RollingFile → 로그 찍는 형식 (Rolling이란 파일을 계속 갱신하는 것을 의미)
name → 로그 찍는 형식의 이름 (AppenderRef에서 사용)

FileName → 로그 저장경로와 로그 이름
FilePattern → Rolling시 저장될 위치및 로그파일 이름
PatternLayout  → 로그에 출력할 로그 내용 형식
%d{yyyy-MM-dd HH:mm:ss} → yyyy-MM-dd HH:mm:ss 로그 찍힌 시각
%5p → 로그형식
%C → 클래스명 표시
%m → 로그 출력 본 내용
%n → 각 플랫폼에 맞는 개행



TimeBasedTriggeringPolicy → 로그에 사용할 정책
interval → interval 단위로 로그 파일 새로 생성 (롤오버라고 한다)
FilePattern 에 적인 형식이 yyyy-MM-dd일 경우  interval=1 기준 1일 단위
yyyy-MM-dd-hh 일 경우는 1시간 단위
moulate → false인 경우 application 시작 시각으로부터가 기준이고 true인 경우 정각인 시각마다 롤오버가 일어난다.

Loggers → 위에 선언한 로그 형식중 사용할 로그 형식 선언
Logger 
 - name → java파일에서 사용할 Log의 이름
 - level → 로그 Level 해당 로그 이상만 로그 적재
 - addtivity → 중복 로깅 여부 (RollingFile name이 여러 AppenderRef ref에서 참조될 시 한번만 찍게 된다.)

package com.company.AS_API;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;




@Controller
public class companyController {
	
	@RequestMapping(value = "/logs", method = RequestMethod.GET)
	public void makeLogs {
		
		Logger console = LogManager.getLogger("CONSOLE");
		Logger APIParamLogger = LogManager.getLogger("AS_API_PARAM");
		Logger APIURILogger = LogManager.getLogger("AS_API_URI");

		// 로그 찍기
		APIParamLogger.debug("debug 입니다.");
		APIParamLogger.info("info 입니다.");
		APIParamLogger.warn("warn 입니다.");
		APIParamLogger.error("error 입니다.");
		APIParamLogger.fatal("fatal 입니다.");
	}
}

 

 

 

─── 참고 RollingFile + Console ── 

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

	<Appenders>
		<!-- 콜솔 -->
		<Console name="console" target="SYSTEM_OUT">
			<PatternLayout
				pattern="%d{yyyy-MM-dd hh:mm:ss} %5p [%c] %m%n" />
		</Console>
		<RollingFile name="admin_real">
			<FileName>/home/Search/logs/admin/AA.log</FileName>
			<FilePattern>/home/Search/logs/admin/AA_%d{yyyy-MM-dd}.log</FilePattern>
			<PatternLayout>
				<Pattern>%d{yyyy-MM-dd HH:mm:ss} %5p [%c] %m%n</Pattern>
			</PatternLayout>
			<Policies>
				<TimeBasedTriggeringPolicy interval="1"
					modulate="true" />
			</Policies>
		</RollingFile>
		<RollingFile name="admin_local">
			<FileName>D://logs//aa.log</FileName>
			<FilePattern>D://logs//aa_%d{yyyy-MM-dd}.log</FilePattern>
			<PatternLayout>
				<Pattern>%d{yyyy-MM-dd HH:mm:ss} %5p [%c] %m%n</Pattern>
			</PatternLayout>
			<Policies>
				<TimeBasedTriggeringPolicy interval="1"	modulate="true" />
			</Policies>
		</RollingFile>
	</Appenders>

	<loggers>
		<Logger name="ADMIN_LOGGER_LOCAL" level="DEBUG"
			additivity="false">
			<AppenderRef ref="console" />
			<AppenderRef ref="admin_local" />
		</Logger>

		<Logger name="ADMIN_LOGGER_REAL" level="DEBUG"
			additivity="false">
			<AppenderRef ref="console" />
			<AppenderRef ref="admin_real" />
		</Logger>
		
		
	</loggers>


</configuration>

 

 

형식 참고 블로그 : http://changpd.blogspot.com/2013/05/spring-lo4j.html

반응형