Commits

Doug Hellmann committed 68b1d10

rotate log file when it grows too big

  • Participants
  • Parent commits 3c6c686

Comments (0)

Files changed (2)

File docs/source/history.rst

   - Add support for listing the available hooks, to be used in help
     output of commands like virtualenvwrapper.project's mkproject.
   - Fix mkvirtualenv -h option behavior.
+  - Change logging so the $WORKON_HOME/hook.log file rotates after
+    10KiB.
 
 2.0.2
 

File virtualenvwrapper/hook_loader.py

 
 import inspect
 import logging
+import logging.handlers
 import optparse
 import os
 
     parser.disable_interspersed_args() # stop when we hit an option without an '-'
     options, args = parser.parse_args()
 
-    # Set up logging to a file and to the console
-    logging.basicConfig(
-        filename=os.path.expandvars(os.path.join('$WORKON_HOME', 'hook.log')),
-        level=logging.DEBUG,
-        format='%(asctime)s %(levelname)s %(name)s %(message)s',
+    root_logger = logging.getLogger('')
+
+    # Set up logging to a file
+    root_logger.setLevel(logging.DEBUG)
+    file_handler = logging.handlers.RotatingFileHandler(
+        os.path.expandvars(os.path.join('$WORKON_HOME', 'hook.log')),
+        maxBytes=10240,
+        backupCount=1,
         )
+    formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')
+    file_handler.setFormatter(formatter)
+    root_logger.addHandler(file_handler)
+
+    # Send higher-level messages to the console, too
     console = logging.StreamHandler()
     console_level = [ logging.WARNING,
                       logging.INFO,
     console.setLevel(console_level)
     formatter = logging.Formatter('%(name)s %(message)s')
     console.setFormatter(formatter)
-    logging.getLogger('').addHandler(console)
+    root_logger.addHandler(console)
 
     #logging.getLogger(__name__).debug('cli args %s', args)