Logging basicConfig not creating log file when i run in pycharm?

前端 未结 6 513
醉话见心
醉话见心 2020-12-29 03:34

When i run below code in terminal its create a log file

import logging 
logging.basicConfig(filename=\'ramexample.log\',level=logging.DEBUG)
logging.debug(\         


        
相关标签:
6条回答
  • 2020-12-29 04:12

    Maximas is right. File path is relative to execution environment. However instead of writing down the absolute path you could try the dynamic path resolution approach:

    filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ram.log')
    logging.basicConfig(filename=filename, level=logging.DEBUG)
    

    This assumes that ram.log resides in the same directory with the one that contains the above code (that's why __file__ is used for).

    0 讨论(0)
  • 2020-12-29 04:12
    import logging
    
    class LogGen:
        @staticmethod
        def loggen():
            logger = logging.getLogger()
            fhandler = logging.FileHandler(filename='.\\logs\\automation.log', mode='a')
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            fhandler.setFormatter(formatter)
            logger.addHandler(fhandler)
            logger.setLevel(logging.INFO)
            return logger
    
    0 讨论(0)
  • 2020-12-29 04:21

    I can't remember where I got this otherwise I would have provided a link. But had the same problem some time ago using in jupyter notebooks and this fixed it:

    import logging
    logger = logging.getLogger()
    fhandler = logging.FileHandler(filename='mylog.log', mode='a')
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fhandler.setFormatter(formatter)
    logger.addHandler(fhandler)
    logger.setLevel(logging.DEBUG)
    
    0 讨论(0)
  • 2020-12-29 04:22

    The answer why this error happens is this:

    The call to basicConfig() should come before any calls to debug(), info() etc.

    If you do so the basicConfig can not create and write a new file. Here I called logging.info() right before logging.basicConfig().

    Don't:

    import logging
    logging.info("root") # call to info too early
    logging.basicConfig(filename="rec/test.log", level=logging.DEBUG) # no file created
    
    0 讨论(0)
  • 2020-12-29 04:26

    I encountered same issue and found none of the answers previously provided here would work. Maybe this issue had been solved long ago to Ramnath Reddy, but I could not find the correct answer anywhere online.

    Luckily, I found a solution from a colleague's code by adding the following lines before logging.basicConfig().

    Remove all handlers associated with the root logger object.

    for handler in logging.root.handlers[:]:
        logging.root.removeHandler(handler)
    

    Try and see if it helps for whomever had the same issue.

    0 讨论(0)
  • 2020-12-29 04:28

    This does create a log within the pycharm terminal using the Py terminal within it. You need to check the location of where the terminal is (try dir on Windows or pwd on linux/mac). Instead of just putting in ram.log, use the full file path of where you would like the file to appear. E.G.

    logging.basicConfig(filename='/Users/Donkey/Test/ram.log', level=logging.DEBUG)
    
    0 讨论(0)
提交回复
热议问题