logging..in python..

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Tariiq
User
Beiträge: 12
Registriert: Freitag 2. Mai 2014, 07:33

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.
BlackJack

@Tariiq: Do you have a section for the formatters in your INI file? Can you show a minimal example that gives you this error?
Tariiq
User
Beiträge: 12
Registriert: Freitag 2. Mai 2014, 07:33

i do have it there...i swear let mi try to upload the file if i can
Tariiq
User
Beiträge: 12
Registriert: Freitag 2. Mai 2014, 07:33

By the way am looking for a way to upload the code..how do i do it...?
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

You just copy-paste the code here, inside of the code tags.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Tariiq
User
Beiträge: 12
Registriert: Freitag 2. Mai 2014, 07:33

The error is:

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()
this is my code

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'

BlackJack

@Tariiq: I get a completely different error:

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
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:

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'
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.
Tariiq
User
Beiträge: 12
Registriert: Freitag 2. Mai 2014, 07:33

:lol: thanx alot BlackJack thnx let mi try to correct it.. :mrgreen:
Antworten