Skip to content

Commit

Permalink
Merge pull request #19 from avioconsulting/feature/mapmessage
Browse files Browse the repository at this point in the history
Feature/mapmessage
  • Loading branch information
dgwouch authored Nov 24, 2021
2 parents 179922a + 4b5a4bc commit 4476b17
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 27 deletions.
55 changes: 50 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ jobs:
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD

- name: Set up Maven
uses: stCarolas/setup-maven@v4.2
with:
maven-version: 3.6.1

- name: Create settings xml
uses: whelk-io/maven-settings-xml-action@v20
with:
servers: >
[
{
"id": "avio-repository",
"username": "${{secrets.NEXUS_USER}}",
"password": "${{secrets.NEXUS_PASSWORD}}"
},
{
"id": "exchange-repository",
"username": "~~~Token~~~",
"password": "${anypoint-exchange-token}"
}
]
mirrors: >
[
{
"id": "avio-repository",
"mirrorOf": "mulesoft-ee-releases",
"url": "https://devops.avioconsulting.com/nexus/repository/mulesoft-ee-releases/"
}
]
- name: Cache for maven dependencies
uses: actions/cache@v2
with:
Expand All @@ -51,21 +81,36 @@ jobs:
if: always()
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
files: ./*/*/surefire-reports/*.xml
files: ./**/surefire-reports/*.xml

# publish anything on master (including -SNAPSHOT)
- name: Publish package
if: github.event_name != 'pull_request'
if: ${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/master'}}
run: mvn --batch-mode deploy -DskipTests -X
env:
MAVEN_USERNAME: ${{ secrets.GIT_USER }}
MAVEN_PASSWORD: ${{ secrets.GIT_TOKEN }}

- name: Get Exchange Token
id: get-token
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' && github.ref == 'refs/heads/master'}}
run: |
echo ::set-output name=token::$(curl --location --request POST 'https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token' --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'client_id=${{secrets.ANYPOINT_EXCHANGE_CLIENT_ID}}' --data-urlencode 'client_secret=${{secrets.ANYPOINT_EXCHANGE_CLIENT_SECRET}}' --data-urlencode 'grant_type=client_credentials')
- name: Create pom for Exchange
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' && github.ref == 'refs/heads/master'}}
run: sed "s/<groupId>com.avioconsulting.mule<\/groupId>/<groupId>f2ea2cb4-c600-4bb5-88e8-e952ff5591ee<\/groupId>/" pom.xml > anypoint-pom.xml

- name: Publish Exchange Artifact
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' && github.ref == 'refs/heads/master'}}
run: mvn -f anypoint-pom.xml deploy -P exchange -Danypoint-exchange-token=${{ steps.get-token.outputs.token }}

- name: Perform Release
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' }}
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' && github.ref == 'refs/heads/master'}}
run: echo "Releasing version ${{ steps.set-version.outputs.version }}"

- name: Perform Release
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' }}
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' && github.ref == 'refs/heads/master'}}
env:
GIT_USER: ${{ secrets.GIT_USER }}
GIT_PASSWORD: ${{ secrets.GIT_TOKEN }}
Expand All @@ -80,7 +125,7 @@ jobs:
git push --follow-tags
- name: Add Release Note and Create Github Release
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' }}
if: ${{ !contains(steps.set-version.outputs.version, 'SNAPSHOT') && github.event_name != 'pull_request' && env.GITHUB_REF_NAME == 'master'}}
uses: release-drafter/release-drafter@v5
with:
version: ${{ steps.set-version.outputs.version }}
Expand Down
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ One of the reasons for developing this custom module is to feed JSON logs to log
* Optionally log location info with each log message.
* Tracepoints compatibility to measure request, response times and such.

# Changes
## 1.2.1
* Added system property '_avio.logger.mapmessage = true_' to utilize a MapMessage instead of ObjectMessage for logging, this allows for filtered logs. See Simplified Logging section below.
* Supports 4.1 - 4.4 releases of the Mule Runtime

# Using the Custom Logger Processor
Here is how mule-custom-logger looks like in action.

Expand Down Expand Up @@ -140,3 +145,105 @@ Alternatively, you can push this mule custom component to your anypoint organiza
* Include exchange credentials in your settings.xml under servers section and with the matching server id with the repository id in pom's distribution management tag.
* Run ```mvn clean deploy``` to deploy this custom component into your anypoint exchange.
* Now, click on "search on exchange" in your mule project pallete, login and install component in your project.

# Simplified Logging
When developing and debugging code locally, the complete log message is rarely necessary, only a small subset.
To be able to filter messages, the logger code must use a MapMessage component, instead of an ObjectMessage. This MapMessage can then be filtered in a Log4j pattern.

* Create a ```src/main/resources/local/log4j2.xml``` file - This file will only be used when running locally
```xml
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<!-- the pattern can be modified to your need -->
<PatternLayout pattern="%5p [%d] %K{log}%n" />
</Console>
</Appenders>
<Loggers>
<AsyncRoot level="INFO">
<!-- don't forget to enable the appender -->
<AppenderRef ref="CONSOLE"/>
</AsyncRoot>
</Loggers>
</Configuration>
```
* Update pom.xml to include the following profiles - Using the ```local``` profile will copy the src/main/resources/local directory into the project output directory, overriding the original log4j2.xml
```xml
. . .
<profiles>
<profile>
<!-- using maven pass in -Plocal to use this profile -->
<id>local</id>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>local-log4j2</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<!-- copies the 'local' folder to outputDirectory -->
<directory>src/main/resources/local/</directory>
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<!-- Standard profile for normal builds -->
<id>other</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>local/**</exclude>
</excludes>
</resource>
<resource>
<!-- Helps IntelliJ understand src/main/mule is a source directory too -->
<directory>src/main/mule</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
. . .
```
* From Studio, Run -> Run Configurations
* Add VM Argument: ```-Davio.logger.mapmessage=true```
![VMArgument](https://user-images.githubusercontent.com/36522886/140087312-290dd3d0-5158-4a6f-b07d-ec18d9ff743e.png)
* Add Maven command line arguments: ```-Plocal```
![MavenCommand](https://user-images.githubusercontent.com/36522886/140087337-5bc6421c-640e-4c57-b836-d80cb8fa0c21.png)

Sample Log
```
INFO [2021-11-03 10:14:29,744] {trace_point=START, payload=OrderId : 548102842, correlation_id=b25b770a.ee7a.4820.8abc.9346ff693840, message=Start of flow}
INFO [2021-11-03 10:14:29,773] {trace_point=FLOW, payload={
"orderId": 548102842,
"customerId": "ARG-12934",
"items": [
"CP-123",
"CP-452"
]
}, correlation_id=correlationId, message=Here is my message}
INFO [2021-11-03 10:14:29,775] {trace_point=END, payload=OrderId : 548102842, correlation_id=b25b770a.ee7a.4820.8abc.9346ff693840, message=End of flow}
```
46 changes: 29 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,40 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.avioconsulting.mule</groupId>
<artifactId>mule-custom-logger</artifactId>
<version>1.2.0</version>
<version>1.2.1-SNAPSHOT</version>
<packaging>mule-extension</packaging>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github.com/avioconsulting/public-packages</url>
<layout>default</layout>
</repository>
</distributionManagement>
</profile>
<profile>
<id>exchange</id>
<distributionManagement>
<repository>
<id>anypoint-exchange</id>
<name>Exchange Repository</name>
<url>https://maven.anypoint.mulesoft.com/api/v2/organizations/${project.groupId}/maven</url>
<layout>default</layout>
</repository>
</distributionManagement>
</profile>
</profiles>
<parent>
<groupId>org.mule.extensions</groupId>
<artifactId>mule-modules-parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</parent>
<build>
<pluginManagement>
Expand All @@ -37,21 +64,6 @@
<version>2.13.2</version>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/avioconsulting/public-packages</url>
<layout>default</layout>
</repository>
<snapshotRepository>
<uniqueVersion>false</uniqueVersion>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/avioconsulting/public-packages</url>
<layout>default</layout>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>maven-central</id>
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/avio/customlogger/CustomLoggerOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.avio.customlogger.utils.CustomLoggerUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.MapMessage;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.ObjectMessage;
import org.mule.runtime.api.component.location.ComponentLocation;
import org.mule.runtime.extension.api.annotation.param.Config;
Expand Down Expand Up @@ -61,6 +63,7 @@ public void customLogger(@ParameterGroup(name = "Log") LogProperties logProperti
ParameterResolver<String> payload = logProperties.getPayload();
if (logger.isEnabled(level) && payload != null) {
logInner.put("payload", payload.resolve());

}

if (exceptionProperties != null) {
Expand All @@ -74,8 +77,17 @@ public void customLogger(@ParameterGroup(name = "Log") LogProperties logProperti
if (logLocationInfoProperty.logLocationInfo) {
logContext.put("location", getLocationInformation(location));
}
ObjectMessage objectMessage = new ObjectMessage(logContext);
logger.log(level, objectMessage);

/* 1.2.1 - Changed to MapMessage instead of ObjectMessage */
/* Check system property - avio.custom.logger.env */
/* this lets us differentiate to use simplified logging locally */
Message message;
if("true".equalsIgnoreCase(System.getProperty("avio.logger.mapmessage"))) {
message = new MapMessage(logContext);
} else {
message = new ObjectMessage(logContext);
}
logger.log(level, message);
}

private static Map<LoggerLevelProperty.LogLevel, Level> getMappings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.logging.log4j.message.ObjectMessage;
import org.mule.runtime.api.artifact.Registry;
import org.mule.runtime.api.component.location.ComponentLocation;
import org.mule.runtime.extension.api.annotation.param.MediaType;
import org.mule.runtime.extension.api.annotation.param.Optional;
import org.mule.runtime.extension.api.annotation.param.ParameterGroup;
import org.mule.runtime.extension.api.runtime.process.CompletionCallback;
Expand All @@ -18,9 +19,7 @@
import java.util.HashMap;
import java.util.Map;

import static com.avio.customlogger.utils.CustomLoggerConstants.*;
import static com.avio.customlogger.utils.CustomLoggerConstants.DEFAULT_CATEGORY_PREFIX;

import static org.mule.runtime.extension.api.annotation.param.MediaType.ANY;

public class CustomLoggerTimerScopeOperations {
public static final String DEFAULT_CATEGORY_SUFFIX = ".timer";
Expand All @@ -32,6 +31,7 @@ public class CustomLoggerTimerScopeOperations {
private Logger logger;
private CustomLoggerUtils customLoggerUtils;

@MediaType(value = ANY, strict = false)
public void timerScope(String timerName,
@Optional(defaultValue = DEFAULT_CATEGORY_SUFFIX) String categorySuffix,
@Optional String moduleConfigurationName,
Expand Down

0 comments on commit 4476b17

Please sign in to comment.