Repository: OpenNMT/OpenNMT-py

Issue:
Log file not append new log message
Why the logger only save a few latest log messages?
For example, I train a model and save checkpoint every 10000 steps,

onmt_train -data data/demo -save_model model/demo -log_file log/train.log \
-train_steps 100000 -valid_steps 10000 -save_checkpoint_steps 10000

When the logger saves new messages, the previous messages will be deleted.
When I view the log file, it only shows the latest log:

[2020-02-06 14:58:02,525 INFO] Step 99900/100000; acc:  70.44; ppl:  3.30; xent: 1.19; lr: 0.00040; 32004/34572 tok/s;   6369 sec
[2020-02-06 14:58:10,779 INFO] Step 99950/100000; acc:  70.48; ppl:  3.27; xent: 1.18; lr: 0.00040; 34320/36778 tok/s;   6377 sec
[2020-02-06 14:58:19,096 INFO] Step 100000/100000; acc:  69.92; ppl:  3.36; xent: 1.21; lr: 0.00040; 32923/35770 tok/s;   6385 sec
[2020-02-06 14:58:19,097 INFO] Loading dataset from data/demo.valid.0.pt
[2020-02-06 14:58:19,391 INFO] number of examples: 19058
[2020-02-06 14:58:44,566 INFO] Validation perplexity: 5.25664
[2020-02-06 14:58:44,566 INFO] Validation accuracy: 64.571
[2020-02-06 14:58:45,811 INFO] Saving checkpoint model/demo.model_step_100000.pt
[2020-02-06 18:00:23,001 INFO] Translating shard 0.
[2020-02-06 18:04:58,505 INFO] Translating shard 1.


Discussion:
The logfile was made "Rotating" a few weeks ago in #1700 (with REST server usage in mind).
We should probably make different behaviors for serving vs training, or at least make this an option.
Any update?

Resolving patch:
diff --git a/onmt/translate/translation_server.py b/onmt/translate/translation_server.py
--- a/onmt/translate/translation_server.py
+++ b/onmt/translate/translation_server.py
@@ -279,7 +279,8 @@ def __init__(self, opt, model_id, preprocess_opt=None, tokenizer_opt=None,
         else:
             log_file = None
         self.logger = init_logger(log_file=log_file,
-                                  log_file_level=self.opt.log_file_level)
+                                  log_file_level=self.opt.log_file_level,
+                                  rotate=True)
 
         self.loading_lock = threading.Event()
         self.loading_lock.set()
diff --git a/onmt/utils/logging.py b/onmt/utils/logging.py
--- a/onmt/utils/logging.py
+++ b/onmt/utils/logging.py
@@ -6,7 +6,7 @@
 logger = logging.getLogger()
 
 
-def init_logger(log_file=None, log_file_level=logging.NOTSET):
+def init_logger(log_file=None, log_file_level=logging.NOTSET, rotate=False):
     log_format = logging.Formatter("[%(asctime)s %(levelname)s] %(message)s")
     logger = logging.getLogger()
     logger.setLevel(logging.INFO)
@@ -16,8 +16,11 @@ def init_logger(log_file=None, log_file_level=logging.NOTSET):
     logger.handlers = [console_handler]
 
     if log_file and log_file != '':
-        file_handler = RotatingFileHandler(
-            log_file, maxBytes=1000000, backupCount=10)
+        if rotate:
+            file_handler = RotatingFileHandler(
+                log_file, maxBytes=1000000, backupCount=10)
+        else:
+            file_handler = logging.FileHandler(log_file)
         file_handler.setLevel(log_file_level)
         file_handler.setFormatter(log_format)
         logger.addHandler(file_handler)


Test patch: