Skip to content

Commit

Permalink
add localfile appender and pattern layout (apache#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
woyumen4597 authored and zheniantoushipashi committed Jul 11, 2020
1 parent 723f441 commit 1fa9242
Show file tree
Hide file tree
Showing 42 changed files with 254 additions and 40 deletions.
2 changes: 1 addition & 1 deletion assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion common/kvstore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion common/network-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion common/network-shuffle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion common/network-yarn/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion common/sketch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion common/tags/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion common/unsafe/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
158 changes: 158 additions & 0 deletions core/src/main/java/org/apache/spark/log4j/LocalFileAppender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Licensed 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.
*/
package org.apache.spark.log4j;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.helpers.LogLog;
import org.apache.spark.SparkEnv;

import com.google.common.annotations.VisibleForTesting;

public class LocalFileAppender extends FileAppender {
private String metadataIdentifier;
private String category;
private String identifier;
private String jobName;
private String project;
private String executorId;
private String mountDir;

private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

public LocalFileAppender() {
super();
}

public LocalFileAppender(Layout layout, String filename, boolean append) throws IOException {
super(layout, filename, append);
}

public LocalFileAppender(Layout layout, String filename) throws IOException {
super(layout, filename);
}

@Override
public void activateOptions() {
LogLog.warn(String.format("%s starting ...", name));
this.fileName = getOutPutDir();
LogLog.warn(String.format("Output path is %s", this.fileName));
LogLog.warn("metadataIdentifier -> " + getMetadataIdentifier());
LogLog.warn("category -> " + getCategory());
LogLog.warn("identifier -> " + getIdentifier());
LogLog.warn("mountDir -> " + getMountDir());

if (null != getProject()) {
LogLog.warn("project -> " + getProject());
}

if (null != getJobName()) {
LogLog.warn("jobName -> " + getJobName());
}
super.activateOptions();
}

@VisibleForTesting
String getOutPutDir() {
String rollingDir = dateFormat.format(new Date());
if (StringUtils.isBlank(executorId)) {
executorId = SparkEnv.get() != null ? SparkEnv.get().executorId() : UUID.randomUUID().toString();
LogLog.warn("executorId set to " + executorId);
}

if ("job".equals(getCategory())) {
return getRootPathName() + "/" + rollingDir +
"/" + getIdentifier() + "/" + getJobName() + "/" + "executor-"
+ executorId + ".log";
}
return getRootPathName() + "/" + rollingDir
+ "/" + getIdentifier() + "/" + "executor-" + executorId + ".log";
}

String getRootPathName() {
if (!mountDir.endsWith("/")) {
mountDir = mountDir + "/";
}
if ("job".equals(getCategory())) {
return mountDir + getProject() + "/spark_logs";
} else if ("sparder".equals(getCategory())) {
return mountDir + "_sparder_logs";
} else {
throw new IllegalArgumentException("illegal category: " + getCategory());
}
}


public String getMetadataIdentifier() {
return metadataIdentifier;
}

public void setMetadataIdentifier(String metadataIdentifier) {
this.metadataIdentifier = metadataIdentifier;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getIdentifier() {
return identifier;
}

public void setIdentifier(String identifier) {
this.identifier = identifier;
}

public String getJobName() {
return jobName;
}

public void setJobName(String jobName) {
this.jobName = jobName;
}

public String getProject() {
return project;
}

public void setProject(String project) {
this.project = project;
}

public String getExecutorId() {
return executorId;
}

public void setExecutorId(String executorId) {
this.executorId = executorId;
}

public String getMountDir() {
return mountDir;
}

public void setMountDir(String mountDir) {
this.mountDir = mountDir;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed 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.
*/

package org.apache.spark.log4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

public class SensitivePatternLayout extends PatternLayout {
private static final String PREFIX_GROUP_NAME = "prefix";
private static final String SENSITIVE_GROUP_NAME = "sensitive";
private static final String MASK = "******";
private static final Pattern SENSITIVE_PATTERN = Pattern.compile(
String.format("(?<%s>password\\s*[:=])(?<%s>[^,.!]*)", PREFIX_GROUP_NAME, SENSITIVE_GROUP_NAME),
Pattern.CASE_INSENSITIVE);

@Override
public String format(LoggingEvent event) {
if (event.getMessage() instanceof String) {
String maskedMessage = mask(event.getRenderedMessage());

Throwable throwable = event.getThrowableInformation() != null
? event.getThrowableInformation().getThrowable()
: null;
LoggingEvent maskedEvent = new LoggingEvent(event.fqnOfCategoryClass,
Logger.getLogger(event.getLoggerName()), event.timeStamp, event.getLevel(), maskedMessage,
throwable);

return super.format(maskedEvent);
}
return super.format(event);
}

private String mask(String message) {
Matcher matcher = SENSITIVE_PATTERN.matcher(message);
if (matcher.find()) {
return matcher.replaceAll(String.format("${%s}%s", PREFIX_GROUP_NAME, MASK));
}
return message;
}
}
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/avro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/docker-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/flume-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/flume-sink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/flume/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/kafka-0-10-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/kafka-0-10-sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/kafka-0-10/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/kafka-0-8-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion external/kafka-0-8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.spark</groupId>
<artifactId>spark-parent_2.11</artifactId>
<version>2.4.1-kylin-r34</version>
<version>2.4.1-kylin-r35</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Loading

0 comments on commit 1fa9242

Please sign in to comment.