FoMS Experts

News / Free Mule Apps

FOMS APP 003

The logger who loved me

The logger who loved me
Share
FacebookGoogle plusLinkedInStumbleuponTwitter
C. Cavalcante / E. Schatz17.6.2020

As much as famous James Bond was Her Majesty's spy in the famous movie "The spy who loved me", logger can be a very good spy for you too. In our third application, we will show you how to use it and how to benefit from it. As in all previous apps, we will assume that you have red, resolved, understood and learned all prerequisites listed down below in this article, so let's start with the creation of our new app together. You can download this application here.

The prerequisites

In order to understand this app, you need to have the knowledge from the following articles:
- The Prerequsits for Free Friends of MuleSoft Apps
- FoMS App 001: Hello, variables!

Used elements

In this example app, we will be using the following components:
-HTTP Listener
-Set Variable
-Logger
-Set Payload

Step 01

Start Anypoint Studio, create a new Mule project and name it foms003-the-logger-who-loved-me.

Step 02

Add HTTP Listener and configure it with the following data:

Connector configuration > Base path: foms003
Path: the-logger-who-loved-me

Your FoMS application is now accessible behind the URL

http://localhost:8081/foms003/the-logger-who-loved-me

PHOTO: HTTP LISTENER CONFIGURATION

While you are already in the configuration of the HTTP Listener, go to the Responses category and replace the payload in the body with the following string: OK. In this Mule app, we will not care about the response that the app will send back to our Postman and that is why we use only a simple "OK" with no meaning in this case. We have removed payload variable from the response, in order to avoid possible misleading.

Step 03

By using Mule Palette, find and add a component called Logger. The Logger is a component that is used to write out content of a variable into the Console. It is used to quickly check what is going on with your variable on that particular spot in your app.

There are some other more advanced ways how to check your variables in the flow, but we will talk about it in some more advanced apps, that will be published with time.

In the configuration of the Logger, define Message as payload and do not forget to press fx to switch to expression mode.

PHOTO: LOGGER CONFIGURATION

Step 04

Now start your Mule application, open Postman and create a new HTTP request towards the URL:

http://localhost:8081/foms003/the-logger-who-loved-me

Write This is my cargo in the body of the Postman and then press Send. As a response, you should get the OK string, that you have defined in the HTTP Listener. Now switch back to Anypoint Studio, and check the Console to see what was happening there, while you were calling the app from the Postman.

PHOTO: LOGGER OUTPUT IN THE CONSOLE

As you can see on the picture above, the Logger has written out the content of the payload (body in the HTTP request) in the following way:

org.mule.runtime.core.internal.processor.LoggerMessageProcessor: This is my cargo

By default word wrap in the Console is not turned on, so you might have to use some horizontal scrolling, to find the output of the Logger, among all other things in the Console.

And that is it. We told you all you need to know about the Logger. If you at this point already fully understand how to use it, you can go to the next FoMS application and for those who would like to practice little bit more, we will continue to play with the flow through few more examples.

Step 05

Let us add another Ser Variable to the flow and let's configure it with the following data:

Name: city
Value: Cologne

Add one more Logger after the previous Set Variable, set its message as vars.city and press fx. This instance of Logger is going to show you in the Console what is the content of the variable city.

PHOTO: CURRENT STATE OF THE FLOW

Let's save our application and let's make another request from Postman to observe the result in the Console!

PHOTO: CONSOLE OUTPUT

Step 06

Let's now use one instance of a Set Payload component. As with any other component, you can find it in Mule Palette and drag & drop it into the flow. To fully understand this component, you must remember that the payload in Mule app is nothing else but a variable with the name payload that gets a value from the body of the HTTP request.

In some more advanced examples, you will find out that you can access (start, use...) a Mule app not only through HTTP request and then you will notice that any cargo that you will need to deal with, becomes a payload in the Mule app.

Our payload variable is mutable in the Mule app, so let's change its value to This is my overwritten cargo

After Set Payload, add another Logger and use it to write out the content of the payload in the Console.

PHOTO: CURRENT FLOW STATUS WITH CONSOLE OUTPUT AFTER THE CALL FROM POSTMAN

The conclusion

At the end of this tutorial, we think we could rename the title to The logger whom I loved, since it can be so useful, while developing Mule app. To be successful in any kind of development, you must first of all have a total control of what is the content of your variables during each moment of the application flow, so Logger is absolutely necessary, when things get complicated. Use it always and test what you have written, before you find yourself in spaghetti code logic.

As always, new apps are coming soon, so stay tuned!

RELATED ARTICLES

OTHER ARTICLES

Register now