Python Forum
Logging in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Logging in python (/thread-14040.html)



Logging in python - saisankalpj - Nov-12-2018

I have a logger method like as follows
import logging
def log():
    logger = logging.getLogger("exampleApp")
    logger.setLevel(logging.INFO)
 
    # create the logging file handler
    fh = logging.FileHandler("new_snake.log")
 
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
 
    # add handler to logger object
    logger.addHandler(fh)
     return logger
Now i have a
def abc():
   Import log
   logger=log()
   logger.info(“hi”)
   
and
def food():
   Import abc
   abc()
   import log
   logger=log()
   logger.info(“hi”)
Now when i am call def food which in turn calls abc
The log of abc gets executed once while every log in food gets executed twice


RE: Logging in python - ichabod801 - Nov-12-2018

Your code is completely non-functional. It has syntax errors (Import), import errors (and import at the top of the file, not in the function body), indentation errors (the return in the logger). Please give us functional code so we can get to the error you are actually having.