Support #5467
closedsaxon PE 11.3 logging and bridging to log4j2 in java
0%
Description
I see there is a proprietary Logger and my question is how to map it into my application log4j2. Additionally there are some APIs which write to STDERR/STDOUT. How do I make these write to log4j2, or to saxon Logger? Specifically, config.displayLicenseMessage(): How do I get this output into a String?
Thanks, Erez
Updated by Michael Kay almost 2 years ago
It should be fairly easy to write a Java class that subclasses net.sf.saxon.lib.Logger
and writes the messages to a log4j logger. (We decided to avoid the complications caused by having a dependency on log4j, and we're quite glad we did so, given the security problems that have arisen.)
You can register your logger using Configuration.setLogger()
.
Configuration.displayLicenseMessage() writes to the supplied Logger. If you want to capture the message as a string, I guess you would have to implement a Logger that allows you to retrieve the most recent message(s).
We did a review a while ago and I think nearly all messages are now directed to the Configuration's Logger rather than to stderr or stdout. If you're aware of exceptions to this, please let us know. The only ones I'm aware of are in test or diagnostic code.
Updated by Erez Harari almost 2 years ago
Great: public static final Logger log =
Configuration config = processor.getUnderlyingConfiguration();
config.setLogger(new net.sf.saxon.lib.Logger() {
@Override
public void println(String text, int level) {
//info 0; warn 1; error 2; fatal 3;
switch(level) {
case 0:
log.info(text);;
break;
case 1:
log.warn(text);;
break;
default:
log.error(text);;
}
}
});
Updated by Erez Harari almost 2 years ago
Still, When I provide an invalid XSLT file, I get some info to STDERR and some to Logger:
STDERR: Error at xsl:keyyyy on line 5 column 59 XTSE0010 Unknown XSLT element: LOGGER: 2022-05-02 09:51:47,220 [main] ERROR [xml.XMLTransformer] net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
Updated by Erez Harari almost 2 years ago
Error at xsl:keyyyy on line 5 column 59
XTSE0010 Unknown XSLT element: <keyyyy>
2022-05-02 09:51:47,220 [main] ERROR [xml.XMLTransformer] net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
Updated by Michael Kay almost 2 years ago
Static errors in the stylesheet are not reported to the Logger, they are reported to the ErrorReporter associated with the XsltCompiler. The default ErrorReporter writes to standard error output, but you can change this by setting your own ErrorReporter
Updated by Michael Kay almost 2 years ago
xsltCompiler.setErrorReporter(error -> {
...
})
Or you could set it using
StandardErrorReporter reporter = new StandardErrorReporter();
reporter.setLogger(....)
xsltCompiler.setErrorReporter(reporter);
Updated by Erez Harari almost 2 years ago
Found it thanks:
xsltCompiler.setErrorReporter(new SaxonErrorReporter());
private static class SaxonErrorReporter implements ErrorReporter {
@Override
public void report(XmlProcessingError xmlProcessingError) {
log.error(String.format("SAXON: %s: %s, location: %s, path: %s", xmlProcessingError.getErrorCode(), xmlProcessingError.getMessage(), xmlProcessingError.getLocation(), xmlProcessingError.getPath()));
}
}
Updated by Michael Kay almost 2 years ago
- Status changed from New to Closed
Closing this, as the question appears to have been answered.
Please register to edit this issue