-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…nyu. In container-log4j.properties, log4j.appender.{APPENDER}.MaxFileSize is set to ${yarn.app.container.log.filesize}, but yarn.app.container.log.filesize is 0 in default. So log is missing. This log is always rolling and only show the latest log.
- Loading branch information
1 parent
5f47f09
commit b8815fe
Showing
6 changed files
with
286 additions
and
24 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
128 changes: 128 additions & 0 deletions
128
...op-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/ContainerLogAppender.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,128 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
package org.apache.hadoop.yarn; | ||
|
||
import java.io.File; | ||
import java.io.Flushable; | ||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
|
||
import org.apache.hadoop.classification.InterfaceAudience.Public; | ||
import org.apache.hadoop.classification.InterfaceStability.Unstable; | ||
import org.apache.log4j.FileAppender; | ||
import org.apache.log4j.spi.LoggingEvent; | ||
|
||
/** | ||
* A simple log4j-appender for container's logs. | ||
*/ | ||
@Public | ||
@Unstable | ||
public class ContainerLogAppender extends FileAppender | ||
implements Flushable { | ||
|
||
private String containerLogDir; | ||
private String containerLogFile; | ||
private int maxEvents; | ||
private Deque<LoggingEvent> eventBuffer; | ||
private boolean closed = false; | ||
|
||
@Override | ||
public synchronized void activateOptions() { | ||
if (maxEvents > 0) { | ||
this.eventBuffer = new ArrayDeque<>(); | ||
} | ||
setFile(new File(this.containerLogDir, containerLogFile).toString()); | ||
setAppend(true); | ||
super.activateOptions(); | ||
} | ||
|
||
@Override | ||
public synchronized void append(LoggingEvent event) { | ||
if (closed) { | ||
return; | ||
} | ||
if (eventBuffer != null) { | ||
if (eventBuffer.size() == maxEvents) { | ||
eventBuffer.removeFirst(); | ||
} | ||
eventBuffer.addLast(event); | ||
} else { | ||
super.append(event); | ||
} | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
if (qw != null) { | ||
qw.flush(); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void close() { | ||
if (!closed) { | ||
closed = true; | ||
if (eventBuffer != null) { | ||
for (LoggingEvent event : eventBuffer) { | ||
super.append(event); | ||
} | ||
// let garbage collection do its work | ||
eventBuffer = null; | ||
} | ||
super.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Getter/Setter methods for log4j. | ||
* | ||
* @return containerLogDir. | ||
*/ | ||
public String getContainerLogDir() { | ||
return this.containerLogDir; | ||
} | ||
|
||
public void setContainerLogDir(String containerLogDir) { | ||
this.containerLogDir = containerLogDir; | ||
} | ||
|
||
public String getContainerLogFile() { | ||
return containerLogFile; | ||
} | ||
|
||
public void setContainerLogFile(String containerLogFile) { | ||
this.containerLogFile = containerLogFile; | ||
} | ||
|
||
private static final long EVENT_SIZE = 100; | ||
|
||
public long getTotalLogFileSize() { | ||
return maxEvents * EVENT_SIZE; | ||
} | ||
|
||
/** | ||
* Setter so that log4j can configure it from the | ||
* configuration(log4j.properties). | ||
* | ||
* @param logSize log size. | ||
*/ | ||
public void setTotalLogFileSize(long logSize) { | ||
maxEvents = (int)(logSize / EVENT_SIZE); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
.../hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/ContainerRollingLogAppender.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,75 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
package org.apache.hadoop.yarn; | ||
|
||
import org.apache.hadoop.classification.InterfaceAudience.Public; | ||
import org.apache.hadoop.classification.InterfaceStability.Unstable; | ||
import org.apache.log4j.RollingFileAppender; | ||
|
||
import java.io.File; | ||
import java.io.Flushable; | ||
|
||
/** | ||
* A simple log4j-appender for container's logs. | ||
* | ||
*/ | ||
@Public | ||
@Unstable | ||
public class ContainerRollingLogAppender extends RollingFileAppender implements Flushable { | ||
private String containerLogDir; | ||
private String containerLogFile; | ||
|
||
@Override | ||
public void activateOptions() { | ||
synchronized (this) { | ||
setFile(new File(this.containerLogDir, containerLogFile).toString()); | ||
setAppend(true); | ||
super.activateOptions(); | ||
} | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
if (qw != null) { | ||
qw.flush(); | ||
} | ||
} | ||
|
||
/** | ||
* Getter/Setter methods for log4j. | ||
* | ||
* @return containerLogDir. | ||
*/ | ||
|
||
public String getContainerLogDir() { | ||
return this.containerLogDir; | ||
} | ||
|
||
public void setContainerLogDir(String containerLogDir) { | ||
this.containerLogDir = containerLogDir; | ||
} | ||
|
||
public String getContainerLogFile() { | ||
return containerLogFile; | ||
} | ||
|
||
public void setContainerLogFile(String containerLogFile) { | ||
this.containerLogFile = containerLogFile; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...arn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestContainerLogAppender.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,48 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
package org.apache.hadoop.yarn; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.apache.log4j.PatternLayout; | ||
|
||
public class TestContainerLogAppender { | ||
|
||
@Test | ||
void testAppendInClose() throws Exception { | ||
final ContainerLogAppender claAppender = new ContainerLogAppender(); | ||
claAppender.setName("testCLA"); | ||
claAppender.setLayout(new PatternLayout("%-5p [%t]: %m%n")); | ||
claAppender.setContainerLogDir("target/testAppendInClose/logDir"); | ||
claAppender.setContainerLogFile("syslog"); | ||
claAppender.setTotalLogFileSize(1000); | ||
claAppender.activateOptions(); | ||
final Logger claLog = Logger.getLogger("testAppendInClose-catergory"); | ||
claLog.setAdditivity(false); | ||
claLog.addAppender(claAppender); | ||
claLog.info(new Object() { | ||
public String toString() { | ||
claLog.info("message1"); | ||
return "return message1"; | ||
} | ||
}); | ||
claAppender.close(); | ||
} | ||
} |
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