vantage
vantage

Reputation: 31

How do I log a message from Javascript called by Camel?

I am calling a Javascript file from Apache Camel XML DSL running inside Apache ServiceMix as follows:

<endpoint id="myJavascript" uri="language:javascript:file:data/myJavascript.js"/>
...
<to ref="myJavascript"/>
...

I want to log a message from inside myJavascript.js so that the message shows up in the main Camel Context log.

The Camel doc here indicates that the CamelContext Java object is passed through. I was hoping it would include a getter for the logger so that I could do something like this within the script:

var log = context.getLog();
log.debug("Some debug log message");

However, there is no reference to a logger (I noted a getter for a Debugger and an Interceptor though) so it looks like I'm out of luck.

Any ideas how I could achieve this?

Upvotes: 1

Views: 568

Answers (1)

vantage
vantage

Reputation: 31

I found my answer buried deep inside this article here.

I just needed to add the following to my Javascript:

var LOG = org.slf4j.LoggerFactory.getLogger(exchange.getFromRouteId());

This uses the Camel Exchange From Route Id as the log name, but you could use the script name instead.

Then I can call the logger as follows and the messages end up in the main Camel logger:

LOG.info("Log message at INFO level");
LOG.debug("Log message at DEBUG level");

Upvotes: 2

Related Questions