Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Simpl-Open-Monitoring-ELK-Developers.md 2.82 KiB
Newer Older
Natalia Szakiel's avatar
Natalia Szakiel committed

### Log4J wrapper 

Repository: https://code.europa.eu/simpl/simpl-open/development/contract-billing/common_logging

1. Import in the project

To import into project add maven dependency:
```
    <properties>
        <simpl.common.logging.version>1.0.0-SNAPSHOT.39.1a139b97</simpl.common.logging.version>
    </properties>

        <dependency>
            <groupId>eu.simpl</groupId>
            <artifactId>SIMPL_COMMON_LOGGING</artifactId>
            <version>${simpl.common.logging.version}</version>
        </dependency>

    and repository

    <repositories>
        <repository>
            <id>gitlab-maven</id>
            <url>${CI_API_V4_URL}/projects/897/packages/maven</url>
        </repository>
    </repositories>
```


2. Normal logging:

LOG.info("message");

```
{
    "timestamp": "2024-08-20T06:20:12.201Z",
    "level": "INFO",
    "message": "Application started",
    "thread": "main",
    "logger": "eu.simpl.simpl_billing.SimplBillingApplication"
}
```


3. Log Http Message
import static eu.simpl.MessageBuilder.buildMessage;
 
```
LOG.info(buildMessage(HttpLogMessage.builder()
.msg("HTTP request")
.httpStatus("200")
.httpRequestSize("100")
.httpExecutionTime("100")
.user("user")
.build()));
```

Result:

```
{
    "timestamp": "2024-08-19T10:32:54.801Z",
    "level": "INFO",
    "message":
    {
        "msg": "HTTP request",
        "httpStatus": "200",
        "httpRequestSize": "100",
        "user": "user",
        "httpExecutionTime": "100"
    },
    "thread": "main",
    "logger": "eu.simpl.simpl_billing.SimplBillingApplication"
}
```

4. Custom log level BUSINESS

buildMessage method can not be used with slf4, normal logging yes, can be used with sl4j.

example usage:

```
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import static eu.simpl.MessageBuilder.buildMessage;
 
private static final Logger LOG = LogManager.getLogger(ClassToBeChanged.class);
 
LOG.log(Level.getLevel("BUSINESS"), buildMessage(LogMessage.builder()
.origin("origin_name")
.destination("destination_name")
.businessOperations(List.of("operation1", "operation2"))
.messageType(MessageType.RESPONSE)
.correlationId("correlation_id")
.httpStatus("200")
.user("user_name")
.msg("Example log message")
.build()));
```

Result:
```
{
    "timestamp": "2024-08-12T12:43:18.437+0200",
    "level": "BUSINESS",
    "message":
    {
        "msg": "Network",
        "messageType": "RESPONSE",
        "businessOperations": "[operation1, operation2]",
        "origin": "origin_name",
        "httpStatus": "200",
        "destination": "destination_name",
        "correlationId": "correlation_id",
        "user": "user_name"
    },
    "thread": "main",
    "logger": "eu.simple.simpl_billing.SimplBillingApplication",
    "httpRequestSize": "null",
    "httpExecutionTime": "null"
}
```