4

I'm trying to log into a file maintaining a config file, as the following directory structure with below file contents.

HERE/
|--WORKSPACE/
|   |-- PROJECT/
|   |   |-- project/
|   |   |   |-- confs/
|   |   |   |   |-- __init__.py
|   |   |   |   |-- custom_handler.py
|   |   |   |   |-- log.ini
|   |   |   |-- log.py

log.py:

import os
import logging.config

logging.raiseExceptions = True
curr_dir = os.path.dirname(os.path.realpath(__file__))
CONFIG = os.path.join(curr_dir, 'confs/log.ini')

logging.config.fileConfig(CONFIG)

log.ini:

[loggers]
keys=file

[logger_file]
handlers=file
level=NOTSET

[formatters]
keys=complex

[formatter_complex]
format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s

[handlers]
keys=file

[handler_file]
class=custom_handler.TRFileHandler
interval=W2
backupCount=2
formatter=complex
level=WARNING
args=('project.log',)

custom_handler.py:

import os
from logging.handlers import TimedRotatingFileHandler

curr_dir = os.path.dirname(os.path.realpath(__file__))
parent_dir = os.path.dirname(curr_dir)
LOGS_DIR = os.path.join(parent_dir, 'logs')


class TRFileHandler(TimedRotatingFileHandler):
    def __init__(self, file_name):
        if not os.path.isdir(LOGS_DIR):
            os.makedirs(LOGS_DIR)
        super(TRFileHandler, self).__init__(os.sep.join(LOGS_DIR, file_name))

When I run the following command, I get the accompanying error. It looks like a python path issue. But I'm not sure on this. It works with python files at the 'confs' directory level.

~HERE$ python WORKSPACE/PROJECT/project/log.py

Traceback (most recent call last):
  File "WORKSPACE/PROJECT/project/log.py", line 8, in <module>
    logging.config.fileConfig(CONFIG)
  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 custom_handler

1 Answer 1

2

Your sys.path needs to contain the project/confs directory, otherwise you won't be able to import the custom_handler module. Try again after ensuring that is the case.

Update: I'm not sure log.py is the place to set your path. There is no one right way to do this - there are various tutorials for how to set up Python projects.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. It does solve the problem. But, I've one more doubt related to it. Which would be the proper way to do this, given there could be multiple such packages or directories? Can we use init.py for this. Currently I've added the following in log.py: sys.path.append(os.path.dirname(CONFIG))

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.