108.10. File Operations (xm_fileop)
This module provides functions and procedures to manipulate files. Coupled with a Schedule block, this module allows various log rotation and retention policies to be implemented, including:
-
log file retention based on file size,
-
log file retention based on file age, and
-
cyclic log file rotation and retention.
Note
|
Rotating, renaming, or removing the file written by om_file is also supported with the help of the om_file reopen() procedure. |
108.10.1. Configuration
The xm_fileop module accepts only the common module directives.
108.10.2. Functions
The following functions are exported by xm_fileop.
- string
dir_temp_get()
-
Return the name of a directory suitable as a temporary storage location.
108.10.3. Procedures
The following procedures are exported by xm_fileop.
dir_make(string path);
-
Create a directory recursively (like
mkdir -p
). It succeeds if the directory already exists. An error is logged if the operation fails.
dir_remove(string file);
-
Remove the directory from the filesystem.
file_chown(string file, integer uid, integer gid);
-
Change the ownership of file. This function is only implemented on POSIX systems where chown() is available in the underlying operating system. An error is logged if the operation fails.
file_chown(string file, string user, string group);
-
Change the ownership of file. This function is only implemented on POSIX systems where chown() is available in the underlying operating system. An error is logged if the operation fails.
file_cycle(string file);
-
Do a cyclic rotation on file. The file will be moved to "file.1". If "file.1" already exists it will be moved to "file.2", and so on. Wildcards are supported in the file path and filename. The backslash (
\
) must be escaped if used as the directory separator with wildcards (for example,C:\\test\\*.log
). This procedure will reopen the LogFile if it is cycled. An error is logged if the operation fails. file_cycle(string file, integer max);
-
Do a cyclic rotation on file as described above. The max argument specifies the maximum number of files to keep. For example, if max is
5
, "file.6" will be deleted.
file_remove(string file);
-
Remove file. It is possible to specify a wildcard in the filename (but not in the path). The backslash (
\
) must be escaped if used as the directory separator with wildcards (for example,C:\\test\\*.log
). This procedure will reopen the LogFile if it is removed. An error is logged if the operation fails. file_remove(string file, datetime older);
-
Remove file if its creation time is older than the value specified in older. It is possible to specify a wildcard in the filename (but not in the path). The backslash (
\
) must be escaped if used as the directory separator with wildcards (for example,C:\\test\\*.log
). This procedure will reopen the LogFile if it is removed. An error is logged if the operation fails.
file_touch(string file);
-
Update the last modification time of file or create the file if it does not exist. An error is logged if the operation fails.
file_truncate(string file);
-
Truncate file to zero length. If the file does not exist, it will be created. An error is logged if the operation fails.
file_truncate(string file, integer offset);
-
Truncate file to the size specified in offset. If the file does not exist, it will be created. An error is logged if the operation fails.
108.10.4. Examples
In this example, the internal log file is rotated based on time and size.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define LOGFILE C:\Program Files (x86)\nxlog\data\nxlog.log
define LOGFILE /var/log/nxlog/nxlog.log
<Extension fileop>
Module xm_fileop
# Check the log file size every hour and rotate if larger than 1 MB
<Schedule>
Every 1 hour
Exec if (file_size('%LOGFILE%') >= 1M) \
file_cycle('%LOGFILE%', 2);
</Schedule>
# Rotate log file every week on Sunday at midnight
<Schedule>
When @weekly
Exec file_cycle('%LOGFILE%', 2);
</Schedule>
</Extension>