Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ich sende erneut - der Code war nicht klar- python - loggin.conf

34 views
Skip to first unread message

Linnea G

unread,
Jul 4, 2022, 7:15:47 AM7/4/22
to
Guten Tag
Wenn möglich habe ich eine Frage:
Das ist mein Code:

mainbcdbcd.py

-------------------------------------------------------------------------------------
import logging
import logging.config

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

logger=logging.getLogger('simpleExample')

#create handler
stream_h=logging.StreamHandler()
file_h=logging.FileHandler('file.log')

#level and the format
stream_h.setLevel(logging.WARNING)
file_h.setLevel(logging.ERROR)

formatter=logging.Formatter('%(name)s - %(levelname)s - %(message)s')
stream_h.setFormatter(formatter)
file_h.setFormatter(formatter)

logger.addHandler(stream_h)
logger.addHandler(file_h)

logger.warning('this is a warning')
logger.error('this is an error')
-------------------------------------------------------------------------------------

loggin.conf:

-------------------------------------------------------------------------------------
[logger]
keys=root,simpleExample

[handler]
keys=consoleHandler

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0


[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format='%(asctime)s - %(name)s %(levelname)s - %(message)s'
-------------------------------------------------------------------------------------

Als ich lief, bekam ich diesen Fehler:

-------------------------------------------------------------------------------------
C:\Users\User\python_logs\venv\Scripts\python.exe C:/Users/User/python_logs/venv/bcdbcd.py
Traceback (most recent call last):
File "C:/Users/User/python_logs/venv/bcdbcd.py", line 3, in <module>
import mainbcdbcd
File "C:\Users\User\python_logs\venv\mainbcdbcd.py", line 4, in <module>
logging.config.fileConfig('loggin.conf')
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\logging\config.py", line 71, in fileConfig
formatters = _create_formatters(cp)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\logging\config.py", line 104, in _create_formatters
flist = cp["formatters"]["keys"]
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\configparser.py", line 958, in __getitem__
raise KeyError(key)
KeyError: 'formatters'
-------------------------------------------------------------------------------------

Process finished with exit code 1
Ich habe alle in diesen Links beschriebenen Lösungen ausprobiert:
https://stackoverflow.com/questions/23161745/python-logging-file-config-keyerror-formatters
https://stackoverflow.com/questions/47636747/python3-6-keyerror-formatters
https://stackoverflow.com/questions/29346678/python-3-2-logging-with-config-file-results-in-keyerror-formatters-on-raspbia
https://stackoverflow.com/questions/42209122/python-fileconfig-issue
https://stackoverflow.com/questions/8912708/python-logging-keyerror-when-loading-fileconfig

Wenn jemand weiß, was die Lösung sein könnte, wird es mir sehr helfen.
danke,
Linnea

Massa, Harald Armin

unread,
Jul 4, 2022, 7:29:24 AM7/4/22
to
Hi Linnea,

-------------------------------------------------------------------------------------
>
> loggin.conf:
>
>
> -------------------------------------------------------------------------------------
> [handler_consoleHandler]
> class=StreamHandler
> level=DEBUG
> * formatter*=simpleFormatter
> args=(sys.stdout,)
>
> [*formatter_simpleFormatter*]
> format='%(asctime)s - %(name)s %(levelname)s - %(message)s'
>
> -------------------------------------------------------------------------------------
>
> Als ich lief, bekam ich diesen Fehler:
>
>
> -------------------------------------------------------------------------------------
> flist = cp["formatters"]["keys"]
> File "C:\Program Files (x86)\Microsoft Visual
> Studio\Shared\Python37_64\lib\configparser.py", line 958, in __getitem__
> raise KeyError(key)
> KeyError: 'formatters'
>
> -------------------------------------------------------------------------------------
>

configparser erwartet einen Eintrag mit dem Schlüssel "formatters".
In Deiner Config sehe ich nur "formatter" und "formatter_simpleFormatter"


Wenn Du mal schaust unter
https://docs.python.org/3/library/logging.config.html

Abschnitt ConfigurationFileFormat

dort werden Sectionen, [formatters], [loggers] und [handlers] gefordert -
die sehe ich in Deiner config nicht

(quote von jener Seite)

[loggers]keys=root,log02,log03,log04,log05,log06,log07
[handlers]keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09
[formatters]keys=form01,form02,form03,form04,form05,form06,form07,form08,form09









> _______________________________________________
> python-de Mailingliste -- pyth...@python.org
> Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an
> python-...@python.org
> https://mail.python.org/mailman3/lists/python-de.python.org/
> Mitgliedsadresse: ch...@ghum.de
>


--

GHUM GmbH
Harald Armin Massa
Spielberger Straße 49
70435 Stuttgart
0173/9409607

Amtsgericht Stuttgart, HRB 734971
GF: Harald Armin Massa

Linnea G

unread,
Jul 4, 2022, 8:26:29 AM7/4/22
to
Vielen Dank!!
Das war die Lösung!!
0 new messages