JCL Log Rotation Configuration Examples

In the Java Logging Framework (JCL) or standard java.util.logging ecosystem, log files continuously accumulate data as application runtime extends. Without intervention, disk space will be rapidly consumed, potentially leading to system resource exhaustion. Therefore, configuring a reasonable log rotation strategy is a critical task in operations and development. JCL supports multiple triggering conditions to initiate new log files, primarily based on file size, time intervals, and custom rotation rules. Understanding these mechanisms forms the foundation for implementing automated log management: when specific thresholds are met (e.g., a single file reaching 10MB), the system automatically closes the current stream and creates a newly named file or a new file within an existing directory, while retaining compressed versions of historical records for auditing and troubleshooting.
The most common and practical configuration method involves triggering rotation based on the maximum capacity of individual log files. This mode is suitable for scenarios with fluctuating data volumes that require regular archiving. In log4j2.xml or logging.properties, we can define a <RollingFile> (for Log4J2) or configure file cleanup policies via properties.

Taking Log4j2 as an example, its configuration typically includes the following key elements:

  • File Size Threshold: Set the maximum byte count for a single log file; for instance, set it to "10 MB". Once the written data exceeds this limit, rotation is triggered.
  • Naming Convention: New files usually inherit the original filename and append timestamps or sequence numbers (e.g., app.log.2023-10-27 or app.log.1.gz).
  • Compression Strategy: It is recommended to enable automatic compression of archived logs to save storage space.

An example configuration snippet is shown below:

<Configuration status="WARN">
    <Appenders>
        <RollingFile name="file" fileName="/var/log/myapp/app.log" 
                    filePattern="${logDir}/%d{yyyy-MM-dd}-%i.log.gz" 
                    policy="SizeBasedTriggeringPolicy" maxFileSize="10 MB"/>
    </Appenders>
    <Loggers>
        <!-- Log output configuration -->
    </Loggers>
</Configuration>

In this scenario, when the size of app.log exceeds 10MB, the JCL framework automatically generates a new file with a date-based name (e.g., 2023-10-28-%i.log.gz) and archives the old data in compressed format.

Rotation Configuration Schemes Based on Time Intervals

In addition to triggering based on file size, many application scenarios prefer fixed-time rotation strategies, such as automatically cleaning up logs from the previous day at midnight daily or performing large-scale organization once a week. This mode is commonly used in systems that comply with commercial settlement cycles or regulatory requirements. In configuration, it is usually necessary to specify a fixed date format (e.g., yyyy-MM-dd) as the prefix for filenames.

Implementing time-interval rotation hinges on defining triggers such as "daily" or "every Monday" periodic events and pairing them with corresponding naming templates:

  • Fixed Filename: New files use today's date to generate a unique name, avoiding confusion with logs from previous dates.
  • Cleanup Policy: You can configure the system to retain only recent N days of rotated logs (e.g., maxHistory=30); older files exceeding this range will be automatically deleted or removed via compression.

A typical time-interval rotation logic example is: whenever the system clock enters a new date at midnight, regardless of the current file size, it forces the generation of a new log file for that specific day. This approach is highly effective for isolating data across days and ensures that operation records from each day remain clear and independent.

Comprehensive Rotation Configuration and Best Practice Recommendations

In actual production environments, a single strategy often fails to meet all requirements; therefore, it is common practice to combine "size" and "time" mechanisms (Combined Triggering). This hybrid mode prevents individual files from becoming excessively large, which can degrade I/O performance, while ensuring logs are archived at specific time points. Furthermore, reasonable disk space management strategies are equally important and must be executed in conjunction with deletion or compression rules within the Policies module.

The following best practices should be followed during actual deployment:

  1. Dynamically Adjust Thresholds: Regularly evaluate and fine-tune file size limits based on traffic characteristics during business peak and off-peak periods (e.g., adjusting from 5MB to 20MB).
  2. Enable Asynchronous Rotation: For high-concurrency systems, configure the logging framework to support non-blocking write modes to avoid service lag caused by lock contention at the moment of file rotation.
  3. Unify Archive Paths: Store logs of different levels (INFO, ERROR) or from different modules in a unified directory structure for easier aggregation analysis by monitoring tools.

By flexibly utilizing the above configuration examples and strategy combinations, developers can build an efficient and secure log management system that ensures good observability and stability during long-term operation.