9

I want to log from withing Javascript script mediator for debugging porpouses. It is quite annoying to have to add the message/variable value... to a context variable and use a log mediator in the sequence.

Is there a way to avoid this?

King Regards.

Community
  • 1
  • 1
fipries
  • 386
  • 3
  • 19

2 Answers2

24

You just need to get ServiceLog inside the script mediator and use it as follows:

<script language="js">
    var log = mc.getServiceLog();        
    log.info("Logging inside Script Mediator");
</script>
Daniel Silva
  • 294
  • 2
  • 6
3

You can use print(message/variable);. It will display the message/variable in the terminal.

  • Can You please tell me about the terminal? What is the terminal? Do you mean wso2carbon.log file? Where exactly I should see it? – Reza Ameri Jul 21 '14 at 09:59
  • Yes, you will see it in the logs. Here terminal means when you run the server in the foreground (i.e just running ./wso2server.sh) you will see the server logs there. – Sumedha Kodithuwakku Jul 22 '14 at 11:31
  • 1
    I did it but it does not happen! I added javascript script from conf registry to my script mediator. the code is as follows: `function hash(mc){ var hashedInput = mc.getProperty("toMD5").toString(); print(hashedInput); mc.setProperty("toMD5",md5(hashedInput)); } function md5(x){//some code here.}` and it does not log in the wso2carbon.log which is the file that is populated after running `./wso2server.sh` – Reza Ameri Jul 23 '14 at 07:15
  • There can be differences in the two outputs(i.e Console out and Log file output). You can see the configurations in log4j.properties. So that might be the reason why you are not seeing the printed messages in the log file. – Sumedha Kodithuwakku Jul 24 '14 at 07:36
  • 1
    That also works, but the answer with log.info is better since it will output the text using the default formatter, with date, time and everything else that is configured. – Ílson Bolzan Jun 04 '18 at 16:18