Skip to content

Commit 68b1d10

Browse files
committed
rotate log file when it grows too big
1 parent 3c6c686 commit 68b1d10

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

docs/source/history.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Dev
2424
- Add support for listing the available hooks, to be used in help
2525
output of commands like virtualenvwrapper.project's mkproject.
2626
- Fix mkvirtualenv -h option behavior.
27+
- Change logging so the $WORKON_HOME/hook.log file rotates after
28+
10KiB.
2729

2830
2.0.2
2931

virtualenvwrapper/hook_loader.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import inspect
1010
import logging
11+
import logging.handlers
1112
import optparse
1213
import os
1314

@@ -53,12 +54,20 @@ def main():
5354
parser.disable_interspersed_args() # stop when we hit an option without an '-'
5455
options, args = parser.parse_args()
5556

56-
# Set up logging to a file and to the console
57-
logging.basicConfig(
58-
filename=os.path.expandvars(os.path.join('$WORKON_HOME', 'hook.log')),
59-
level=logging.DEBUG,
60-
format='%(asctime)s %(levelname)s %(name)s %(message)s',
57+
root_logger = logging.getLogger('')
58+
59+
# Set up logging to a file
60+
root_logger.setLevel(logging.DEBUG)
61+
file_handler = logging.handlers.RotatingFileHandler(
62+
os.path.expandvars(os.path.join('$WORKON_HOME', 'hook.log')),
63+
maxBytes=10240,
64+
backupCount=1,
6165
)
66+
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')
67+
file_handler.setFormatter(formatter)
68+
root_logger.addHandler(file_handler)
69+
70+
# Send higher-level messages to the console, too
6271
console = logging.StreamHandler()
6372
console_level = [ logging.WARNING,
6473
logging.INFO,
@@ -67,7 +76,7 @@ def main():
6776
console.setLevel(console_level)
6877
formatter = logging.Formatter('%(name)s %(message)s')
6978
console.setFormatter(formatter)
70-
logging.getLogger('').addHandler(console)
79+
root_logger.addHandler(console)
7180

7281
#logging.getLogger(__name__).debug('cli args %s', args)
7382

0 commit comments

Comments
 (0)