简体   繁体   中英

spring-boot tomcat access log rotation by size

Is it possible to rotate tomcat access logs based on size? Going through the Appendix , I couldn't find such an option. These are the only access log options I'm seeing:

server.tomcat.accesslog.buffered=true # Whether to buffer output such that it is flushed only periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Whether to defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for the IP address, Hostname, protocol, and port used for the request.
server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.

There is no way to do that with the out of the box Tomcat. But there are a couple of options.

a) Disable tomcat's rotation completely by setting server.tomcat.accesslog.rotate as false, then do the rotation using another utility like unix's logrotate which does support rotating by size. Use the copytruncate option to avoid restarting tomcat.

b) Implement a custom AccessLogValve and override the rotate() method to customize rotation as you want. Then inject this valve using addContextValves in TomcatEmbeddedServletContainerFactory (you can find an example of customizing TomcatEmbeddedServletContainerFactory here ). You'll also have to remove the default implementation from the list returned by getValves .

By default, Tomcat doesn't provide access log rotation based on size, though you can configure hourly/monthly etc. configuration using file date format using server.tomcat.accesslog.file-date-format . for example hourly

server.tomcat.accesslog.file-date-format=.yyyy-MM-dd.HH

If you still need to rotate based on size only, you can extend Access Log Valve . check tomcat docs or refer this thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM