Python logging module (write log file) / basicConfig does not create log file issue(solved)

Python

logging module : write log file for exec. log in python(i think)

issue :

If "fullpath = variable + filename" as shown above, there is an issue(not create a log file).
The cause is unknown, but if " fullpath = "./log/test.log" " directly, you can see that the file is normally generated.


example 1 and issue of example 1,  solved: example 2


example 1.

import logging

def writeLogFile(logText):
    mypath = "./log"
    fullpath = mypath + "/test.log"    #mypath = "./log"
    logging.basicConfig(filename = fullpath, level=logging.DEBUG)
    logging.debug(logText)

If "fullpath = variable + filename" as shown above, there is an issue(not create a log file).

The cause is unknown, but if " fullpath = "./log/test.log" " directly, you can see that the file is normally generated.

For the above problem, I don't use basicConfig, So, I use it with example 2.


example 2 and solve.

import logging
import logging.handlers

log : ""

def initLog():
    global log
    mypath = "./log"
    fullpath = mypath + "/test.log"    #mypath = "./log"

    log = logging.getLogger('my_log')
    log.setLevel(logging.DEBUG)
    fileHandler = logging.FileHandler(fullpath)
    log.addHandler(fileHandler)


def writeLogFile(logText):
    log.debug(logText)

As shown above, log can be created as a file without any problems.


#python #logging #log #logfile #basicConfig #issue #solve #logginghandler


No comments:

Post a Comment

Apartment Buying Guide 2025: Shocking Red Flags You Should NEVER Ignore!

 🏙️ Apartment Buying Guide 2025: Shocking Red Flags You Should NEVER Ignore! 🚨 Are you thinking about buying an apartment in 2025? 🏢  It’...