提问人:Scripta14 提问时间:9/10/2019 最后编辑:Scripta14 更新时间:3/1/2022 访问量:568
如何从Java中的控制台日志记录中删除空行?
How to remove blank lines from Console logging in Java?
问:
我用java.util.logging做了一个软件。应用程序运行良好,但输出在日志记录信息之间打印空白行。循环可能打印一个空行或多个空行。在循环期间,应用程序可以分析更多设备,当一切正常时,应用程序会跳转到下一个设备。如果它出现异常,它将在控制台上打印日志。
我的输出:
10/09/2019 10:16:24.671 - [com.ids.main.IdsMain.main] - [INFO] - MES Polling LAN attivo
10/09/2019 10:16:35.594 - [com.ids.factoryPatternProtocol.CallFactory.checkProtocol] - [WARNING] - connect timed out
10/09/2019 10:16:46.983 - [com.ids.factoryPatternProtocol.CallFactory.checkProtocol] - [WARNING] - connect timed out
10/09/2019 10:16:58.308 - [com.ids.factoryPatternProtocol.CallFactory.checkProtocol] - [WARNING] - connect timed out
我试图从代码中删除 (\n) 换行符,但什么都没有。在网上,我找到了这个解决方案
logger.info(message.substring(0,message.lastIndexOf('\n')));
但它没有用
主要
public static void main(String[] args) {
MyFormatter formatter=new MyFormatter();
ch=new ConsoleHandler();
logger.addHandler(ch);
ch.setFormatter(formatter);
logger.info("MES Polling LAN actived");
DAOFactory oracleFactory;
oracleFactory = DAOFactory.getDAOFactory(DAOFactory.ORACLE,dbConn,dbUser,dbPass);
if(flagLog.equalsIgnoreCase("N")) {
logger.setLevel(Level.OFF);
}
DeviceDAO devDAO=oracleFactory.getDeviceDAO();
List<DeviceMTConnect> devices=devDAO.getAllDevicesLAN(line,logger,ch);
CallFactory callFactory=new CallFactory();
callFactory.listDevices(devices,line,devDAO,logger,ch,minute);
}
public class MyFormatter extends Formatter {
// Create a DateFormat to format the logger timestamp.
private static final DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
@Override
public String format(LogRecord record) {
StringBuilder builder = new StringBuilder(1000);
builder.append(df.format(new Date(record.getMillis()))).append(" - ");
builder.append("[").append(record.getSourceClassName()).append(".");
builder.append(record.getSourceMethodName()).append("] - ");
builder.append("[").append(record.getLevel()).append("] - ");
builder.append(formatMessage(record));
// builder.append(System.getProperty("line.separator"));
builder.append("\n");
return builder.toString();
}
}
@Override
protected void checkProtocol(List<DeviceMTConnect> devicesFactory, int line, DeviceDAO devDAO, Logger logger, ConsoleHandler ch, long minute) {
DeviceMTConnect device = null;
long time = 0;
int i = 0;
if (minute > 0) {
time = minute * 60 * 1000L;
}
for (DeviceMTConnect d : devicesFactory) {
d.setLinea(linea);
d.setDeviceState(1);
// System.out.println(d);
}
while (true) {
try {
while (i < devicesFactory.size()) {
devicesFactory.get(i).setStatoDevice(devDAO.getDeviceStatus(devicesFactory.get(i).getDeviceCode(), logger, ch));
if (devicesFactory.get(i).getDeviceState() > 0) {
if (devicesFactory.get(i).getProtocol().equalsIgnoreCase(PROT1)) {
device = devicesFactory.get(i);
MTConnect mt = new MTConnect(device.getIp(), device.getPort());
ParserMTConnect pmtConnect = new ParserMTConnect();
pmtConnect.parser(mt.httpCall(), device, logger, ch);
//System.out.println();
}
if (device.getProtocol().equalsIgnoreCase(PROT2)) {
}
i++;
Thread.sleep(SleepingTime);
} else {
i++;
}
}
} catch (InterruptedException e) {
logger.warning(e.getMessage());
Utility.checkDeviceState(devDAO, device, logger, ch, time);
} catch (NumberFormatException e) {
logger.warning(e.getMessage());
Utility.checkDeviceState(devDAO, device, logger, ch, time);
} catch (IOException e) {
logger.warning(e.getMessage());
Utility.checkDeviceState(devDAO, device, logger, ch, time);
} catch (ParserConfigurationException e) {
logger.warning(e.getMessage());
Utility.checkDeviceState(devDAO, device, logger, ch, time);
} catch (SAXException e) {
logger.warning(e.getMessage());
Utility.checkDeviceState(devDAO, device, logger, ch, time);
} catch (ParseException e) {
logger.warning(e.getMessage());
Utility.checkDeviceState(devDAO, device, logger, ch, time);
} finally {
if (i < devicesFactory.size()) {
i++;
} else if (i == devicesFactory.size()) {
i = 0;
}
}
}
}
我想删除两个日志记录行信息之间的空行 任何人都可以帮我找到解决我问题的方法吗?
不带 MyFormatter 类的 NEW OUTPUT
set 10, 2019 11:03:19 AM com.ids.main.IdsMain main
INFORMAZIONI: MES Polling LAN enabled
set 10, 2019 11:03:30 AM com.ids.factoryPatternProtocol.CallFactory checkProtocol
AVVERTENZA: connect timed out
set 10, 2019 11:03:41 AM com.ids.factoryPatternProtocol.CallFactory checkProtocol
AVVERTENZA: connect timed out
答:
0赞
Francisco Ramirez
3/1/2022
#1
根据评论,该问题是由 .
检查日志记录是否具有正确格式的一种方法是将日志记录写入文件 ()。这样做,您将看到附加文本是由日志记录还是其他原因引起的。System.out.println
FileHandler
评论