Skip to content

Commit

Permalink
Made logging an option that can be enabled by the initiate_logger fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
Jennifer A Clark committed Oct 30, 2020
1 parent 5394858 commit 4c3cd45
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 189 deletions.
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ API Documentation
.. autosummary::
:toctree: _autosummary

initiate_logger
multipole_mie_combining_rules

43 changes: 43 additions & 0 deletions mapsci/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,46 @@
__version__ = versions['version']
__git_revision__ = versions['full-revisionid']
del get_versions, versions

import logging
import logging.handlers
import os

logger = logging.getLogger()
logger.setLevel(30)

def initiate_logger(console=False, log_file=None, verbose=30):
"""
Initiate a logging handler if more detail on the calculations is desired.
Parameters
----------
console : bool, Optional, default=False
Initiates a stream handler to print to a console
log_file : bool/str, Optional, default=None
If log output should be recorded in a file, set this keyword to either True or to a name for the log file. If True, the file name 'mapsci.log' is used. Note that if a file with the same name already exists, it will be deleted.
verbose : int, Optional, default=30
The verbosity of logging information can be set to any supported representation of the `logging level <https://docs.python.org/3/library/logging.html#logging-levels>`_.
"""

logger.setLevel(verbose)

if console:
# Set up logging to console
console_handler = logging.StreamHandler() # sys.stderr
console_handler.setFormatter( logging.Formatter('[%(levelname)s](%(name)s): %(message)s') )
console_handler.setLevel(verbose)
logger.addHandler(console_handler)

if log_file is not None:

if type(log_file) != str:
log_file = "mapsci.log"

if os.path.isfile(log_file):
os.remove(log_file)

log_file_handler = logging.handlers.RotatingFileHandler(log_file)
log_file_handler.setFormatter( logging.Formatter('%(asctime)s [%(levelname)s](%(name)s:%(funcName)s:%(lineno)d): %(message)s') )
log_file_handler.setLevel(verbose)
logger.addHandler(log_file_handler)
15 changes: 0 additions & 15 deletions mapsci/__main__.py

This file was deleted.

Loading

0 comments on commit 4c3cd45

Please sign in to comment.