Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Commit

Permalink
[logging] only create log file if a message is being written (no empt…
Browse files Browse the repository at this point in the history
…y files). Fixes #48
  • Loading branch information
marscher committed Jan 20, 2015
1 parent 463b4f2 commit 20a9b2a
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pyemma/util/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,36 @@ def setupLogging():
args = conf_values['Logging']

if args.enabled:
if args.tofile and args.file:
filename = args.file
else:
filename = None
try:
logging.basicConfig(level=args.level,
format=args.format,
datefmt='%d-%m-%y %H:%M:%S',
filename=filename,
filemode='a')
datefmt='%d-%m-%y %H:%M:%S')
except IOError as ie:
import warnings
warnings.warn('logging could not be initialized, because of %s' % ie)
return
""" in case we want to log to both file and stream, add a separate handler"""
formatter = logging.Formatter(args.format)

if args.tofile:
# set delay to True, to prevent creation of empty log files
fh = logging.FileHandler(args.file, mode='a', delay=True)
fh.setFormatter(formatter)
fh.setLevel(args.level)
logging.getLogger('').addHandler(fh)
if args.toconsole and args.tofile:
ch = logging.StreamHandler()
# since we have used basicConfig (without file args),
# a StreamHandler is already used. Set our arguments.
ch = logging.getLogger('').handlers[0]
ch.setLevel(args.level)
ch.setFormatter(logging.Formatter(args.format))
logging.getLogger('').addHandler(ch)
ch.setFormatter(formatter)
else:
dummyInstance = dummyLogger()

enabled = args.enabled


def getLogger(name = None):
def getLogger(name=None):
if not enabled:
return dummyInstance
""" if name is not given, return a logger with name of the calling module."""
Expand Down

0 comments on commit 20a9b2a

Please sign in to comment.