Store application logs with ossfs
ossfs 1.0 uploads data to OSS only when a file is closed, but application log files stay open for the entire application lifetime. Writing logs directly to an ossfs mount path causes logs to be invisible in OSS during runtime, fills the local disk cache, and risks data loss on abnormal exit. Use log rotation to bridge this gap: write logs to a local directory, rotate them into closed files, then move the rotated files to the ossfs mount path.
Background
ossfs is a FUSE-based tool provided by Alibaba Cloud that mounts an OSS bucket as a local file system. ossfs 1.0 uses a single-part PUT model: it writes incoming data to a local disk cache and triggers the upload to OSS only when the file handle is closed. For details, see File uploads.
An application log file opens when the application starts and closes only when the application exits. This open-file lifecycle is incompatible with ossfs 1.0's upload-on-close behavior, which is why a bridging strategy is required.
Recommended practice: log locally and archive with ossfs
Write application logs to a local directory, rotate them periodically into closed files, then move the rotated files to the ossfs mount path. ossfs uploads each closed file to the OSS bucket automatically.
Configure your application to write logs to a standard local directory—not the ossfs mount path. This keeps logs immediately readable for real-time troubleshooting during runtime.
Set up a log rotation utility such as
logrotateto split logs periodically by time or size. Use thecreatedirective instead ofcopytruncateso that each rotated file is fully closed before the next write cycle begins. Thecopytruncatedirective keeps the original file open and may produce incomplete objects in OSS.-
Move or copy the rotated log files to the ossfs 1.0 mount path. Because rotated files are closed and complete, this step aligns with the upload-on-close behavior of ossfs 1.0, ensuring timely and durable storage in OSS. To automate this step, add a
postrotateorlastactionblock to your logrotate config:/var/log/myapp/*.log { daily rotate 7 create compress postrotate mv /var/log/myapp/*.log.1 /mnt/ossfs/logs/ endscript }Replace
/var/log/myapp/with your application's log directory and/mnt/ossfs/logs/with the ossfs mount path.
This pattern keeps active logs on the local disk for real-time access and archived logs in an OSS bucket for long-term retention and cross-node access, while eliminating the three risks described below.
Why direct logging to ossfs is not recommended
Writing logs directly to a path mounted by ossfs 1.0 produces three failure modes:
Logs are not visible in OSS during runtime. An application log stays open from startup to exit. Because ossfs 1.0 uploads only when the file is closed, log data remains in the local cache throughout the entire runtime and never appears in the OSS bucket, making real-time troubleshooting impossible.
Local disk fills up. As the application writes logs, ossfs 1.0 accumulates temporary files in its cache directory. Over time, this consumes all available local disk space.
Data loss on abnormal exit. Before a file is closed, data written through ossfs 1.0 exists only in temporary local files and has not been persisted to OSS. If the host crashes or loses power and the ossfs 1.0 process exits abnormally, all unclosed log files are lost.