Spring Boot配置log4j记录日志(2)
- UID
- 1066743
|
Spring Boot配置log4j记录日志(2)
代码中打印日志
使用方式非常简单
log4j2静态加载配置文件
如下:
package com.biologic.api.service.impl;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;
import org.springframework.stereotype.Service;
import com.biologic.api.service.LogService;
@Service
public class LogServiceImpl implements LogService {
public static void main(String[] args) throws IOException {
File file = new File("F:/eclipseworkspace/platform/report-api/src/main/resources/log4j.xml");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
final ConfigurationSource source = new ConfigurationSource(in);
Configurator.initialize(null, source);
Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
logger.debug("统计异常池数量异常,异常信息如下:e.getStackTrace().toString()");
logger.error("统计异常池数量异常,异常信息如下:e.getStackTrace().toString()");
logger.info("统计异常池数量异常,异常信息如下:e.getStackTrace().toString()");
}
} |
|
|
|
|
|