How to set log4j2 log levels and categories in DataWeave 2.x? - mule

When using the log() function in DataWeave I have a few questions:
How can I set a log level and log category so my logs are handled by log4j2 the same way as log messages from the Logger components in the Mule flow?
How can I suppress logging the expression? If the expression result is very large (what if it is streaming data?) I might only want to log to first argument to log, and skip the actual DW expression evaluation.

There is no way to set the logging level with the log() function in DataWeave. As an alternative you could implement a custom function in a custom module in to log that allows to set levels.
You could use the same custom function to implement some logic, however there is the generic problem of determining if a payload is big without fully consuming it. In any case DataWeave logging is meant to be used as a debugging tool and should not be used in production or for big payloads. The best practice is to avoid logging at all unless you need to debug an issue, and then remove the logging.

Related

Mule flow terminate

I have a small question. Do we have any option to stop/terminate the flow where ever we want in Mulesoft 4? Example: After executing transform message or after logger processor want to stop/terminate the flow based on our business requirement.
Two different ways to achieve that are described in the KB article https://help.mulesoft.com/s/article/How-To-Stop-Or-Start-Flows-In-Mule-4-x-Programmatically
Basically you need to get an instance of the Mule registry, the lookup the flow and stop or start it.
Yes, you can make use of groovy script or java class to start/stop mulesoft at runtime. Drag and drop the scripting component from the palette and choose groovy as your engine and use the script below.
flowName = registry.lookupByName('flowName').get();
if (flowName.isStarted())
flowName.stop()
else
flowName.start()

Does Apiglity provide a buil-in error handling and logging?

I'm working on an API project and have to decide over the error handling and logging strategy/concept. So as first step I want to check, whether Apigility provides its own logging functionality.
If I see it correctly, Apigility only provides a minimal error handling for REST specific errors (Apigility documentation -> Error Reporting). That's it. So, only a limited error handling and no logging. Right? But maybe it's wrong and I have just not found the functionality I need. So, is an error handling mechanism provided? Is a logging mechanism provided in Apigility?
It handles error rendering if you use ApiProblem objects to encapsulate your errors.
It doesn't handle logging out of the box, you'll have to add your application's logging strategy yourself.

Is it possible to programatically add an interceptor before a VM endpoint from within a Mule FunctionalTestCase? If so, how?

I have a flow with two VM endpoints both configured with the exchange pattern of request/response. I want to evaluate the message at the end of the flow when it reaches the seecond VM endpoint, before the next flow takes off with the message. I thought I might be able to do this with an interceptor inserted before the VM endpoint. Is this possible from within a Mule FunctionalTestCase? Is it possible to programatically add an interceptor to a flow at all..?
Personally, I think that the flows should not really be altered during the testings. In that case you would have another (although just slightly different) version running when you deploy it to a server.
Instead, I would argue that you divide your flows into testable parts and put the endpoint addresses into separate configuration. That way you can test each vm-based flow separated from each other and verify the behaviour using mock flows or similar.
vm://in-flow1 -> process -> vm://mock
vm://mock -> verify payload -> vm://in-flow2
In the "real" configuration, you change "mock" to something pointing to the second vm flow.
You can also elaborate on mocking the first or second VM flows entirely from each other to create distinct unit tests.
However, if you really want to go down the "modify code for testing purposes" rabbit hole, you can likely use some aspect oriented black magic to achieve that.
Look at this blogpost how it's done in mule.
You could try with Munit, and run an spy around the flow (it should work). So you can run assertions after the flow execution
https://github.com/mulesoft/munit

Capturing output messages during flyway migration

I would like to capture the messages logged by SQLServerDbSupport and DBMigrate during a migration. Calling flyway.migrate does the migration, but it is not always obvious what actions were applied. I am hoping to capture this to determine what changes, if any were applied.
I already tried setting STDOUT to a ByteArrayOutputStream but that didn't work, presumably as the logger is initialised before the re-direction.
What other options are there to obtain the output messages ?
All you have to do is configure whatever logging framework you use to achieve this. No need to reassign stdout.
While this suggestion is great I am not sure how it addresses the need to capture the output from one migration only, while another migration is running. Do you have an example of a logger configuration which deal with individual migration in a concurrent scenario /

Is it safe to rely on session variables to store information to be shared between multiple asynchronous flows?

I have a couple of flows that rely on session variables that are generated in one flow and then passed to the other. Is it safe to rely on session variables used by two asynchronous flows? I guess I don't fully understand the scope of 'sessionVars' in a mule application or in a given mule message.
The mule session has nothing to do with the Java EE session that is shared across threads. The mule session is part of the MuleMessage and how they works is explained here, therefor if you want to share something across multiple flows processing the same message that is the way to go.
If instead you are looking into a way to store a value from a flow processing the message A and pick that value from a flow processing the message B you should consider store this value into the objectstore
I am pretty sure that session variables are returned to http endpoints that are request-response. This could expose sensitive data. I am trying to locate the original mention and official mitigation strategy but have yet to locate it again.
But an easy solution is to remove them at return point of a flow
Edit:
Found the thing I was looking for...
`
<http:connector name="NoSessionConnector">
<service-overrides sessionHandler="org.mule.session.NullSessionHandler"/>
</http:connector>
`
found here under 'HTTP Response Header'
http://www.mulesoft.org/documentation/display/current/HTTP+Transport+Reference
Or, you can also create a custom SessionHandler