Skip to content

Commit

Permalink
bugfix: Change to setup_logging so that it attempts to use the progra…
Browse files Browse the repository at this point in the history
…m name

to determine log location instead of the __main__ module. This is because
sometimes the __main__ module is actually zprocess.process_class_wrapper,
for subprocesses started using the zprocess.Process class. To prevent their logs
ending up in the zprocess directory, we can use the name of the program to
determine the location of that program's __main__.py module instead.

This is particularly important on linux if you are not using
anaconda then zprocess may be installed somewhere without write permissions,
so the log file is not just misplaced but throws a permissions error.
  • Loading branch information
chrisjbillington committed May 22, 2017
1 parent d130bbe commit b48580c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions setup_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
def setup_logging(program_name, log_level=logging.DEBUG, terminal_level=logging.INFO, maxBytes=1024*1024*50, backupCount=1):
logger = logging.getLogger(program_name)
main_path = __main__.__file__ if hasattr(__main__, '__file__') else __file__
try:
try:
program_module = __import__(program_name)
except ImportError:
program_module = __import__(program_name.lower())
main_path = program_module.__file__
except ImportError:
main_path = __main__.__file__ if hasattr(__main__, '__file__') else __file__

log_dir = os.path.dirname(os.path.realpath(main_path))
log_path = os.path.join(log_dir, '%s.log' % program_name)
handler = logging.handlers.RotatingFileHandler(log_path, maxBytes=maxBytes, backupCount=backupCount)
Expand Down

0 comments on commit b48580c

Please sign in to comment.