schrodinger.application.matsci.textlogger module¶
Class to set up a nicely formatted logger
Copyright Schrodinger, LLC. All rights reserved.
- schrodinger.application.matsci.textlogger.log_error(msg, logger=None)¶
Add a message to the log file and exit with an error code
- Parameters
msg (str) – The message to log
logger (
logging.Logger
) – The logger object or None if msg should be printed
- schrodinger.application.matsci.textlogger.log_msg(msg, timestamp=False, pad=False, pad_below=False, logger=None, break_lines=False)¶
Add a message to the log file
- Parameters
msg (str) – The message to log
timestamp (bool) – Whether to print a timestamp with the message
pad (bool) – Whether to pad above this message with a blank line
pad_below (bool) – Whether to pad below this message with a blank line
logger (
logging.Logger
) – The logger object or None if msg should be printedbreak_lines (bool) – Whether to break the message into multiple lines when there are linebreaks in the message
- schrodinger.application.matsci.textlogger.log(logger, msg, level=20)¶
Log a message if the logger is defined, else print it
- Parameters
logger (
logging.Logger
) – The logger object or None if no logger is definedmsg (str) – The message to log
level (int) – The log level to use when logging. These are constants from the Python logging module. The default is INFO.
- schrodinger.application.matsci.textlogger.log_debug(logger, msg, timestamp=True)¶
Log a massage at the DEBUG level.
- Parameters
logger (
logging.Logger
) – The logger object or None if no logger is definedmsg (str) – The message to log
timestamp (bool) – Whether to print a timestamp with the message
msg – The message to log
- schrodinger.application.matsci.textlogger.timeit_logger_debug(func)¶
Log enter and leave time of a function in a class that may or may not contain self.logger attribute. If it doesn’t, logging will be done to the stdout.
- schrodinger.application.matsci.textlogger.log_initial_data(logger, namespace, msgwidth=None, namespace_header='Command line options:', job_header='Job information')¶
Log information about the currently running job, if running under job control, and all information in the namespace object.
- Parameters
namespace (object) – The object whose properties should be logged. In typical use, this is an
argparse.Namespace
object. All the values in the __dict__ of this object will be logged. Note that __dict__ does not contain the double-underscore class methods.logger (
logging.Logger
) – The logger to log withmsgwidth (int) – The width of the log messages. Taken from the logger if not supplied
namespace_header (str or None) – If not None, this line will be printed above the logged namespace properties
job_header (str or None) – If not None, this line will be printed above the logged job properties
- schrodinger.application.matsci.textlogger.log_namespace(namespace, logger, msgwidth=None, header='Command line options:')¶
Log the value of all properties on a object, such as logging all the parameters of an
argparse.Namespace
object.- Parameters
namespace (object) – The object whose properties should be logged. In typical use, this is an
argparse.Namespace
object. All the values in the __dict__ of this object will be logged. Note that __dict__ does not contain the double-underscore class methods.logger (
logging.Logger
) – The logger to log withmsgwidth (int) – The width of the log messages. Taken from the logger if not supplied
header (str or None) – If not None, this line will be printed above the logged properties
- schrodinger.application.matsci.textlogger.log_jobinfo(logger, msgwidth=None, header='Job information')¶
Log the information about the current running job if there is one.
- Parameters
logger (
logging.Logger
) – The logger to log withmsgwidth (int) – The width of the log messages. Taken from the logger if not supplied
header (str or None) – If not None, this line will be printed above the logged properties
- schrodinger.application.matsci.textlogger.run_jobs_with_update_logging(jobq, logfn, fraction=0.1, interval=3500, add_subjob_files=True)¶
Run all the current jobs in the JobDJ and logs progress information at semi-regular intervals so the user can track status
- Parameters
jobq (
schrodinger.job.queue.JobDJ
) – The queue to runlogfn (callable) – A function that takes a string and a boolean timestamp argument and logs the string
fraction (float) – A value between 0 and 1. When this fraction of the total jobs has completed since the last log update, a new update will be triggered.
interval (int) – A new update will be triggered when this number of seconds have passed since the last log update
add_subjob_files (bool) – Add subjob files to the backend when a subjob completes (if a backend exists)
- Note
fraction and interval are not obeyed strictly as updates can only be triggered when a job completes
- schrodinger.application.matsci.textlogger.create_logger(logfilename=None, extension='-driver.log', related_filename=None, verbose=False, width=80, set_as_global=False)¶
Create a logger that can be used for logging information to a file
If running under job control, the log file this logger writes to will be added to the backend as a Log File.
- Parameters
logfilename (str) – The name of the log file the logger should write to. If not supplied, the name will be based off the Job name if running under job control or based off of the basename of a related_file if not. As a last resort, the logfile name will use ‘logfile’ as the base name.
extension (str) – The extension to add to the file name if logfilename is not supplied.
related_filanem – The basename of this filename will be used as the base of the log file name IF logfilename is not supplied directly and we are not running under job control.
verbose (bool, or None) – Whether to enable verbose logging. If None, the verbosity is automatically setup according to debugging mode.
width (int) – Text width of the file
set_as_global (bool) – Whether to store the logger object as a module variable to be used when textlogger log functions are called from anywhere in the code.
- Return type
(
logging.Logger
, str)- Returns
The logger and the name of the file it logs to.
- schrodinger.application.matsci.textlogger.driver_logging(related_name=None, options=None, log_time=True, set_as_global=False)¶
Setup logging for the driver and log initial data and finish and run times
- Parameters
related_name (str) – If not running under job control, the basename of this name will be used for the log file.
options (Namespace) – The options to log
log_time (bool) – Whether finish time and duration should be logged
set_as_global (bool) – Whether to store the logger object as a module variable to be used when textlogger log functions are called from anywhere in the code.
- schrodinger.application.matsci.textlogger.get_global_logger()¶
Return the global logger object
- Return type
logging.Logger
- Returns
The global logger object
- schrodinger.application.matsci.textlogger.pretty_time_delta(seconds)¶
Turn seconds into 1d 2h 3m 4s format
- Parameters
seconds (float) – The number of seconds to convert
- Return str
The time as string
- schrodinger.application.matsci.textlogger.get_param_string(identifier, value, msgwidth)¶
Return a formatted line with identifier on the left and value right-justified. A line of ‘…’ will consume the intervening space.
- Parameters
identifier (str) – description of the value
value (any) – the value of the descriptor
msgwidth (int) – the length of the msg
- Return type
str
- Returns
msg, formatted msg
- class schrodinger.application.matsci.textlogger.GetLogger(logfilename, verbose, msgwidth)¶
Bases:
object
Set up a textwrapped log file.
- __init__(logfilename, verbose, msgwidth)¶
Create a GetLogger instance and process it.
- Parameters
logfilename (str) – name of the log file
verbose (boolean) – enable verbose logging
msgwidth (int) – message width
- class MyFormatter(msgwidth)¶
Bases:
logging.Formatter
Extend logging.Formatter to customize the formats of different log levels and to textwrap the messages.
- FORMATS = {10: '%(message)s', 20: '%(message)s', 'DEFAULT': '%(levelname)s (from function %(funcName)s):\n\n%(message)s\n'}¶
- __init__(msgwidth)¶
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()
({}
) formatting orstring.Template
formatting in your format string.Changed in version 3.2: Added the
style
parameter.
- format(record)¶
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
- converter()¶
- localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
Convert seconds since the Epoch to a time tuple expressing local time. When ‘seconds’ is not passed in, convert the current time instead.
- default_msec_format = '%s,%03d'¶
- default_time_format = '%Y-%m-%d %H:%M:%S'¶
- formatException(ei)¶
Format and return the specified exception information as a string.
This default implementation just uses traceback.print_exception()
- formatMessage(record)¶
- formatStack(stack_info)¶
This method is provided as an extension point for specialized formatting of stack information.
The input data is a string as returned from a call to
traceback.print_stack()
, but with the last trailing newline removed.The base implementation just returns the value passed in.
- formatTime(record, datefmt=None)¶
Return the creation time of the specified LogRecord as formatted text.
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the ‘converter’ attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the ‘converter’ attribute in the Formatter class.
- usesTime()¶
Check if the format uses the creation time of the record.
- setItUp()¶
Set up the logger.
- schrodinger.application.matsci.textlogger.decode_base64_cmd(value)¶
Extract a BASE64 encoded command line from the full command line in value and return it as plain text string
- Parameters
value (str) – A full job control command line containing a -cmd BASE64 etc. encoded command line string
- Return type
str, str
- Returns
The second value is the raw decoded BASE64 value and the first value is the raw value modified to be similar to what a user would type to on the command line (double quotes, redirects, parens, “python” removed, $SCHRODINGER/run added)
- schrodinger.application.matsci.textlogger.close_logger_file_handlers(logger)¶
Close all logging.FileHandler’s for the given logger.
- Parameters
logger (
logging.Logger
) – the logger object
- schrodinger.application.matsci.textlogger.create_debug_logger(name, formatter=None, add_to_backend=None, stream=True)¶
Create a logger for module level debug usage.
- Parameters
name (str) – the logger name
formatter ('logging.Formatter') – the logging format
add_to_backend (bool or None) – if True and logfile is created, add it to the backend. If None, add the file to backend in debug environment.
stream (bool) – if True and the newly created logfile is added to the backend, stream the file to the submission host
- Returns
The logger created for this function
- Return type
logging.Logger