1

I am trying to add the python MemoryHandler into a logging configuration file. I am using a similar structure to a file handler that worked previously. I am a bit confused as to the error I am receiving and can't figure out what I am missing in order to get the logging to go to memory. The necessary information has been included below.In assistance would be greatly appreciated.

log_settings.conf

[loggers]
keys=root

[handlers]
keys=memHandler, consoleHandler

[formatters]
keys=fileFormatter, consoleFormatter

[logger_root]
level=DEBUG
handlers=memHandler, consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=consoleFormatter
args=(sys.stdout,)

[handler_memHandler]
class=handlers.MemoryHandler
formatter=fileFormatter
args=(10, DEBUG, '%(logfilename)s')

[formatter_fileFormatter]
format=[%(asctime)s] [%(levelname)8s] --- (%(filename)15s:%(lineno)4s) %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_consoleFormatter]
format=%(message)s

Error Traceback

Traceback (most recent call last):
  File "E:\BuildScript\proj_tcl\blr_sbz\blr_sbz_build.py", line 22, in <module>
    main(args.jenkins, args.revision, args.jenkins_num)
  File "E:\BuildScript\proj_tcl\blr_sbz\blr_sbz_build.py", line 10, in main
    build(jenkins, revision, jenkins_num)
  File "../scripts\build.py", line 17, in build
    logging.info('Starting the FGPA build script')
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1870, in info
    root.info(msg, *args, **kwargs)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1301, in info
    self._log(INFO, msg, args, **kwargs)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1437, in _log
    self.handle(record)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1447, in handle
    self.callHandlers(record)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 1509, in callHandlers
    hdlr.handle(record)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 858, in handle
    self.emit(record)
  File "C:\Program Files\Python36\lib\logging\handlers.py", line 1213, in emit
    self.flush()
  File "C:\Program Files\Python36\lib\logging\handlers.py", line 1289, in flush
    self.target.handle(record)
AttributeError: 'str' object has no attribute 'handle'

build.py

logging.config.fileConfig('../scripts/logging/log_settings.conf', defaults={'logfilename': '../scripts/logging/log.log'})
logging.info('Starting the FGPA build script')

1 Answer 1

1

Use this configuration file instead:

[loggers]
keys=root

[handlers]
keys=memHandler, consoleHandler

[formatters]
keys=fileFormatter, consoleFormatter

[logger_root]
level=DEBUG
handlers=memHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=consoleFormatter
args=(sys.stdout,)

[handler_memHandler]
class=handlers.MemoryHandler
formatter=fileFormatter
args=(10, DEBUG)
target=consoleHandler

[formatter_fileFormatter]
format=[%(asctime)s] [%(levelname)8s] --- (%(filename)15s:%(lineno)4s) %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_consoleFormatter]
format=%(message)s

Note that you shouldn't include the consoleHandler in the list of handlers for the root logger. Also, you need to set the target using a target= line, and it needs to reference the key for the target handler. Use the following to configure:

logging.config.fileConfig('logging.ini')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.