fix(TBD-9884) : Missing log42 conf on Yarn side (#3021)

This commit is contained in:
Laurent BOURGEOIS
2020-02-11 12:11:29 +01:00
committed by GitHub
parent 08815a1e04
commit 9f4f8c2692
11 changed files with 178 additions and 7 deletions

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@ dist/
.settings/
*.iml
.idea
lib

View File

@@ -18,6 +18,7 @@ import java.util.Set;
import org.apache.log4j.Level;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
@@ -241,4 +242,6 @@ public interface IRunProcessService extends IService {
public boolean isSelectLog4j2();
public IFolder getJavaProjectExternalResourcesFolder(IProcess process);
}

View File

@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="class"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry exported="true" kind="lib" path="lib/log4j-api-2.12.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/log4j-core-2.12.1.jar"/>
<classpathentry kind="src" path="src/main/java/"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -35,8 +35,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>com.atlassw.tools.eclipse.checkstyle.CheckstyleNature</nature>

View File

@@ -55,3 +55,6 @@ Export-Package: org.talend.core,
org.talend.core.views,
org.talend.designer.runprocess
Import-Package: org.apache.commons.collections4.map
Bundle-ClassPath: lib/log4j-api-2.12.1.jar,
lib/log4j-core-2.12.1.jar,
.

View File

@@ -1,9 +1,10 @@
source.. = src/main/java/
output.. = class/
output.. = target/classes/
bin.includes = META-INF/,\
.,\
plugin.xml,\
icons/,\
plugin.properties,\
metadata.properties,\
schema/
schema/,\
lib/

View File

@@ -9,6 +9,13 @@
</parent>
<artifactId>org.talend.core</artifactId>
<packaging>eclipse-plugin</packaging>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@@ -22,6 +29,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/lib</outputDirectory>
<includeGroupIds>
org.apache.logging
</includeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -13,6 +13,9 @@
package org.talend.designer.runprocess;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@@ -25,10 +28,20 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
@@ -2640,6 +2653,15 @@ public class ProcessorUtilities {
}
return null;
}
public static String getJavaProjectExternalResourcesFolderPath(IProcess process) {
if (GlobalServiceRegister.getDefault().isServiceRegistered(IRunProcessService.class)) {
IRunProcessService processService =
(IRunProcessService) GlobalServiceRegister.getDefault().getService(IRunProcessService.class);
return processService.getJavaProjectExternalResourcesFolder(process).getLocation().toPortableString();
}
return null;
}
public static boolean isExportAsOSGI() {
return exportAsOSGI;
@@ -2775,4 +2797,55 @@ public class ProcessorUtilities {
public static boolean isNeedProjectProcessId(String componentName) {
return "tRunJob".equalsIgnoreCase(componentName) || "cTalendJob".equalsIgnoreCase(componentName);
}
/**
* Generate a log4j2 config and write it into file
*
* @param configFile target file where config will be written
* @param rootLevel root logger level to be used
*/
public static void writeLog4j2ConfToFile(File configFile, Level rootLevel) throws IOException {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
FileOutputStream configOs = new FileOutputStream(configFile);
final AppenderComponentBuilder appenderBuilder = builder.newAppender("Console", "Console").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "[%-5level] %d{HH:mm:ss} %logger{36}- %msg%n"));
final RootLoggerComponentBuilder rootLoggerBuilder = builder.newRootLogger(rootLevel).add(builder.newAppenderRef("Console"));
builder.add(appenderBuilder);
builder.add(rootLoggerBuilder);
builder.writeXmlConfiguration(configOs);
}
public static void addFileToJar(String configFile, String jarFile) throws FileNotFoundException, IOException {
JarOutputStream jarOs = new JarOutputStream(new FileOutputStream(jarFile));
FileInputStream configIs = new FileInputStream(configFile);
JarEntry jarEntry = new JarEntry("log4j2.xml");
jarOs.putNextEntry(jarEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = configIs.read(bytes)) >= 0) {
jarOs.write(bytes, 0, length);
}
configIs.close();
jarOs.closeEntry();
jarOs.close();
}
/**
* Generate a jar containing a log4j2 config file
*
* @param process job for which to generate jar
* @param rootLevel root logger level
* @return path of the generated jar
* @throws IOException
*/
public static String buildLog4jConfigJar(IProcess process, String rootLevel) throws IOException {
String externalResourcesFolderPath = getJavaProjectExternalResourcesFolderPath(process);
String configFilePath = externalResourcesFolderPath + "/log4j2.xml";
String jarFilePath = externalResourcesFolderPath + "/talend-studio-log4j2.xml.jar";
// Create config file
ProcessorUtilities.writeLog4j2ConfToFile(new File(configFilePath), Level.getLevel(rootLevel));
// Put config file into jar
ProcessorUtilities.addFileToJar(configFilePath, jarFilePath);
return jarFilePath;
}
}

View File

@@ -2,6 +2,10 @@
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="src" path="src/">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -20,8 +20,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>

View File

@@ -20,6 +20,13 @@ import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -27,7 +34,11 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.stream.Collectors;
import org.apache.logging.log4j.Level;
import org.eclipse.core.runtime.Path;
import org.junit.Before;
import org.junit.Test;
@@ -59,6 +70,8 @@ import org.talend.designer.core.model.utils.emf.talendfile.ProcessType;
import org.talend.designer.core.model.utils.emf.talendfile.TalendFileFactory;
import org.talend.repository.ProjectManager;
import junit.framework.Assert;
/**
* DOC ggu class global comment. Detailled comment
*/
@@ -530,5 +543,38 @@ public class ProcessorUtilitiesTest {
jobIdParam.setValue(projectLabel + ProcessUtils.PROJECT_ID_SEPARATOR + subjobId);
node.getElementParameter().add(jobIdParam);
}
@Test
public void testWriteLog4j2ConfToFile() throws IOException {
String configFilePath = System.getProperty("java.io.tmpdir") + "/log4j2.xml";
ProcessorUtilities.writeLog4j2ConfToFile(new File(configFilePath), Level.getLevel("INFO"));
String expectedContent = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<Configuration>\n" +
" <Appenders>\n" +
" <Console name=\"Console\" target=\"SYSTEM_OUT\">\n" +
" <PatternLayout pattern=\"[%-5level] %d{HH:mm:ss} %logger{36}- %msg%n\"/>\n" +
" </Console>\n" +
" </Appenders>\n" +
" <Loggers>\n" +
" <Root level=\"INFO\">\n" +
" <AppenderRef ref=\"Console\"/>\n" +
" </Root>\n" +
" </Loggers>\n" +
"</Configuration>";
String actualContent = Files.lines(Paths.get(configFilePath), StandardCharsets.UTF_8).collect(Collectors.joining("\n"));
assertEquals(expectedContent, actualContent);
}
@Test
public void testAddFileToJar() throws FileNotFoundException, IOException {
String configFilePath = System.getProperty("java.io.tmpdir") + "/log4j2.xml";
String jarFilePath = System.getProperty("java.io.tmpdir") + "/log4j2.xml.jar";
ProcessorUtilities.addFileToJar(configFilePath, jarFilePath);
JarInputStream jIs = new JarInputStream(new FileInputStream(jarFilePath));
JarEntry entry = jIs.getNextJarEntry();
jIs.closeEntry();
jIs.close();
assertEquals("log4j2.xml", entry.getName());
}
}