Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
*/
package org.apache.logging.log4j.core.appender.rolling;

import java.io.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
Expand All @@ -26,8 +36,8 @@
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.core.util.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junitpioneer.jupiter.Issue;

public class RollingFileManagerTest {

Expand Down Expand Up @@ -75,14 +85,14 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
.withPolicy(new SizeBasedTriggeringPolicy(100))
.build();

Assert.assertNotNull(appender);
assertNotNull(appender);
final String testContent = "Test";
try (final RollingFileManager manager = appender.getManager()) {
Assert.assertEquals(file.getAbsolutePath(), manager.getFileName());
assertEquals(file.getAbsolutePath(), manager.getFileName());
manager.writeToDestination(testContent.getBytes(StandardCharsets.US_ASCII), 0, testContent.length());
}
try (final Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.US_ASCII)) {
Assert.assertEquals(testContent, IOUtils.toString(reader));
assertEquals(testContent, IOUtils.toString(reader));
}
}
}
Expand Down Expand Up @@ -125,7 +135,7 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
null,
null,
configuration);
Assert.assertNotNull(manager);
assertNotNull(manager);
manager.initialize();

// Get the initialTime of this original log file
Expand All @@ -139,9 +149,43 @@ public RolloverDescription rollover(final RollingFileManager manager) throws Sec
manager.rollover();

// If the rollover fails, then the size should not be reset
Assert.assertNotEquals(0, manager.getFileSize());
assertNotEquals(0, manager.getFileSize());

// The initialTime should not have changed
Assert.assertEquals(initialTime, manager.getFileTime());
assertEquals(initialTime, manager.getFileTime());
}

@Test
@Issue("https://github.com/apache/logging-log4j2/issues/1645")
public void testCreateParentDir() {
final Configuration configuration = new NullConfiguration();
final RollingFileManager manager = RollingFileManager.getFileManager(
null,
"testCreateParentDir.log.%d{yyyy-MM-dd}",
true,
false,
NoOpTriggeringPolicy.INSTANCE,
DirectWriteRolloverStrategy.newBuilder()
.withConfig(configuration)
.build(),
null,
PatternLayout.createDefaultLayout(configuration),
0,
true,
true,
null,
null,
null,
configuration);
assertNotNull(manager);
try {
final File file = new File("file_in_current_dir.log");
assertNull(file.getParentFile());
manager.createParentDir(file);
} catch (final Throwable t) {
fail("createParentDir failed: " + t.getMessage());
} finally {
manager.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,11 @@ public String getFileName() {
@Override
protected void createParentDir(File file) {
if (directWrite) {
file.getParentFile().mkdirs();
final File parent = file.getParentFile();
// If the parent is null the file is in the current working directory.
if (parent != null) {
parent.mkdirs();
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions log4j-to-slf4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
<artifactId>log4j-api-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.spi.CleanableThreadContextMap;
Expand Down Expand Up @@ -75,13 +76,12 @@ public boolean containsKey(final String key) {
}

@Override
@SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
public Map<String, String> getCopy() {
return MDC.getCopyOfContextMap();
final Map<String, String> contextMap = MDC.getCopyOfContextMap();
return contextMap != null ? contextMap : new HashMap<>();
}

@Override
@SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
public Map<String, String> getImmutableMapOrNull() {
return MDC.getCopyOfContextMap();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.logging.slf4j;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import org.apache.logging.log4j.spi.ThreadContextMap;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.Issue;
import org.slf4j.MDCTestHelper;
import org.slf4j.spi.MDCAdapter;

class MDCContextMapTest {

@Test
@Issue("https://github.com/apache/logging-log4j2/issues/1426")
void nonNullGetCopy() {
final ThreadContextMap contextMap = new MDCContextMap();
final MDCAdapter mockAdapter = mock(MDCAdapter.class);
when(mockAdapter.getCopyOfContextMap()).thenReturn(null);
final MDCAdapter adapter = MDCTestHelper.replaceMDCAdapter(mockAdapter);
try {
assertThat(contextMap.getImmutableMapOrNull()).isNull();
assertThat(contextMap.getCopy()).isNotNull();
verify(mockAdapter, times(2)).getCopyOfContextMap();
verifyNoMoreInteractions(mockAdapter);
} finally {
MDCTestHelper.replaceMDCAdapter(adapter);
}
}
}
28 changes: 28 additions & 0 deletions log4j-to-slf4j/src/test/java/org/slf4j/MDCTestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.slf4j;

import org.slf4j.spi.MDCAdapter;

public class MDCTestHelper {

public static MDCAdapter replaceMDCAdapter(final MDCAdapter adapter) {
final MDCAdapter old = MDC.mdcAdapter;
MDC.mdcAdapter = adapter;
return old;
}
}
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/fix_NPE_in_closeable_thread_context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
type="fixed">
<issue id="1426" link="https://github.com/apache/logging-log4j2/pull/1426"/>
<description format="asciidoc">
Fix NPE in `CloseableThreadContext`.
</description>
</entry>
10 changes: 10 additions & 0 deletions src/changelog/.2.x.x/fix_create_parent_dir.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
type="fixed">
<issue id="1645" link="https://github.com/apache/logging-log4j2/pull/1645"/>
<description format="asciidoc">
Fix NPE in `RollingFileManager`.
</description>
</entry>