Skip to content

Java library providing rotating/rolling FileOutputStream.

License

Notifications You must be signed in to change notification settings

liran2000/rotating-fos

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Actions Status Maven Central License

rotating-fos is a Java 8 library providing RotatingFileOutputStream which internally rotates a delegate FileOutputStream using provided rotation policies similar to logrotate, Log4j and Logback.

Usage

You first need to include rotating-fos in your Maven/Gradle dependencies:

<dependency>
    <groupId>com.vlkan.rfos</groupId>
    <artifactId>rotating-fos</artifactId>
    <version>${rotating-fos.version}</version>
</dependency>

(Note that the Java 9 module name is com.vlkan.rfos.)

RotatingFileOutputStream does not extend java.io.FileOutputStream (as a deliberate design decision, see How (not) to extend standard collection classes), but java.io.OutputStream. Its basic usage is pretty straightforward:

RotationConfig config = RotationConfig
        .builder()
        .file("/tmp/app.log")
        .filePattern("/tmp/app-%d{yyyyMMdd-HHmmss.SSS}.log")
        .policy(new SizeBasedRotationPolicy(1024 * 1024 * 100 /* 100MiB */))
        .policy(DailyRotationPolicy.getInstance())
        .build();

try (RotatingFileOutputStream stream = new RotatingFileOutputStream(config)) {
    stream.write("Hello, world!".getBytes(StandardCharsets.UTF_8));
}

RotationConfig.Builder supports the following methods:

Method(s) Description
file(File)
file(String)
file accessed (e.g., /tmp/app.log)
filePattern(RotatingFilePattern)
filePattern(String)
rotating file pattern (e.g., /tmp/app-%d{yyyyMMdd-HHmmss-SSS}.log)
policy(RotationPolicy)
policies(Set<RotationPolicy> policies)
rotation policies
executorService(ScheduledExecutorService) scheduler for time-based policies and compression tasks
append(boolean) append while opening the file (defaults to true)
compress(boolean) GZIP compression after rotation (defaults to false)
rollingFile(boolean) rolling file by indexes, based on SizeBasedRotationPolicy, similar to log4j RollingFileAppender. (defaults to false)
clock(Clock) clock for retrieving date and time (defaults to SystemClock)
callback(RotationCallback) rotation callback (defaults to LoggingRotationCallback)

The default ScheduledExecutorService can be retrieved via RotationConfig#getDefaultExecutorService(), which is a ScheduledThreadPoolExecutor of size Runtime.getRuntime().availableProcessors(). Note that unless explicitly specified in RotationConfig.Builder, all instances of RotationConfig (and hence of RotatingFileOutputStream) will share the same ScheduledExecutorService. You can change the default pool size via RotationJanitorCount system property.

Packaged rotation policies are listed below. (You can also create your own rotation policies by implementing RotationPolicy interface.)

  • DailyRotationPolicy
  • WeeklyRotationPolicy
  • SizeBasedRotationPolicy

Once you have a handle on RotatingFileOutputStream, in addition to standard java.io.OutputStream methods (e.g., write(), close(), etc.), it provides the following methods:

Method Description
getConfig() employed RotationConfig
rotate(RotationPolicy, Instant) trigger a rotation

RotatingFilePattern.Builder supports the following methods:

Method Description
pattern(String) rotating file pattern (e.g., /tmp/app-%d{yyyyMMdd-HHmmss-SSS}.log)
locale(Locale) Locale used in the DateTimeFormatter (defaults to Locale.getDefault())
timeZoneId(ZoneId) ZoneId denoting the time zone used in the DateTimeFormatter (defaults to TimeZone.getDefault().toZoneId())

Rotation-triggered custom behaviours can be introduced via RotationCallback passed to RotationConfig.Builder. RotationCallback provides the following methods.

Method Description
onTrigger(RotationPolicy, Instant) invoked at the beginning of every rotation attempt
onOpen(RotationPolicy, Instant, OutputStream) invoked at start and during rotation
onClose(RotationPolicy, Instant, OutputStream) invoked on stream close and during rotation
onSuccess(RotationPolicy, Instant, File) invoked after a successful rotation
onFailure(RotationPolicy, Instant, File, Exception) invoked after a failed rotation attempt

Caveats

  • append is enabled for RotatingFileOutputStream by default, whereas it is disabled (and hence truncates the file at start) for standard FileOutputStream by default.

  • Rotated file conflicts are not resolved by rotating-fos. Once a rotation policy gets triggered, rotating-fos applies the given filePattern to determine the rotated file name. In order to avoid previously generated files to be overridden, prefer a sufficiently fine-grained date-time pattern.

    For instance, given filePattern is /tmp/app-%d{yyyyMMdd}.log, if SizeBasedRotationPolicy gets triggered multiple times within a day, the last one will override the earlier generations in the same day. In order to avoid this, you should use a date-time pattern with a higher resolution, such as /tmp/app-%d{yyyyMMdd-HHmmss-SSS}.log.

  • Make sure RotationCallback methods are not blocking. Callbacks are invoked using the ScheduledExecutorService passed via RotationConfig. Hence blocking callback methods have a direct impact on time-sensitive policies and compression tasks.

  • When append is enabled, be cautious while using onOpen and onClose callbacks. These callbacks might be employed to introduce headers and/or footers to certain type of files, e.g., CSV. Though one needs to avoid injecting the same header and/or footer multiple times when a file is re-opened for append. Note that this is not a problem for files opened/closed via rotation.

Contributors

License

Copyright © 2018-2020 Volkan Yazıcı

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

https://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.

About

Java library providing rotating/rolling FileOutputStream.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%