Logs to a file.



Log data to a file.

class composer.loggers.file_logger.FileLogger(filename='{run_name}/logs-rank{rank}.txt', artifact_name=None, *, capture_stdout=True, capture_stderr=True, buffer_size=1, log_level=LogLevel.EPOCH, log_interval=1, flush_interval=100, overwrite=False)[source]#

Bases: composer.loggers.logger_destination.LoggerDestination

Log data to a file.

Example usage:
from composer.loggers import FileLogger, LogLevel
from composer.trainer import Trainer
file_logger = FileLogger(
trainer = Trainer(

Example output:

[FIT][step=2]: { "logged_metric": "logged_value", }
[EPOCH][step=2]: { "logged_metric": "logged_value", }
[BATCH][step=2]: { "logged_metric": "logged_value", }
[EPOCH][step=3]: { "logged_metric": "logged_value", }
  • filename (str, optional) โ€“

    Format string for the filename.

    The following format variables are available:




    The name of the training run. See run_name.


    The global rank, as returned by get_global_rank().


    The local rank of the process, as returned by get_local_rank().


    The world size, as returned by get_world_size().


    The local world size, as returned by get_local_world_size().


    The node rank, as returned by get_node_rank().


    When training with multiple devices (i.e. GPUs), ensure that '{rank}' appears in the format. Otherwise, multiple processes may attempt to write to the same file.

    Consider the following example when using default value of โ€˜{run_name}/logs-rank{rank}.txtโ€™:

    >>> file_logger = FileLogger(filename='{run_name}/logs-rank{rank}.txt')
    >>> trainer = Trainer(loggers=[file_logger], run_name='my-awesome-run')
    >>> file_logger.filename

    Default: โ€˜{run_name}/logs-rank{rank}.txtโ€™

  • artifact_name (str, optional) โ€“

    Format string for the logfileโ€™s artifact name.

    The logfile will be periodically logged (according to the flush_interval) as a file artifact. The artifact name will be determined by this format string.

    See also

    log_file_artifact() for file artifact logging.

    The same format variables for filename are available. Setting this parameter to None (the default) will use the same format string as filename. It is sometimes helpful to deviate from this default. For example, when filename contains an absolute path, it is recommended to set this parameter explicitely, so the absolute path does not appear in any artifact stores.

    Leading slashes ('/') will be stripped.

    Default: None (which uses the same format string as filename)

  • capture_stdout (bool, optional) โ€“ Whether to include the stdout``in ``filename. (default: True)

  • capture_stderr (bool, optional) โ€“ Whether to include the stderr``in ``filename. (default: True)

  • buffer_size (int, optional) โ€“ Buffer size. See open(). Default: 1 for line buffering.

  • log_level (LogLevel, optional) โ€“ LogLevel (i.e. unit of resolution) at which to record. Default: EPOCH.

  • log_interval (int, optional) โ€“ Frequency to print logs. If log_level is EPOCH, logs will only be recorded every n epochs. If log_level is BATCH, logs will be printed every n batches. Otherwise, if log_level is FIT, this parameter is ignored, as calls at the FIT log level are always recorded. Default: 1.

  • flush_interval (int, optional) โ€“ How frequently to flush the log to the file, relative to the log_level. For example, if the log_level is EPOCH, then the logfile will be flushed every n epochs. If the log_level is BATCH, then the logfile will be flushed every n batches. Default: 100.

  • overwrite (bool, optional) โ€“ Whether to overwrite an existing logfile. (default: False)

property artifact_name[source]#

The artifact name for the logfile.

property filename[source]#

The filename for the logfile.

write(prefix, s)[source]#

Write to the logfile.


If the write occurs before the INIT event, the write will be enqueued, as the file is not yet open.

  • prefix (str) โ€“ A prefix for each line in the logfile.

  • s (str) โ€“ The string to write. Each line will be prefixed with prefix.