import os
import logging
import logging.config
import socket
import copy
[docs]def create_shared_logger_data(logger_names, log_levels, log_to_consoles,
sim_name, log_directory):
"""
This function must be called to create a shared copy of information that will be
required to setup logging across processes. This must be run exactly once in the
root process.
:param logger_names: This is a list of names of the loggers whose output you're
interested in.
:param log_levels: This is the list of the same size of `logger_names` containing
the log levels specified as strings (e.g. 'INFO', 'DEBUG', 'WARNING', 'ERROR').
:param log_to_consoles: This is a list of the same size of `logger_names` containing
boolean values which indicate whether or not to redirect the output of the said
logger to stdout or not. Note that with scoop, and output to stdout on any
worker gets directed to the console of the main process.
:param sim_name: This is a string that is used when creating the log files.
Short for simulation name.
:param log_directory: This is the path of the directory in which the log files will
be stored. This directory must be an existing directory.
"""
# process / validate input
assert len(logger_names) == len(log_levels) == len(log_to_consoles), \
"The sizes of logger_names, log_levels, log_to_consoles are inconsistent"
assert all(isinstance(x, str) for x in logger_names + log_levels), \
"'logger_names' and 'log_levels' must be lists of strings"
assert os.path.isdir(log_directory), "The log_directory {} is not a vlid log directory".format(log_directory)
log_to_consoles = [bool(x) for x in log_to_consoles]
global logger_names_global, log_levels_global, log_to_consoles_global
global sim_name_global, log_directory_global
logger_names_global = logger_names
log_levels_global = log_levels
log_to_consoles_global = log_to_consoles
sim_name_global = sim_name
log_directory_global = log_directory
configure_loggers._already_configured = False
configure_loggers.basic_config_dict = {
'version': 1,
'formatters': {
'file': {
'format': '%(asctime)s %(name)s {} %(process)d %(levelname)-8s: %(message)s'.format(socket.gethostname())
},
'stream': {
'format': '%(processName)-10s %(name)s {} %(process)d %(levelname)-8s: %(message)s'.format(
socket.gethostname())
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout',
'formatter': 'stream',
},
'file_log': {
'class': 'logging.FileHandler',
'formatter': 'file',
'filename': 'LOG.txt',
},
'file_error': {
'class': 'logging.FileHandler',
'formatter': 'file',
'filename': 'ERROR.txt',
'level': 'ERROR',
},
},
'loggers': {},
'root': {
# 'level': 'INFO',
'handlers': []
}
}