THun
THun

Reputation: 11

How can I fix the error in logging (config file)

I'm facing an error and I don't know why.. (The python file and the config file are in the same location.)

The python file is as follows

import logging
import logging.config

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

logger = logging.getLogger(__name__)

logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')

The config file is as follows

[logger_root]
level = NOTSET
handlers = 

[logger_infoLogger]
level = INFO
handlers = simpleHandler
qualname = __main__
propagate = 1


[handler_simpleHandler]
class = StreamHandler
formatter = simpleFormatter
args = (sys.stdout,)


[formatter_simpleFormatter]
format = %(asctime)s - %(time)s - %(levelname)-8s - %(message)s
datefmt = 

The error is as follows

Traceback (most recent call last):
  File "D:/Python/logging.py", line 1, in <module>
    import logging
  File "D:/Python/logging.py", line 2, in <module>
    import logging.config
ModuleNotFoundError: No module named 'logging.config'; 'logging' is not a package

Upvotes: 1

Views: 1429

Answers (2)

Sibin Thomas Qu4d
Sibin Thomas Qu4d

Reputation: 1

try by replacing the first code to this and rename the file logging.conf

from logging import *
logging.config.fileConfig('logging.conf')
logger = logging.getLogger(__name__)
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')

Upvotes: 0

Torben Klein
Torben Klein

Reputation: 3116

Your script file is named logging.py, which makes it shadow the builtin logging module. Renaming your script file should do.

Upvotes: 1

Related Questions