# file() destination options

The `file()` driver outputs messages to the specified text file, or to a set of files. The `file()` destination has the following options:

Warning When creating several thousands separate log files, AxoSyslog might not be able to open the required number of files. This might happen for example, when using the `${HOST}` macro in the filename while receiving messages from a large number of hosts. To overcome this problem, adjust the `--fd-limit` command-line parameter of AxoSyslog or the global `ulimit` parameter of your host. For setting the `--fd-limit` command-line parameter ofAxoSyslog see the [The syslog-ng manual page](../../../docs/axosyslog-core/app-man-syslog-ng/syslog-ng.8/index.md). For setting the `ulimit` parameter of the host, see the documentation of your operating system. 

## create-dirs()

|   
---|---  
Type: | yes or no  
Default: | no  
  
_Description:_ Enable creating non-existing directories when creating files or socket files.

## dir-group()

|   
---|---  
Type: | string  
Default: | Use the global settings  
  
_Description:_ The group of the directories created by `syslog-ng`. To preserve the original properties of an existing directory, use the option without specifying an attribute: `dir-group()`.

## dir-owner()

|   
---|---  
Type: | string  
Default: | Use the global settings  
  
_Description:_ The owner of the directories created by `syslog-ng`. To preserve the original properties of an existing directory, use the option without specifying an attribute: `dir-owner()`.

Starting with version 3.16, the default value of this option is -1, so AxoSyslog does not change the ownership, unless explicitly configured to do so.

## dir-perm()

|   
---|---  
Type: | number  
Default: | Use the global settings  
  
_Description:_ The permission mask of directories created by `syslog-ng`. Log directories are only created if a file after macro expansion refers to a non-existing directory, and directory creation is enabled (see also the `create-dirs()` option). For octal numbers prefix the number with `0`, for example, use `0755` for `rwxr-xr-x`.

To preserve the original properties of an existing directory, use the option without specifying an attribute: `dir-perm()`. Note that when creating a new directory without specifying attributes for `dir-perm()`, the default permission of the directories is masked with the umask of the parent process (typically `0022`).

## disk-buffer()

_Description:_ This option enables putting outgoing messages into the disk buffer of the destination to avoid message loss in case of a system failure on the destination side. It has the following options:

### capacity-bytes()

Type: | number (bytes)  
---|---  
Default: | 1MiB  
  
_Description:_ This is a required option. The maximum size of the disk-buffer in bytes. The minimum value is `1048576` bytes. If you set a smaller value, the minimum value will be used automatically. It replaces the old `log-disk-fifo-size()` option.

In AxoSyslog version 4.2 and earlier, this option was called `disk-buf-size()`.

### compaction()

Type: | yes/no  
---|---  
Default: | no  
  
_Description:_ If set to `yes`, AxoSyslog prunes the unused space in the LogMessage representation, making the disk queue size smaller at the cost of some CPU time. Setting the `compaction()` argument to `yes` is recommended when numerous name-value pairs are unset during processing, or when the same names are set multiple times.

Note Simply unsetting these name-value pairs by using the `unset()` rewrite operation is not enough, as due to performance reasons that help when AxoSyslog is CPU bound, the internal representation of a `LogMessage` will not release the memory associated with these name-value pairs. In some cases, however, the size of this overhead becomes significant (the raw message size can grow up to four times its original size), which unnecessarily increases the disk queue file size. For these cases, the compaction will drop `unset` values, making the `LogMessage` representation smaller at the cost of some CPU time required to perform compaction. 

### dir()

Type: | string  
---|---  
Default: | N/A  
  
_Description:_ Defines the folder where the disk-buffer files are stored.

Warning

When creating a new `dir()` option for a disk buffer, or modifying an existing one, make sure you delete the persist file.

AxoSyslog creates disk-buffer files based on the path recorded in the persist file. Therefore, if the persist file is not deleted after modifying the `dir()` option, then following a restart, AxoSyslog will look for or create disk-buffer files in their old location. To ensure that AxoSyslog uses the new `dir()` setting, the persist file must not contain any information about the destinations which the disk-buffer file in question belongs to.

Note If the `dir()` path provided by the user does not exist, AxoSyslog creates the path with the same permission as the running instance. 

### flow-control-window-bytes()

Type: | number (bytes)  
---|---  
Default: | 163840000  
  
_Description:_ Use this option if the option `reliable()` is set to `yes`. This option contains the size of the messages in bytes that is used in the memory part of the disk buffer. It replaces the old `log-fifo-size()` option. It does not inherit the value of the global `log-fifo-size()` option, even if it is provided. Note that this option will be ignored if the option `reliable()` is set to `no`.

In AxoSyslog version 4.2 and earlier, this option was called `mem-buf-size()`.

### flow-control-window-size()

Type: | number(messages)  
---|---  
Default: | 10000  
  
_Description:_ Use this option if the option `reliable()` is set to `no`. This option contains the number of messages stored in overflow queue. It replaces the old `log-fifo-size()` option. It inherits the value of the global `log-fifo-size()` option if provided. If it is not provided, the default value is `10000` messages. Note that this option will be ignored if the option `reliable()` is set to `yes`.

In AxoSyslog version 4.2 and earlier, this option was called `mem-buf-length()`.

### front-cache-size()

Type: | number(messages)  
---|---  
Default: | 1000  
  
_Description:_ The number of messages stored in the output buffer of the destination. Note that if you change the value of this option and the disk-buffer already exists, the change will take effect when the disk-buffer becomes empty.

Options `reliable()` and `capacity-bytes()` are required options.

In AxoSyslog version 4.2 and earlier, this option was called `qout-size()`.

### prealloc()

Type: | yes/no  
---|---  
Default: | no  
  
_Description:_

By default, AxoSyslog doesn’t reserve the disk space for the disk-buffer file, since in a properly configured and sized environment the disk-buffer is practically empty, so a large preallocated disk-buffer file is just a waste of disk space. But a preallocated buffer can prevent other data from using the intended buffer space (and elicit a warning from the OS if disk space is low), preventing message loss if the buffer is actually needed. To avoid this problem, when using AxoSyslog 4.0 or later, you can preallocate the space for your disk-buffer files by setting `prealloc(yes)`.

In addition to making sure that the required disk space is available when needed, preallocated disk-buffer files provide radically better (3-4x) performance as well: in case of an outage the amount of messages stored in the disk-buffer is continuously growing, and using large continuous files is faster, than constantly waiting on a file to change its size.

If you are running AxoSyslog on a dedicated host (always recommended for any high-volume settings), use `prealloc(yes)`.

Available in AxoSyslog 4.0 and later.

### reliable()

Type: | yes/no  
---|---  
Default: | no  
  
_Description:_ If set to `yes`, AxoSyslog cannot lose logs in case of reload/restart, unreachable destination or AxoSyslog crash. This solution provides a slower, but reliable disk-buffer option. It is created and initialized at startup and gradually grows as new messages arrive. If set to `no`, the normal disk-buffer will be used. This provides a faster, but less reliable disk-buffer option.

Warning Hazard of data loss! If you change the value of `reliable()` option when there are messages in the disk-buffer, the messages stored in the disk-buffer will be lost. 

### truncate-size-ratio()

Type: | number((between 0 and 1))  
---|---  
Default: | 1 (do not truncate)  
  
_Description:_ Limits the truncation of the disk-buffer file. Truncating the disk-buffer file can slow down the disk IO operations, but it saves disk space. By default, AxoSyslog version 4.0 and later doesn’t truncate disk-buffer files by default (`truncate-size-ratio(1)`). Earlier versions freed the disk-space when at least 10% of the disk-buffer file could be freed (`truncate-size-ratio(0.1)`).

AxoSyslog only truncates the file if the possible disk gain is more than `truncate-size-ratio()` times `capacity-bytes()`.

  * Smaller values free disk space quicker.
  * Larger ratios result in better performance.



If you want to avoid performance fluctuations:

  * use `truncate-size-ratio(1)` (never truncate), or
  * use `prealloc(yes)` to reserve the entire size of the disk-buffer on disk.



Warning Axoflow does not recommend you to change `truncate-size-ratio()`. Only change its value if you understand the performance implications of doing so. 

### Example: Examples for using disk-buffer()

In the following case reliable disk-buffer() is used.
```
 
    destination d_demo {
        network(
            "127.0.0.1"
            port(3333)
            disk-buffer(
                flow-control-window-bytes(10000)
                capacity-bytes(2000000)
                reliable(yes)
                dir("/tmp/disk-buffer")
            )
        );
    };
    
```

In the following case normal disk-buffer() is used.
```
 
    destination d_demo {
        network(
            "127.0.0.1"
            port(3333)
                disk-buffer(
                flow-control-window-size(10000)
                capacity-bytes(2000000)
                reliable(no)
                dir("/tmp/disk-buffer")
            )
        );
    };
    
```

## flags()

|   
---|---  
Type: | no-multi-line, syslog-protocol, threaded  
Default: | empty set  
  
_Description:_ Flags influence the behavior of the destination driver.

  * _no-multi-line_ : The `no-multi-line` flag disables line-breaking in the messages: the entire message is converted to a single line.

  * _syslog-protocol_ : The `syslog-protocol` flag instructs the driver to format the messages according to the new IETF syslog protocol standard (RFC5424), but without the frame header. If this flag is enabled, macros used for the message have effect only for the text of the message, the message header is formatted to the new standard. Note that this flag is not needed for the `syslog` driver, and that the `syslog` driver automatically adds the frame header to the messages.

  * _threaded_ : The `threaded` flag enables multithreading for the destination. For details on multithreading, see [Multithreading and scaling](../../../docs/axosyslog-core/chapter-multithreading/index.md).

Note The `file` destination uses multiple threads only if the destination filename contains macros. 




## flush-lines()

|   
---|---  
Type: | number  
Default: | Use global setting (exception: for http() destination, the default is 1).  
  
_Description:_ Specifies how many lines are flushed to a destination at a time. The AxoSyslog application waits for this number of lines to accumulate and sends them off in a single batch. Increasing this number increases throughput as more messages are sent in a single batch, but also increases message latency.

The AxoSyslog application flushes the messages if it has sent `flush-lines()` number of messages, or the queue became empty. If you stop or reload AxoSyslog or in case of network sources, the connection with the client is closed, AxoSyslog automatically sends the unsent messages to the destination.

For optimal performance when sending messages to an AxoSyslog server, make sure that the value of `flush-lines()` is smaller than the window size set in the `log-iw-size()` option in the source of your server.

## frac-digits()

|   
---|---  
Type: | number  
Default: | 0  
  
_Description:_ The AxoSyslog application can store fractions of a second in the timestamps according to the ISO8601 format. The `frac-digits()` parameter specifies the number of digits stored. The digits storing the fractions are padded by zeros if the original timestamp of the message specifies only seconds. Fractions can always be stored for the time the message was received.

Note The AxoSyslog application can add the fractions to non-ISO8601 timestamps as well. 

Note As AxoSyslog is precise up to the microsecond, when the `frac-digits()` option is set to a value higher than 6, AxoSyslog will truncate the fraction seconds in the timestamps after 6 digits. 

## fsync()

|   
---|---  
Type: | yes or no  
Default: | no  
  
_Description:_ Forces an `fsync()` call on the destination fd after each write.

Note Enabling this option may seriously degrade performance. 

## hook-commands()

_Description:_ This option makes it possible to execute external programs when the relevant driver is initialized or torn down. The `hook-commands()` can be used with all source and destination drivers with the exception of the `usertty()` and `internal()` drivers.

Note The AxoSyslog application must be able to start and restart the external program, and have the necessary permissions to do so. For example, if your host is running AppArmor or SELinux, you might have to modify your AppArmor or SELinux configuration to enable AxoSyslog to execute external applications. 

### Using `hook-commands()` when AxoSyslog starts or stops

To execute an external program when AxoSyslog starts or stops, use the following options:

#### `startup()`

Type: | string  
---|---  
Default: | N/A  
  
_Description:_ Defines the external program that is executed as AxoSyslog starts.

#### `shutdown()`

Type: | string  
---|---  
Default: | N/A  
  
_Description:_ Defines the external program that is executed as AxoSyslog stops.

### Using the hook-commands() when AxoSyslog reloads

To execute an external program when the AxoSyslog configuration is initiated or torn down, for example, on startup/shutdown or during a AxoSyslog reload, use the following options:

#### `setup()`

Type: | string  
---|---  
Default: | N/A  
  
_Description:_ Defines an external program that is executed when the AxoSyslog configuration is initiated, for example, on startup or during a AxoSyslog reload.

#### `teardown()`

Type: | string  
---|---  
Default: | N/A  
  
_Description:_ Defines an external program that is executed when the AxoSyslog configuration is stopped or torn down, for example, on shutdown or during a AxoSyslog reload.

### Example: Using hook-commands() with a network source

In the following example, the `hook-commands()` is used with the `network()` driver and it opens an [iptables](<https://en.wikipedia.org/wiki/Iptables> "https://en.wikipedia.org/wiki/Iptables") port automatically as AxoSyslog is started/stopped.

The assumption in this example is that the `LOGCHAIN` chain is part of a larger ruleset that routes traffic to it. Whenever the AxoSyslog created rule is there, packets can flow, otherwise the port is closed.
```
 
    source {
        network(transport(udp)
        hook-commands(
              startup("iptables -I LOGCHAIN 1 -p udp --dport 514 -j ACCEPT")
              shutdown("iptables -D LOGCHAIN 1")
            )
         );
    };
    
```

## group()

|   
---|---  
Type: | string  
Default: | Use the global settings  
  
_Description:_ Set the group of the created file to the one specified. To preserve the original properties of an existing file, use the option without specifying an attribute: `group()`.

## local-time-zone()

|   
---|---  
Type: | name of the timezone, or the timezone offset  
Default: | The local timezone.  
  
_Description:_ Sets the timezone used when expanding filename and tablename templates.

The timezone can be specified by using the name, for example, `time-zone("Europe/Budapest")`), or as the timezone offset in +/-HH:MM format, for example, `+01:00`). On Linux and UNIX platforms, the valid timezone names are listed under the `/usr/share/zoneinfo` directory.

## log-fifo-size()

|   
---|---  
Type: | number  
Default: | Use global setting.  
  
_Description:_ The number of messages that the output queue can store.

## mark-freq()

|   
---|---  
Accepted values: | number [seconds]  
Default: | 1200  
  
_Description:_ An alias for the obsolete `mark()` option, retained for compatibility with version 1.6.x.

The number of seconds between two `MARK` messages. `MARK` messages are generated when there was no message traffic to inform the receiver that the connection is still alive. If set to zero (`0`), no `MARK` messages are sent. The `mark-freq()` can be set for global option and/or every `MARK` capable destination driver if `mark-mode()` is periodical or dst-idle or host-idle. If `mark-freq()` is not defined in the destination, then the `mark-freq()` will be inherited from the global options. If the destination uses internal `mark-mode()`, then the global `mark-freq()` will be valid (does not matter what `mark-freq()` set in the destination side).

## mark-mode()

Accepted values: | `internal` | `dst-idle` | `host-idle` | `periodical` | `none` | `global`  
---|---  
Default: | `internal` for pipe, program drivers `none` for file, unix-dgram, unix-stream drivers `global` for syslog, tcp, udp destinations `host-idle` for global option  
  
_Description:_ The `mark-mode()` option can be set for the following destination drivers: file(), program(), unix-dgram(), unix-stream(), network(), pipe(), syslog() and in global option.

  * `internal`: When internal mark mode is selected, internal source should be placed in the log path as this mode does not generate mark by itself at the destination. This mode only yields the mark messages from internal source. This is the mode as AxoSyslog 3.3 worked. `MARK` will be generated by internal source if there was NO traffic on local sources:

`file()`, `pipe()`, `unix-stream()`, `unix-dgram()`, `program()`

  * `dst-idle`: Sends `MARK` signal if there was NO traffic on destination drivers. `MARK` signal from internal source will be dropped.

`MARK` signal can be sent by the following destination drivers: `network()`, `syslog()`, `program()`, `file()`, `pipe()`, `unix-stream()`, `unix-dgram()`.

  * `host-idle`: Sends `MARK` signal if there was NO local message on destination drivers. for example, `MARK` is generated even if messages were received from tcp. `MARK` signal from internal source will be dropped.

`MARK` signal can be sent by the following destination drivers: `network()`, `syslog()`, `program()`, `file()`, `pipe()`, `unix-stream()`, `unix-dgram()`.

  * `periodical`: Sends `MARK` signal perodically, regardless of traffic on destination driver. `MARK` signal from internal source will be dropped.

`MARK` signal can be sent by the following destination drivers: `network()`, `syslog()`, `program()`, `file()`, `pipe()`, `unix-stream()`, `unix-dgram()`.

  * `none`: Destination driver drops all `MARK` messages. If an explicit mark-mode() is not given to the drivers where `none` is the default value, then `none` will be used.

  * `global`: Destination driver uses the global `mark-mode()` setting. Note that setting the global `mark-mode()` to global causes a syntax error in AxoSyslog.




Note In case of `dst-idle`, `host-idle` and `periodical`, the `MARK` message will not be written in the destination, if it is not open yet. 

Available in AxoSyslog 3.4 and later.

## overwrite-if-older()

|   
---|---  
Type: | number (seconds)  
Default: | 0  
  
_Description:_ If set to a value higher than 0, AxoSyslog checks when the file was last modified before starting to write into the file. If the file is older than the specified amount of time (in seconds), then AxoSyslog removes the existing file and opens a new file with the same name. In combination with for example, the `${WEEKDAY}` macro, this can be used for simple log rotation, in case not all history has to be kept. (Note that in this weekly log rotation example if its Monday 00:01, then the file from last Monday is not seven days old, because it was probably last modified shortly before 23:59 last Monday, so it is actually not even six days old. So in this case, set the `overwrite-if-older()` parameter to a-bit-less-than-six-days, for example, to `518000` seconds.

## owner()

|   
---|---  
Type: | string  
Default: | Use the global settings  
  
_Description:_ Set the owner of the created file to the one specified. To preserve the original properties of an existing file, use the option without specifying an attribute: `owner()`.

## pad-size()

|   
---|---  
Type: | number  
Default: | 0  
  
_Description:_ If set, AxoSyslog will pad output messages to the specified size (in bytes). Some operating systems (such as HP-UX) pad all messages to block boundary. This option can be used to specify the block size. (HP-UX uses 2048 bytes).

Warning Hazard of data loss! If the size of the incoming message is larger than the previously set `pad-size()` value, AxoSyslog will truncate the message to the specified size. Therefore, all message content above that size will be lost. 

## perm()

|   
---|---  
Type: | number  
Default: | Use the global settings  
  
_Description:_ The permission mask of the file if it is created by `syslog-ng`. For octal numbers prefix the number with `0`, for example, use `0755` for `rwxr-xr-x`.

To preserve the original properties of an existing file, use the option without specifying an attribute: `perm()`.

## suppress()

|   
---|---  
Type: | seconds  
Default: | 0 (disabled)  
  
_Description:_ If several identical log messages would be sent to the destination without any other messages between the identical messages (for example, an application repeated an error message ten times), AxoSyslog can suppress the repeated messages and send the message only once, followed by the `Last message repeated n times.` message. The parameter of this option specifies the number of seconds AxoSyslog waits for identical messages.

## symlink-as()

|   
---|---  
Type: | Filename  
Default: | N/A  
  
_Description:_ The configured file name will be used as a symbolic link to the last created file by file destination.

## Example: symlink-as()

An example when time-based macro is used:
```
 
       file("/var/log/cron.${YEAR}${MONTH}" symlink-as("/var/log/cron"));
    
```

In this case the `/var/log/cron` should point to the current month.

Note The symlink uses the same permissions as the file destination. 

## template()

|   
---|---  
Type: | string  
Default: | A format conforming to the default logfile format.  
  
_Description:_ Specifies a template defining the logformat to be used in the destination. Macros are described in [Macros of AxoSyslog](../../../docs/axosyslog-core/chapter-manipulating-messages/customizing-message-format/reference-macros/index.md). Please note that for network destinations it might not be appropriate to change the template as it changes the on-wire format of the syslog protocol which might not be tolerated by stock syslog receivers (like `syslogd` or `syslog-ng` itself). For network destinations make sure the receiver can cope with the custom format defined.

## template-escape()

|   
---|---  
Type: | yes or no  
Default: | no  
  
_Description:_ Turns on escaping for the `'`, `"`, and backspace characters in templated output files. This is useful for generating SQL statements and quoting string contents so that parts of the log message are not interpreted as commands to the SQL server.

> Note: Starting with AxoSyslog version 4.5, `template-escape(yes)` escapes the top-level template function in case of nested template functions.

## time-reap()

|   
---|---  
Accepted values: | number (seconds)  
Default: | 60 or 0, see description for details  
  
_Description:_ The time to wait in seconds before an idle destination file or pipe is closed. Note that only destination files having macros in their filenames are closed automatically.

Starting with version 3.23, the way how `time-reap()` works is the following.

  1. If the `time-reap()` option of the destination is set, that value is used, for example:
```
 destination d_fifo {
                 pipe(
                     "/tmp/test.fifo",
                     time-reap(30)  # sets time-reap() for this destination only
                 );
             };
         
```

  2. If the `time-reap()` option of the destination is not set, and the destination does not use a template or macro in its filename or path, `time-reap()` is automatically set to 0. For example:
```
 destination d_fifo {
                 pipe(
                     "/tmp/test.fifo",
                 );
             };
         
```

  3. Otherwise, the value of the global `time-reap()` option is used, which defaults to 60 seconds.




## time-reopen()

|   
---|---  
Accepted values: | number [seconds]  
Default: | 60  
  
_Description:_ The time to wait in seconds before a dead connection is reestablished.

## time-zone()

|   
---|---  
Type: | name of the timezone, or the timezone offset  
Default: | unspecified  
  
_Description:_ Convert timestamps to the timezone specified by this option. If this option is not set, then the original timezone information in the message is used. Converting the timezone changes the values of all date-related macros derived from the timestamp, for example, `HOUR`. For the complete list of such macros, see [Date-related macros](../../../docs/axosyslog-core/chapter-manipulating-messages/customizing-message-format/date-macros/index.md).

The timezone can be specified by using the name, for example, `time-zone("Europe/Budapest")`), or as the timezone offset in +/-HH:MM format, for example, `+01:00`). On Linux and UNIX platforms, the valid timezone names are listed under the `/usr/share/zoneinfo` directory.

## ts-format()

|   
---|---  
Type: | rfc3164, bsd, rfc3339, iso  
Default: | rfc3164  
  
_Description:_ Override the global timestamp format (set in the global `ts-format()` parameter) for the specific destination. For details, see [ts-format()](../../../docs/axosyslog-core/chapter-global-options/reference-options/index.md).

Note This option applies only to file and file-like destinations. Destinations that use specific protocols (for example, `network()`, or `syslog()`) ignore this option. For protocol-like destinations, use a template locally in the destination, or use the [proto-template](../../../docs/axosyslog-core/chapter-global-options/reference-options/index.md) option. 

Last modified July 2, 2023: [Change highlight mode of code examples (2f8a959)](<https://github.com/axoflow/axosyslog-core-docs/commit/2f8a95937c6498193e7168ce8b0dc831e9f0f8ad>)