Logging & logger properties

apache_commons_compress
Terekhin Alexander 5 years ago
parent 8a9e39536c
commit 694bafcd98
  1. 43
      src/main/java/me/bearns/fias/service/StreamSaverImpl.java
  2. 1
      src/main/resources/application-default.properties
  3. 1
      src/main/resources/application-test.properties

@ -1,5 +1,6 @@
package me.bearns.fias.service; package me.bearns.fias.service;
import lombok.extern.slf4j.Slf4j;
import me.bearns.fias.exceptions.CommonException; import me.bearns.fias.exceptions.CommonException;
import me.bearns.fias.exceptions.UnmarshallingException; import me.bearns.fias.exceptions.UnmarshallingException;
import me.bearns.fias.helpers.UnmarshallerParameters; import me.bearns.fias.helpers.UnmarshallerParameters;
@ -11,16 +12,21 @@ import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement; import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.XMLEvent;
import java.io.InputStream; import java.io.InputStream;
import java.util.Iterator;
import java.util.function.Predicate; import java.util.function.Predicate;
@Slf4j
@Component @Component
public class StreamSaverImpl implements StreamSaver{ public class StreamSaverImpl implements StreamSaver{
@Override @Override
public void process(InputStream is, UnmarshallerParameters conf, Predicate filter) throws CommonException { public void process(InputStream is, UnmarshallerParameters conf, Predicate filter) throws CommonException {
log.debug("Init unmarshaller");
try { try {
// create xml event reader for input stream // create xml event reader for input stream
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
@ -35,6 +41,8 @@ public class StreamSaverImpl implements StreamSaver{
long countRead=0, countWrite=0, countErrors=0; long countRead=0, countWrite=0, countErrors=0;
log.debug("Start reading stream");
// loop though the xml stream // loop though the xml stream
while ((e = xmlEventReader.peek()) != null) { while ((e = xmlEventReader.peek()) != null) {
@ -50,12 +58,16 @@ public class StreamSaverImpl implements StreamSaver{
} catch (Exception ex) { } catch (Exception ex) {
countErrors++; countErrors++;
//log.error("Unmarshalling error {} in {}", ex.getMessage(), file.getName());
/*final Iterator attributes = ((StartElement) e).getAttributes(); log.error("Unmarshalling error[{}] {}", countErrors, ex.getMessage());
attributes.forEachRemaining(
(a) -> log.error("Object dump: {} {}", ((Attribute) a).getName(), ((Attribute) a).getValue()) if(log.isTraceEnabled()) {
);*/ //debug print
final Iterator attributes = ((StartElement) e).getAttributes();
attributes.forEachRemaining(
(a) -> log.error("Object dump: {} {}", ((Attribute) a).getName(), ((Attribute) a).getValue())
);
}
continue; continue;
} }
@ -67,8 +79,9 @@ public class StreamSaverImpl implements StreamSaver{
//Save to repository //Save to repository
conf.getRepository().save(obj); conf.getRepository().save(obj);
countWrite++;
/*if(++countWrite%10000==0) { /*if(countWrite%10000==0) {
repository.flush(); repository.flush();
log.info("Write {} items to repository.", countWrite); log.info("Write {} items to repository.", countWrite);
}*/ }*/
@ -77,21 +90,21 @@ public class StreamSaverImpl implements StreamSaver{
xmlEventReader.next(); xmlEventReader.next();
} }
} }
log.info("SUCSESS: read={}, write={}", countRead, countWrite);
if(countErrors > 0) log.warn("Completed with errors, problems count: {}", countErrors);
} catch (XMLStreamException ex) { } catch (XMLStreamException ex) {
//todo log log.error("Caught XMLStreamException (createXMLEventReader or peak methods)");
throw new UnmarshallingException(ex); //createXMLEventReader & peak throw new UnmarshallingException(ex);
} catch (JAXBException ex) { } catch (JAXBException ex) {
//todo log log.error("Caught JAXBException on createUnmarshaller()");
throw new UnmarshallingException(ex); //createUnmarshaller(); throw new UnmarshallingException(ex);
} }
//write to DB from cache //write to DB from cache
conf.getRepository().flush(); conf.getRepository().flush();
/*log.info("SUCSESS: read={}, write={}", countRead, countWrite); log.debug("Repository flush completed");
if(countErrors > 0) {
log.warn("Unmarshalling errors suppressed: {}", countErrors);
log.warn("Check data consistency in {}", file.getName());
}*/
} }
} }

@ -3,3 +3,4 @@ spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=sa spring.datasource.username=sa
spring.datasource.password=password spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
logging.level.root=INFO

@ -3,3 +3,4 @@ spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa spring.datasource.username=sa
spring.datasource.password=password spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
logging.level.root=DEBUG
Loading…
Cancel
Save