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

Custom log handler and logging.config.fileConfig()

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 and and in the same directory. As soon as I move my and in a different directory, my code starts throwing following error.
************************************************** **********
****************** Error ***********************************
Traceback (most recent call last):
File "C:\Python\Python36\lib\logging\", 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\", line 153, in <module>
modules = [loadSource(a[0])]
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2\helpers\pycharm\", line 65, in loadSource
module = imp.load_source(moduleName, fileName)
File "C:\Python\Python36\lib\", 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\", line 9, in <module>
from Selunium_Automation.WebPages.Pages import Pages
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\WebPages\", line 1, in <module>
from Selunium_Automation.Locators.HomePgLocator import *
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Locators\", line 1, in <module>
from Selunium_Automation.Lib.PageElements import PageElements
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Lib\", line 3, in <module>
from Selunium_Automation.Utilities.WebDriverSetup import Web
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Utilities\", line 1, in <module>
from Selunium_Automation.Utilities.WebDriverFactory import WebDriverFactory
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\Utilities\", line 3, in <module>
from Selunium_Automation.LogConfig.LogConfig import Logger
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\LogConfig\", line 16, in <module>
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\LogConfig\", line 13, in singleton
return get_instance()
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\LogConfig\", line 10, in get_instance
instances[cls] = cls()
File "C:\Users\bilal\workspace\PycharmProjects\Selunium _Automation\LogConfig\", line 20, in __init__
logging.config.fileConfig(log_file_path, disable_existing_loggers=False)
File "C:\Python\Python36\lib\logging\", line 84, in fileConfig
handlers = _install_handlers(cp, formatters)
File "C:\Python\Python36\lib\logging\", line 145, in _install_handlers
klass = _resolve(klass)
File "C:\Python\Python36\lib\logging\", line 94, in _resolve
found = __import__(used)
ImportError: No module named 'MyFileHandler'
#Here is MyFileHandler.

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

************************************************** **********Config File:

keys=fileHandler, consoleHandler





args=('Automation','./', 'a')

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

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

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

return get_instance()

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 for a faster answer!
Share on Google+

Post your reply

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