'How to remove blank lines from Console logging in Java?
I made a software with java.util.logging. The application runs well but the output prints lines blank between logging information. Probably the loop prints a blank line or more blank lines. During the loop the application can analize more devices and when it's all ok, the application jumps on the next device. Just if it gets an exception, it will print on console a log.
My output:
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
I've tried to remove the (\n) newline from the code but nothing. Online I found this solution
logger.info(message.substring(0,message.lastIndexOf('\n')));
but It didn't work
MAIN
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;
}
}
}
}
I would like to remove the blank line between two logging lines information Can Anyone help me to find a way to solve me problem?
NEW OUTPUT without MyFormatter class
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
Solution 1:[1]
According to the comments, the issue was caused by a System.out.println.
One way to check if the logging has the proper format is writing the logging to a file (FileHandler). Doing this, you will see if the additional text is caused of the logging or something else.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Francisco Ramirez |
