On 2016-09-15 15:57, Daiyue Weng wrote:
Hi, I am trying to record memory and CPU usages and load a logger from an
external config file (logging.conf),

[snip]

import logging

import psutil

logging.config.fileConfig('logging.conf')

hardware_log = logging.getLogger(HARDWARELOGNAME)

free_mem_gb_pre = psutil.virtual_memory().available / 1000000000.
cpu_util_pct_pre = psutil.cpu_percent()

hardware_log.info(cpu_util_pct_pre, free_mem_gb_pre)


I got the following errors,

--- Logging error ---
Traceback (most recent call last):
  File "C:\Continuum\Anaconda3\lib\logging\__init__.py", line 980, in emit
    msg = self.format(record)
  File "C:\Continuum\Anaconda3\lib\logging\__init__.py", line 830, in format
    return fmt.format(record)
  File "C:\Continuum\Anaconda3\lib\logging\__init__.py", line 567, in format
    record.message = record.getMessage()
  File "C:\Continuum\Anaconda3\lib\logging\__init__.py", line 330, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting

[snip]

  File 
"C:\Users\dweng\PycharmProjects\lumar_ingestion\ingestion_workflow_modules\import_to_dataframe.py",
line 61, in execute
    hardware_log.info(cpu_util_pct_pre, free_mem_gb_pre)
Message: 38.5
Arguments: (8.87662592,)


How to fix the problem?

The doc for logging.info says:

    logging.info(msg, *args, **kwargs)

In this line of your code:

    hardware_log.info(cpu_util_pct_pre, free_mem_gb_pre)

you're passing in 2 values.

The first (38.5) is being treated as the message template (a format string) and the second (8.87662592) as the value to be put into the template.

What you should be doing is something like:

logging.info('cpu_util_pct_pre is %s, free_mem_gb_pre is %s', cpu_util_pct_pre, free_mem_gb_pre)

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to