473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Custom log handler and logging.config.fileConfig()

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
0 941

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

Similar topics

12
by: Rob Cranfill | last post by:
Hello, I've successfully coded Python to do what I want with a RotatingFileHandler, but am having trouble getting the same behavior via a config file. I wanted to create one log file each...
0
by: cgmoore | last post by:
I added the Exception Handling Application Block to a project I am working on and I've created a type that implements IExceptionHandler to serve as a custom handler. When I try to set the TypeName...
2
by: Rob | last post by:
I am developing an intranet application, that has to pass interanl security audit. The framework version that I am using is .Net framework version 1.1. Some combinations of text entered in any text...
1
by: Frank S | last post by:
I'm trying to write a custom handler for .rtf files on our website. Using the book "Essential ASP.Net C#" as a guide, I wrote the following and compiled to a dll (assembly named "rtf_cl": ...
1
by: Almad | last post by:
Hi, our applications can have plugins as subpackages and I'd like to allow them to use their own logger as well as it's configuration. I thought that best way will be their own configuration...
1
by: ThunderMusic | last post by:
Hi, I want to use a class (if possible other than Socket) which one I could call a custom handler and receive the redirect code it sends... Actually, I tried using the WebClient class, but when...
1
by: Kenneth Love | last post by:
I have a Python logging config file that contains a RotatingFileHandler handler. In the args key, I have hard-coded the log filename. Everything works great. However, I find that I now need to...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
0
by: Jordan S. | last post by:
Using .NET 3.5... in a "plain old" .aspx page I have the following code in the Init event: this.Context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));...
4
by: Matthew Wilson | last post by:
I'm working on a package that uses the standard library logging module along with a .cfg file. In my code, I use logging.config.fileConfig('/home/matt/mypackage/matt.cfg') to load in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.