tinylog native logging implementation providing writers, policies, converters, and core logging functionality
Rollover policies in tinylog-impl determine when the RollingFileWriter should create a new log file and optionally archive the current one. Policies can be combined to create sophisticated rollover strategies based on file size, time, or application startup.
All rollover policies implement the Policy interface, which defines the contract for rollover decisions.
/**
* Interface for rollover policies that determine when to create new log files
*/
interface Policy {
/**
* Check if an existing log file should be continued
* @param path Path to the existing log file
* @return true if the file should be continued, false to trigger rollover
*/
boolean continueExistingFile(String path);
/**
* Check if the current log file should be continued after writing an entry
* @param entry The log entry bytes that were just written
* @return true if the current file should continue, false to trigger rollover
*/
boolean continueCurrentFile(byte[] entry);
/**
* Reset the policy state (called after rollover)
*/
void reset();
}Triggers rollover when the log file reaches a specified size limit.
/**
* Policy that triggers rollover based on file size
*/
class SizePolicy implements Policy {
/**
* Default constructor for size-based rollover policy
*/
public SizePolicy();
/**
* Constructor for size-based rollover policy with size specification
* @param argument Size specification (supports units: B, KB, MB, GB)
*/
public SizePolicy(String argument);
}Configuration Examples:
# Rollover when file reaches 10 MB
writer=rolling file
writer.file=application.log
writer.policies=size:10MB
# Rollover at 500 KB
writer=rolling file
writer.file=debug.log
writer.policies=size:500KB
# Rollover at 2 GB
writer=rolling file
writer.file=archive.log
writer.policies=size:2GBSupported Size Units:
B - BytesKB - Kilobytes (1,024 bytes)MB - Megabytes (1,024 KB)GB - Gigabytes (1,024 MB)Triggers rollover daily at midnight, creating date-stamped log files.
/**
* Policy that triggers rollover daily at midnight
*/
class DailyPolicy extends AbstractDatePolicy {
/**
* Default constructor for daily rollover at midnight
*/
public DailyPolicy();
/**
* Constructor with configuration argument
* @param argument Configuration argument for policy
*/
public DailyPolicy(String argument);
}Configuration Examples:
# Daily rollover at midnight (system timezone)
writer=rolling file
writer.file=daily.log
writer.policies=daily
# Daily rollover in specific timezone
writer=rolling file
writer.file=utc-daily.log
writer.policies=daily:UTC
# Daily rollover in New York timezone
writer=rolling file
writer.file=ny-daily.log
writer.policies=daily:America/New_YorkTriggers rollover monthly on the first day of each month at midnight.
/**
* Policy that triggers rollover monthly at midnight on the first day
*/
class MonthlyPolicy extends AbstractDatePolicy {
/**
* Default constructor for monthly rollover at midnight
*/
public MonthlyPolicy();
/**
* Constructor with configuration argument
* @param argument Configuration argument for policy
*/
public MonthlyPolicy(String argument);
}Configuration Examples:
# Monthly rollover at midnight on the 1st
writer=rolling file
writer.file=monthly.log
writer.policies=monthly
# Monthly rollover in UTC timezone
writer=rolling file
writer.file=monthly-utc.log
writer.policies=monthly:UTCTriggers rollover when the application starts, ensuring each application run gets its own log file.
/**
* Policy that triggers rollover on application startup
*/
class StartupPolicy implements Policy {
/**
* Default constructor for startup-based rollover
*/
public StartupPolicy();
}Configuration Examples:
# New log file for each application startup
writer=rolling file
writer.file=startup.log
writer.policies=startup
# Combine with size policy for startup + size-based rollover
writer=rolling file
writer.file=app.log
writer.policies=startup,size:50MBAdvanced policy that allows custom rollover logic based on system properties, environment variables, or other dynamic conditions.
/**
* Policy with dynamic rollover conditions
*/
class DynamicPolicy implements Policy {
/**
* Constructor with dynamic condition expression
* @param condition Dynamic condition for rollover evaluation
*/
public DynamicPolicy(String condition);
}Configuration Examples:
# Rollover based on system property
writer=rolling file
writer.file=dynamic.log
writer.policies=dynamic:${sys:rollover.trigger}
# Rollover based on environment variable
writer=rolling file
writer.file=env-dynamic.log
writer.policies=dynamic:${env:LOG_ROLLOVER}Base class for time-based policies providing common date/time functionality.
/**
* Abstract base class for date/time-based rollover policies
*/
abstract class AbstractDatePolicy implements Policy {
/**
* Constructor with timezone support
* @param timezone Timezone for date calculations
*/
protected AbstractDatePolicy(String timezone);
/**
* Get the configured timezone
* @return TimeZone instance
*/
protected TimeZone getTimeZone();
/**
* Calculate the next rollover time
* @param currentTime Current timestamp
* @return Next rollover timestamp
*/
protected abstract long calculateNextRolloverTime(long currentTime);
}Multiple policies can be combined to create sophisticated rollover strategies. When multiple policies are specified, rollover occurs when ANY policy condition is met.
Size + Daily Rollover:
# Rollover daily OR when file reaches 100MB
writer=rolling file
writer.file=combined.log
writer.policies=daily,size:100MBStartup + Size Rollover:
# New file at startup, then rollover every 50MB
writer=rolling file
writer.file=app.log
writer.policies=startup,size:50MBMultiple Time-based Policies:
# Rollover daily in business hours, monthly otherwise
writer=rolling file
writer.file=business.log
writer.policies=daily:America/New_York,monthly:UTCBasic Size-based Rollover:
writer=rolling file
writer.file=application.log
writer.policies=size:25MB
writer.format={date: yyyy-MM-dd HH:mm:ss.SSS} [{level}] {class}: {message}Time-based Rollover with Compression:
writer=rolling file
writer.file=daily-app.log
writer.policies=daily
writer.converter=gzip
writer.format={date: HH:mm:ss} {level}: {message}Complex Multi-Policy Setup:
writer=rolling file
writer.file=/var/log/myapp/application.log
writer.policies=startup,daily:UTC,size:500MB
writer.converter=gzip
writer.format={date: yyyy-MM-dd HH:mm:ss.SSS} {level} [{thread}] {class}.{method}(): {message}
writer.charset=UTF-8
writer.buffered=trueDevelopment vs Production Policies:
# Development - new file each startup for debugging
writer=rolling file
writer.file=dev-app.log
writer.policies=startup,size:10MB
# Production - daily rollover with large size limit
writer=rolling file
writer.file=/var/log/prod-app.log
writer.policies=daily,size:1GB
writer.converter=gzipWhen rollover occurs, archived files follow specific naming patterns:
filename.log.1, filename.log.2, etc.filename.2023-12-25.logfilename.2023-12.logfilename.2023-12-25.log.gzCustom Naming:
writer=rolling file
writer.file=app-{date:yyyy-MM-dd}.log
writer.policies=daily
# Creates: app-2023-12-25.log, app-2023-12-26.log, etc.Install with Tessl CLI
npx tessl i tessl/maven-org-tinylog--tinylog-impl