web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
  #29  
Old 01-23-2014, 08:47 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,124
narupley is on a distinguished road
Default Automatically overwrite the log4j categories used in user scripts

Quote:
UPDATE: I've created a public GitHub repository to track these example channels, code templates, scripts, or whatever else!

https://github.com/nextgenhealthcare/connect-examples

To start with I only added the ones I wrote, because I didn't want to presume and add code from others without their explicit permission. Pull requests welcome!
overwriteCategories: Overwrites the categories of Logger objects placed in JavaScript contexts.

When you call "logger.info('something');" in a user-defined script, you'll see something like this in the server log:

Code:
[2014-01-23 08:36:32,862]  INFO  (transformer:?): something
In a recent thread, someone was asking whether it's possible to change the category ("transformer") that shows up to something custom, without having to edit log4j.properties and restart the server. The answer is yes, it's definitely possible, and this code template can do that for you.

All you have to do is import the code template, redeploy your channels, and it'll start working, except for transformer scripts. Because of how we handle the filter/transformer, in order to get this to work in a transformer script you'll just have to add this line at the top of the first step in your transformer:

Code:
overwriteCategories();
Note that the above is only necessary for the regular transformer, not for response transformers, filters, deploy/shutdown/preprocessor/postprocessor scripts, Database/JavaScript connectors, or attachment/batch scripts.

The Code:

Code:
/* 
 * NOTE: This will work automatically for all contexts except for
 * the transformer. For transformers, you will need to manually
 * include "overwriteCategories();" at the beginning of your first step.
 */

function overwriteCategories() {
	logger = org.apache.log4j.Logger.getLogger({
		'transformer':		'custom-transformer',
		'preprocessor':		'custom-preprocessor',
		'postprocessor':	'custom-postprocessor',
		'deploy':		'custom-deploy',
		'shutdown':		'custom-shutdown',
		'filter':		'custom-filter',
		'db-connector':		'custom-db-connector',
		'js-connector':		'custom-js-connector',
		'attachment':		'custom-attachment',
		'batch':		'custom-batch',
		'response':		'custom-response'
	}[logger.getName()]);
	logger.setLevel(org.apache.log4j.Level.DEBUG);
}

overwriteCategories();
Obviously, you will want to switch out the "custom-*" strings for your own custom categories.
Attached Files
File Type: xml overwriteCategories.xml (1.3 KB, 79 views)
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.

Last edited by narupley; 06-08-2018 at 11:36 AM.
Reply With Quote