logging..in python..
i am trying to do logging with python code using a ".ini" file..i get an error saying "NO SECTION "FORMATTERS"" I have googled they tell mi my file might be not well configured..i have checked and i think its okay..am really stack somrewhere...pliz help mi anyone thank you.
@Tariiq: Do you have a section for the formatters in your INI file? Can you show a minimal example that gives you this error?
By the way am looking for a way to upload the code..how do i do it...?
Code: Alles auswählen
The error is:this is my code
Code: Alles auswählen
Traceback (most recent call last):
File "/Users/Genius/Documents/loggin.py/HBInsect.py", line 3, in <module>
class HBInsect(object):
File "/Users/Genius/Documents/loggin.py/HBInsect.py", line 4, in HBInsect
logging.config.fileConfig("logi.ini")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 70, in fileConfig
formatters = _create_formatters(cp)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 106, in _create_formatters
flist = cp.get("formatters", "keys")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ConfigParser.py", line 607, in get
raise NoSectionError(section)
NoSectionError: No section: 'formatters'
Code: Alles auswählen
import logging
import logging.config
class HBInsect(object):
logging.config.fileConfig("logi.ini")
def __init__(self):
self.logger=logging.getLogger("henry")
def HBmault(self):
print("an insect maults by shedding off its wings")
self.logger.debug("debug message")
self.logger.info("info message")
self.logger.warn("warn message")
self.logger.error("error message")
self.logger.critical("critical message")
def HBmove(self):
print ("an insect moves by scrabbing wings")
HBInsect().HBmault()
Code: Alles auswählen
[loggers]
keys=root,henry
[handlers]
keys=consoleHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_henry]
level=DEBUG
handlers=consoleHandler
qualname=Henry
propagate=0
[handler_consoleHandler]
class=streamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
@Tariiq: I get a completely different error:
Which comes from the ``class`` key in the ``[handler_consoleHandler]`` section where the value ``streamHandler`` has to start with a capital 'S' to match the `logging.StreamHandler`\s class name. Then the output looks like this:
I guess the leading and trailing ``'`` is not wanted, so you may remove them from the value in the INI file.
Other remarks about the code: Convention is four spaces per indentation level. And method names usually don't contain capital letters. Have a look at the Style Guide for Python Code.
Abbreviation shoudn't be used unless they are common. Otherwise the reader starts to guess what the `HB` prefix might stand for. At the methods that prefix seems odd anyway.
I wouldn't do the `fileConfig()` call within the class definition. It has no other meaning there compared to module level so it raises the question why it is in the class definition anyway.
And as you said you come from Java, here is a small advise based on the file name: In Python it is not common to have one file per class. A file in Python describes a module and modules are for grouping functions and classes. Think of a module, and therefore of a file, as something like a leaf package in Java.
Code: Alles auswählen
$ python forum5.py
Traceback (most recent call last):
File "forum5.py", line 3, in <module>
class HBInsect(object):
File "forum5.py", line 4, in HBInsect
logging.config.fileConfig("logi.ini")
File "/usr/lib/python2.7/logging/config.py", line 78, in fileConfig
handlers = _install_handlers(cp, formatters)
File "/usr/lib/python2.7/logging/config.py", line 153, in _install_handlers
klass = _resolve(klass)
File "/usr/lib/python2.7/logging/config.py", line 88, in _resolve
found = __import__(used)
ImportError: No module named streamHandler
Code: Alles auswählen
$ python forum5.py
an insect maults by shedding off its wings
'2014-05-18 14:21:35,632 - henry - DEBUG - debug message'
'2014-05-18 14:21:35,632 - henry - INFO - info message'
'2014-05-18 14:21:35,632 - henry - WARNING - warn message'
'2014-05-18 14:21:35,632 - henry - ERROR - error message'
'2014-05-18 14:21:35,633 - henry - CRITICAL - critical message'
Other remarks about the code: Convention is four spaces per indentation level. And method names usually don't contain capital letters. Have a look at the Style Guide for Python Code.
Abbreviation shoudn't be used unless they are common. Otherwise the reader starts to guess what the `HB` prefix might stand for. At the methods that prefix seems odd anyway.
I wouldn't do the `fileConfig()` call within the class definition. It has no other meaning there compared to module level so it raises the question why it is in the class definition anyway.
And as you said you come from Java, here is a small advise based on the file name: In Python it is not common to have one file per class. A file in Python describes a module and modules are for grouping functions and classes. Think of a module, and therefore of a file, as something like a leaf package in Java.