Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSon Logging Extension for Maven 3 and Maven 4 #5500

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ config/**/*.gen.json
*-installer*

# Fabric8 CRDs
java/*/target
java/**/target
pkg/resources/resources.go

# MAC OS files
Expand Down
127 changes: 127 additions & 0 deletions java/maven-logging/maven-logging-mvn3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-maven-logging-mvn3</artifactId>
<version>2.4.0-SNAPSHOT</version>
<description>Set of dependencies used for structural logging of Maven output</description>
<packaging>jar</packaging>

<properties>
<logstash-logback-encoder.version>6.6</logstash-logback-encoder.version>
<maven-dependency-plugin-version>3.5.0</maven-dependency-plugin-version>
<slf4j.version>1.7.36</slf4j.version>
<logback.version>1.2.13</logback.version>
<jackson.version>2.15.2</jackson.version>
<maven.version>3.9.6</maven.version>
</properties>

<build>
<defaultGoal>generate-resources</defaultGoal>
<plugins>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<version>0.3.5</version>
<executions>
<execution>
<id>generate-index</id>
<goals>
<goal>main-index</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<!-- structural logging: we only need this dependencies to be part of the overlay package, reason why are marked as provided -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>${logstash-logback-encoder.version}</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>

<!-- dependency for JSR 330 annotation -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope> <!-- always provided by the Maven Core Classloader -->
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.apache.maven.extensions.jsonlog;

import ch.qos.logback.classic.util.ContextInitializer;
import org.apache.maven.cli.logging.Slf4jLoggerManager;
import org.apache.maven.eventspy.AbstractEventSpy;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.codehaus.plexus.MutablePlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.logging.LoggerManager;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.lang.reflect.Field;

@Named
@Singleton
public class JsonLog extends AbstractEventSpy {

@Inject
JsonLog(RuntimeInformation runtimeInformation, PlexusContainer container) throws Exception {
try {
// Maven 3 extension code
if (runtimeInformation.getMavenVersion().startsWith("3.")) {
Class<?> binderClass = org.slf4j.LoggerFactory.class.getClassLoader().loadClass(
org.slf4j.impl.StaticLoggerBinder.class.getName());
System.err.println(binderClass.getClassLoader().getResource(binderClass.getName().replace('.','/') + ".class"));
Object binder = binderClass.getMethod("getSingleton").invoke(null);

Field field = binder.getClass().getDeclaredField("loggerFactory");
field.setAccessible(true);
ch.qos.logback.classic.LoggerContext context = new ch.qos.logback.classic.LoggerContext();
context.start();
new ContextInitializer(context).autoConfig();
field.set(binder, context);

if (container instanceof MutablePlexusContainer) {
((MutablePlexusContainer) container).setLoggerManager(new Slf4jLoggerManager());
}

LoggerFactory.getLogger(getClass()).debug("Json Logging initialized");
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}

}
145 changes: 145 additions & 0 deletions java/maven-logging/maven-logging-mvn4/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-maven-logging-mvn4</artifactId>
<version>2.4.0-SNAPSHOT</version>
<description>Set of dependencies used for structural logging of Maven output</description>
<packaging>jar</packaging>

<properties>
<logstash-logback-encoder.version>7.4</logstash-logback-encoder.version>
<maven-dependency-plugin-version>3.5.0</maven-dependency-plugin-version>
<slf4j.version>2.0.7</slf4j.version>
<logback.version>1.3.12</logback.version>
<jackson.version>2.15.2</jackson.version>
<maven.version>4.0.0-alpha-12</maven.version>
</properties>

<build>
<defaultGoal>generate-resources</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin-version}</version>
<executions>
<execution>
<id>default-cli</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeTransitive>true</excludeTransitive>
<outputDirectory>${project.build.directory}/maven-overlay</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<version>0.3.5</version>
<executions>
<execution>
<id>generate-index</id>
<goals>
<goal>main-index</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<!-- structural logging: we only need this dependencies to be part of the overlay package, reason why are marked as provided -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>${logstash-logback-encoder.version}</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>

<!-- dependency for JSR 330 annotation -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope> <!-- always provided by the Maven Core Classloader -->
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.apache.maven.extensions.jsonlog;

import org.apache.maven.cli.logging.Slf4jLoggerManager;
import org.apache.maven.eventspy.AbstractEventSpy;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.codehaus.plexus.MutablePlexusContainer;
import org.codehaus.plexus.PlexusContainer;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.lang.reflect.Field;

@Named
@Singleton
public class JsonLog extends AbstractEventSpy {

@Inject
JsonLog(RuntimeInformation runtimeInformation, PlexusContainer container) throws Exception {
try {
// Maven 4 extension code:
if (runtimeInformation.getMavenVersion().startsWith("4.")) {
org.slf4j.spi.SLF4JServiceProvider provider = new ch.qos.logback.classic.spi.LogbackServiceProvider();
provider.initialize();
Field field = LoggerFactory.class.getDeclaredField("PROVIDER");
field.setAccessible(true);
field.set(null, provider);

if (container instanceof MutablePlexusContainer) {
((MutablePlexusContainer) container).setLoggerManager(new Slf4jLoggerManager());
}

LoggerFactory.getLogger(getClass()).debug("Json Logging initialized");
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}

}
Loading
Loading