Python Forum

Full Version: Logging in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.