Reputation: 2242
I have a bean that instantiates a schema through a static block, so that it is loaded once for every instance of MyClass. However I never see the logging (only saw it once when I entered the wrong path to the file, so that was the logging from the exception). If I replace the logging with System.out.println()
it shows up in the console.
My project uses SLF4J and LOG4J. I've seen this question: Putting Logger.info in static block which claims that LOG4J should have been initialized by the time the static block gets executed. My hunch is that by using SLF4J this is no longer guaranteed. Is that correct?
private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);
private static Schema XSD_SCHEMA = null;
static {
// NB The logging in this static block does not always appear in the output. It seems
// that the indirection between SLF4J and LOG4J causes slow initialization of the logging
// framework.
if (XSD_SCHEMA != null) {
LOGGER.info("Reading schema...");
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = null;
try {
schema = sf.newSchema(new File("some.xsd"));
LOGGER.info("Read schema successfully, using validation...");
} catch (SAXException e) {
LOGGER.error("Could not load validation schema, not using validation (Reason {})",
e);
}
XSD_SCHEMA = schema;
}
}
Upvotes: 0
Views: 77
Reputation: 31290
Simply omit this if statement
if (XSD_SCHEMA != null) {
As it is, it is always false and the static block never executes anythng useful.
You don't need it because the static block will only be exec'd once anyway.
Upvotes: 1