Project

Profile

Help

Support #5467

closed

saxon PE 11.3 logging and bridging to log4j2 in java

Added by Erez Harari almost 2 years ago. Updated almost 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Category:
-
Sprint/Milestone:
-
Start date:
2022-05-01
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:
Platforms:

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

Actions #1

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.

Actions #2

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);;
            }
        }
    });
Actions #3

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

Actions #4

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
Actions #5

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

Actions #6

Updated by Erez Harari almost 2 years ago

Sure. how?

Actions #7

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);
Actions #8

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()));
        }
    }
Actions #9

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

Also available in: Atom PDF