예제 #1
0
try:
    logging_settings = QSettings().value(LOGGING_SETTINGS_NAME, {}) or {}
except Exception:
    logging_settings = {}

if logging_settings:
    # Unserialize the bytes stream (gives us a dict)
    try:
        logging_settings = pickle.loads(logging_settings).get("children", {})
    except ValueError:
        logging_settings = {}
    # Since LoggingSettingsPlugin is a ParameterSettingsPlugin,
    # we access the settings (parameters) through the "children" key

# Create a file handler for logging to a file
file_log_level = logging_settings.get(
    FILE_LOG_LEVEL_SETTINGS_NAME, {}).get("value") or DEFAULT_FILE_LOG_LEVEL

# By default, append to log file
file_handler = logging.FileHandler(os.path.join(log_dir, log_file))
file_handler.setLevel(file_log_level)  # minimum level shown
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

# Create a stream handler (attaches to sys.stderr by default)
stream_log_level = DEFAULT_STREAM_LOG_LEVEL
# See if we have the stream logging level available to us
stream_log_settings = logging_settings.get(STREAM_LOG_LEVEL_SETTINGS_NAME,
                                           {}).get("value")
if logging_settings and stream_log_settings:
    stream_log_level = stream_log_settings
stream_handler = logging.StreamHandler()
예제 #2
0
logger.setLevel("DEBUG")  # minimum level shown

# Create a formatter that all handlers below can use for formatting their log messages
# format = "%(asctime)s - %(name)s - %(module)s:%(lineno)d - %(funcName)s - "
format = "%(asctime)s - %(caller_name)s - %(levelname)s - %(threadName)s > %(message)s"
date_format = "%a %b %d %H:%M:%S %Y"
formatter = logging.Formatter(fmt=format, datefmt=date_format)

# Use the QSettings set up for us by LoggingSettingsPlugin
logging_settings = QSettings().value(LOGGING_SETTINGS_NAME, {})
if logging_settings:
    # Unserialize the bytes stream (gives us a dict)
    logging_settings = pickle.loads(logging_settings)
    # Since LoggingSettingsPlugin is a ParameterSettingsPlugin,
    # we access the settings (parameters) through the "children" key
    logging_settings = logging_settings.get("children", {})

# Create a file handler for logging to a file
file_log_level = DEFAULT_FILE_LOG_LEVEL
# See if we have the file logging level available to us
file_log_settings = logging_settings.get(FILE_LOG_LEVEL_SETTINGS_NAME,
                                         {}).get("value")
if logging_settings and file_log_settings:
    file_log_level = file_log_settings
# By default, append to log file
file_handler = logging.FileHandler(os.path.join(log_dir, log_file))
file_handler.setLevel(file_log_level)  # minimum level shown
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

# Create a stream handler (attaches to sys.stderr by default)