By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Python Custom FileHandler in logging.conf

P: 3
I have added a custom File handler to create a log file append with date and time in a specified directory. Everything works fine if I have my main.py and logconfig.py and MyFileHandler.py in the same directory. As soon as I move my logconfig.py and MyFileHandler.py in a different directory, my code starts throwing following error.
************************************************** **********
****************** Error ***********************************
Traceback (most recent call last):
File "C:\Python\Python36\lib\logging\config.py", line 143, in _install_handlers
klass = eval(klass, vars(logging))
File "<string>", line 1, in <module>
NameError: name 'MyFileHandler' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2\helpers\pycharm\utrunner.py", line 153, in <module>
modules = [loadSource(a[0])]
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2\helpers\pycharm\utrunner.py", line 65, in loadSource
module = imp.load_source(moduleName, fileName)
File "C:\Python\Python36\lib\imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 656, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Tests\SmokeTest.py", line 9, in <module>
from Selunium_Automation.WebPages.Pages import Pages
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\WebPages\Pages.py", line 1, in <module>
from Selunium_Automation.Locators.HomePgLocator import *
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Locators\HomePgLocator.py", line 1, in <module>
from Selunium_Automation.Lib.PageElements import PageElements
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Lib\PageElements.py", line 3, in <module>
from Selunium_Automation.Utilities.WebDriverSetup import Web
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Utilities\WebDriverSetup.py", line 1, in <module>
from Selunium_Automation.Utilities.WebDriverFactory import WebDriverFactory
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Utilities\WebDriverFactory.py", line 3, in <module>
from Selunium_Automation.LogConfig.LogConfig import Logger
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\LogConfig\LogConfig.py", line 16, in <module>
@singleton
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\LogConfig\LogConfig.py", line 13, in singleton
return get_instance()
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\LogConfig\LogConfig.py", line 10, in get_instance
instances[cls] = cls()
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\LogConfig\LogConfig.py", line 20, in __init__
logging.config.fileConfig(log_file_path, disable_existing_loggers=False)
File "C:\Python\Python36\lib\logging\config.py", line 84, in fileConfig
handlers = _install_handlers(cp, formatters)
File "C:\Python\Python36\lib\logging\config.py", line 145, in _install_handlers
klass = _resolve(klass)
File "C:\Python\Python36\lib\logging\config.py", line 94, in _resolve
found = __import__(used)
ImportError: No module named 'MyFileHandler'
-----------------------------------------------------------
#Here is MyFileHandler.

import logging
************************************************** **********
****************MyFileHandler.py **************************
# import random
# import os
from datetime import datetime
class MyFileHandler(logging.FileHandler):
def __init__(self, filename, path, mode):
self.filename = datetime.now().strftime(filename + '%H_%M_%d_%m_%Y.log')
super(MyFileHandler, self).__init__(path + "/" + self.filename, mode)

************************************************** **********Config File:
[loggers]
keys=root,exampleApp

[handlers]
keys=fileHandler, consoleHandler

[formatters]
keys=myFormatter

[logger_root]
level=CRITICAL
handlers=consoleHandler

[logger_exampleApp]
level=DEBUG
handlers=fileHandler
qualname=exampleApp

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

[handler_fileHandler]
class=MyFileHandler.MyFileHandler
formatter=myFormatter
args=('Automation','./', 'a')

[formatter_myFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - [%(filename)s:%(funcName)10s()] %(message)s"
datefmt=

below is the function I am using to call logger in other modules
import logging.config
from os import path
************************************************** *********
**********************LogConfig.py**************** *********
def singleton(cls):
instances = {}

def get_instance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]

return get_instance()


@singleton
class Logger:
def __init__(self):
log_file_path = path.join(path.dirname(path.abspath(__file__)), 'log.conf')
logging.config.fileConfig(log_file_path, disable_existing_loggers=False)
self.logr = logging.getLogger('exampleApp')

-----------------------
Any help would be greatly appreciated?
Aug 25 '16 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 613
Python has a specific search path stored in a list. You can see it with
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. print sys.path
  3. ##
  4. ## You can add other directories to be searched with
  5. sys.path.append("/the/new/directory") 
Sep 19 '16 #2

P: 3
@dwblas

Can you please be specific as I am new to Python
Sep 20 '16 #3

Expert 100+
P: 613
There is no way to tell what "specific" means as the original answer was specific, i.e. everything you need to do. Please post a simple example of what you have tried to add the directory where you moved the programs to the Python search path.
Sep 20 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.