forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add localfile appender and pattern layout (apache#134)
- Loading branch information
1 parent
10cc3e1
commit 5770a68
Showing
42 changed files
with
254 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
core/src/main/java/org/apache/spark/log4j/LocalFileAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
core/src/main/java/org/apache/spark/log4j/SensitivePatternLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.