spring将日志写入一个文件。log/。使用Java记录器的txt文件
我制作了一个日志系统,但我需要将日志消息写入一个单独的log/txt文件中
我想到了这个:
public class UIRestService {
private static final Logger LOGGER = Logger.getLogger(UIRestService.class.toString());
FileHandler fh;
public UIRestService() {
try {
fh = new FileHandler("E:/MyLog.log");
LOGGER.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
} catch (IOException e){
e.printStackTrace();
}
}
public MainDTO registerEORI(@RequestBody EoriRegistration json){
long startTime = System.currentTimeMillis();
try {
LOGGER.info("--- Start EORI registration ---");
EoriData entity = Converter.convertDTO2Entity(json);
LOGGER.info("Converted DTO to Entity");
MainDTO dto = dataManager.registration(entity);
if (dto instanceof EoriRegistration) {
msgManager.writeMsg(json.getState(), json.getC8_number() == null ? "test" : json.getC8_number());//TODO remove after IAM json.getC8_number()
}
return dto;
} catch (Exception e){
LOGGER.info("Exception in registerEORI");
e.printStackTrace();
Error error = new Error(Constants.TYPE_DTO2);
error.setErrorDescription(e.getMessage());
LOGGER.info("Exception in registerEORI" + error);
return error;
} finally {
long endTime = System.currentTimeMillis();
LOGGER.info("Registering EORI id: " + json.getId() + " , eori number:" + json.getEoriNumber() + " , elapsed:" + (endTime - startTime));
}
}
}
我手动创建了MyLog。日志文件位于E:/,但没有任何内容。我没有手动创建文件,也没有尝试。它说构造函数从未被使用过。你能告诉我我做错了什么吗?我用了这个例子-Example
# 1 楼答案
我想你必须设定好日志级别
我总是按照vogella的教程初始化我的记录器。com(http://www.vogella.com/tutorials/Logging/article.html#logexample_createLogger),它们工作正常:
(来源:http://www.vogella.com/tutorials/Logging/article.html#logexample_createLogger)