How to get logger from within a MVEL script in Nuxeo Studio?
Today we have a question from regular Nuxeo Answers user Antoine. He asks how to get logger from within a MVEL script in Nuxeo Studio.
If you are not familiar with MVEL, it’s the script language that we use in Studio. It gives a lot of flexibility when writing operation chains.
When writing those scripts, you have access to certain variables:
- CurrentDate: wrapper for a Calendar instance
- Context: the operation context
- This: the operation input
- Session: the CoreSession
- CurrentUser: Principal wrapper around the user running the chain
- currentUser: CurrentUser alias
- Env: the Framework properties
- Fn: wrapper to the CoreFunctions class
- Document only if the input is a document, wrap the input in a DocumentWrapper
- CurrentDocument: alias to Document
- Documents: only if the input is a list of documents if, wrap them in a DocumentWrapper.
- WorkflowVariables: Available only in a workflow context
- NodeVariables: available only in a workflow context
But in all those objects available in the context, there is nothing that gives you the logger. So you have to instantiate it yourself like this:
org.apache.commons.logging.LogFactory.getLog("RunScript").debug("Something happened.");
And if you have some long running script you can do it like this:
logger = org.apache.commons.logging.LogFactory.getLog("RunScript");
logger.debug("Something happened.");
....
logger.debug("Something else happened.");
...
This works because you have access to most of the objects available in Nuxeo’s classpath.
Just make sure log4j is configured according to the name of your logger. Here I’ve chosen >RunScript so I have to add the following line to my _$NUXEO_HOME/lib/log4j.xml_ file.
<category name="RunScript">
<priority value="DEBUG" />
</category>