-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to dynamically change log filename
- Loading branch information
Showing
9 changed files
with
357 additions
and
0 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
69 changes: 69 additions & 0 deletions
69
tinylog-impl/src/main/java/org/tinylog/path/DynamicNameSegment.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,69 @@ | ||
/* | ||
* Copyright 2021 Martin Winandy | ||
* | ||
* 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.tinylog.path; | ||
|
||
import org.tinylog.policies.DynamicNamePolicy; | ||
import org.tinylog.policies.Policy; | ||
import org.tinylog.runtime.Timestamp; | ||
|
||
/** | ||
* Path segment that represents the log filename from {@link DynamicNamePolicy}. | ||
*/ | ||
public class DynamicNameSegment implements Segment { | ||
|
||
private static String dynamicName; | ||
|
||
DynamicNameSegment(final String defaultValue) { | ||
setDynamicName(defaultValue); | ||
} | ||
|
||
/** | ||
* Returns the current dynamic name. | ||
* | ||
* @return the current dynamic name | ||
*/ | ||
public static String getDynamicName() { | ||
return dynamicName; | ||
} | ||
|
||
/** | ||
* Sets a new dynamic name. | ||
* When used together with {@link DynamicNamePolicy} and the dynamic name differs from the current one, | ||
* a {@linkplain Policy#reset() reset} is triggered. | ||
* | ||
* @param newDynamicName the dynamic name to set | ||
*/ | ||
public static void setDynamicName(final String newDynamicName) { | ||
if (dynamicName != null && dynamicName.equals(newDynamicName)) { | ||
return; | ||
} | ||
dynamicName = newDynamicName; | ||
DynamicNamePolicy.setReset(); | ||
} | ||
|
||
@Override | ||
public String getStaticText() { | ||
return getDynamicName(); | ||
} | ||
|
||
@Override | ||
public boolean validateToken(final String token) { | ||
return dynamicName != null && dynamicName.equals(token); | ||
} | ||
|
||
@Override | ||
public String createToken(final String prefix, final Timestamp timestamp) { | ||
return getDynamicName(); | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
tinylog-impl/src/main/java/org/tinylog/policies/DynamicNamePolicy.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,65 @@ | ||
/* | ||
* Copyright 2021 Martin Winandy | ||
* | ||
* 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.tinylog.policies; | ||
|
||
import org.tinylog.Level; | ||
import org.tinylog.path.DynamicNameSegment; | ||
import org.tinylog.provider.InternalLogger; | ||
|
||
/** | ||
* Policy for triggering a manual rollover by calling {@link #setReset()}. | ||
* Might be used together with {@link DynamicNameSegment}. | ||
*/ | ||
public final class DynamicNamePolicy implements Policy { | ||
|
||
private static boolean reset; | ||
|
||
/** | ||
* | ||
*/ | ||
public DynamicNamePolicy() { | ||
this(null); | ||
} | ||
|
||
/** | ||
* @param argument Should be always {@code null} as dynamic name policy does not support arguments | ||
*/ | ||
public DynamicNamePolicy(final String argument) { | ||
if (argument != null) { | ||
InternalLogger.log(Level.WARN, "Dynamic name policy does not support arguments"); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean continueExistingFile(final String path) { | ||
return !reset; | ||
} | ||
|
||
@Override | ||
public boolean continueCurrentFile(final byte[] entry) { | ||
return !reset; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
reset = false; | ||
} | ||
|
||
/** | ||
* Sets the reset flag to trigger a {@linkplain #reset() reset}. | ||
*/ | ||
public static void setReset() { | ||
reset = true; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
tinylog-impl/src/main/resources/META-INF/services/org.tinylog.policies.Policy
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
org.tinylog.policies.DailyPolicy | ||
org.tinylog.policies.DynamicNamePolicy | ||
org.tinylog.policies.MonthlyPolicy | ||
org.tinylog.policies.StartupPolicy | ||
org.tinylog.policies.SizePolicy |
66 changes: 66 additions & 0 deletions
66
tinylog-impl/src/test/java/org/tinylog/path/DynamicNameSegmentTest.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,66 @@ | ||
/* | ||
* Copyright 2021 Martin Winandy | ||
* | ||
* 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.tinylog.path; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link DynamicNameSegment}. | ||
*/ | ||
public final class DynamicNameSegmentTest { | ||
|
||
/** | ||
* Verifies that the passed initial dynamic name text as well as the dynamic name set via | ||
* {@link DynamicNameSegment#setDynamicName(String)} will be returned as static text. | ||
*/ | ||
@Test | ||
public void doesHaveStaticText() { | ||
DynamicNameSegment segment = new DynamicNameSegment("test"); | ||
assertThat(segment.getStaticText()).isEqualTo("test"); | ||
DynamicNameSegment.setDynamicName("foo"); | ||
assertThat(segment.getStaticText()).isEqualTo("foo"); | ||
DynamicNameSegment.setDynamicName("bar"); | ||
assertThat(segment.getStaticText()).isEqualTo("bar"); | ||
} | ||
|
||
/** | ||
* Verifies that the passed initial dynamic name text as well as the dynamic name set via | ||
* {@link DynamicNameSegment#setDynamicName(String)} will be returned as generated token. | ||
*/ | ||
@Test | ||
public void createToken() { | ||
DynamicNameSegment segment = new DynamicNameSegment("test"); | ||
assertThat(segment.createToken(null, null)).isEqualTo("test"); | ||
DynamicNameSegment.setDynamicName("foo"); | ||
assertThat(segment.getStaticText()).isEqualTo("foo"); | ||
DynamicNameSegment.setDynamicName("bar"); | ||
assertThat(segment.getStaticText()).isEqualTo("bar"); | ||
} | ||
|
||
/** | ||
* Verifies that the dynamic name text will be accepted as valid token. | ||
*/ | ||
@Test | ||
public void validateValidToken() { | ||
DynamicNameSegment segment = new DynamicNameSegment("test"); | ||
assertThat(segment.validateToken("test")).isTrue(); | ||
assertThat(segment.validateToken("foo")).isFalse(); | ||
DynamicNameSegment.setDynamicName("foo"); | ||
assertThat(segment.validateToken("test")).isFalse(); | ||
assertThat(segment.validateToken("foo")).isTrue(); | ||
} | ||
|
||
} |
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
93 changes: 93 additions & 0 deletions
93
tinylog-impl/src/test/java/org/tinylog/policies/DynamicNamePolicyTest.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,93 @@ | ||
/* | ||
* Copyright 2021 Martin Winandy | ||
* | ||
* 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.tinylog.policies; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.tinylog.configuration.ServiceLoader; | ||
import org.tinylog.rules.SystemStreamCollector; | ||
import org.tinylog.util.FileSystem; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link DynamicNamePolicy}. | ||
*/ | ||
public final class DynamicNamePolicyTest { | ||
|
||
/** | ||
* Redirects and collects system output streams. | ||
*/ | ||
@Rule | ||
public final SystemStreamCollector systemStream = new SystemStreamCollector(true); | ||
|
||
/** | ||
* Verifies that an existing log file will be never continued. | ||
* | ||
* @throws IOException | ||
* Failed creating temporary file | ||
*/ | ||
@Test | ||
public void discontinueExistingFile() throws IOException { | ||
String file = FileSystem.createTemporaryFile(); | ||
final DynamicNamePolicy policy = new DynamicNamePolicy(null); | ||
assertThat(policy.continueExistingFile(file)).isTrue(); | ||
DynamicNamePolicy.setReset(); | ||
assertThat(policy.continueExistingFile(file)).isFalse(); | ||
policy.reset(); | ||
assertThat(policy.continueExistingFile(file)).isTrue(); | ||
} | ||
|
||
/** | ||
* Verifies that the current log file will be always continued. | ||
*/ | ||
@Test | ||
public void continueCurrentFile() { | ||
final DynamicNamePolicy policy = new DynamicNamePolicy(null); | ||
assertThat(policy.continueCurrentFile(new byte[0])).isTrue(); | ||
DynamicNamePolicy.setReset(); | ||
assertThat(policy.continueCurrentFile(new byte[0])).isFalse(); | ||
policy.reset(); | ||
assertThat(policy.continueCurrentFile(new byte[0])).isTrue(); | ||
} | ||
|
||
/** | ||
* Verifies that the reset() method can be executed without throwing any exception. | ||
*/ | ||
@Test | ||
public void resetIsCallable() { | ||
new DynamicNamePolicy(null).reset(); | ||
} | ||
|
||
/** | ||
* Verifies that a warning will be output, if an argument is passed. | ||
*/ | ||
@Test | ||
public void warnIfArgumentIsSet() { | ||
new DynamicNamePolicy("test"); | ||
assertThat(systemStream.consumeErrorOutput()).containsOnlyOnce("WARN").containsOnlyOnce("argument"); | ||
} | ||
|
||
/** | ||
* Verifies that policy is registered as service under the name "startup". | ||
*/ | ||
@Test | ||
public void isRegistered() { | ||
Policy policy = new ServiceLoader<>(Policy.class, String.class).create("dynamic name", (String) null); | ||
assertThat(policy).isInstanceOf(DynamicNamePolicy.class); | ||
} | ||
|
||
} |
Oops, something went wrong.