1
 logging.basicConfig( filename=f"{output_location}/log.txt", format="{asctime} - {levelname} - {filename}: {message}", datefmt = "%d %b %H:%M", style="{", level=logging.INFO ) 

This is my code and it used to work just fine but after I changed a line of code it stopped working. Outcome before it stopped working:

  1. Have the updated logging format as specified by the format parameter.
  2. Have the file log.txt created with the logs in it.

Now the logs look like this: WARNING:root when the format parameter says otherwise. In additon the log.txt file is not created.

The line of code I changed is output_location = os.getcwd() + r"/../output" where it is changed from r"/output" to r"/../output". This line of code is in this function:

def create_output_dir() -> Path: """Creates the output directory where all the log files and user facing sheets will go.""" output_location = os.getcwd() + r"/output" path = Path(output_location) if not path.exists(): msg = "A folder named 'output' has been created. All files generated will go there." print(msg) logging.info(msg) path.mkdir() return path 

After I reverted the code to remove the /.. part the logging format stays as the default which is WARNING:root. I'm not entirely sure what happened but it just stopped working out of nowhere.

1
  • You didn't provide a MRE, but it could be related to the fact that basicConfig() only applies the config if there is no other logging config yet. Commented Sep 3, 2024 at 6:22

1 Answer 1

0

Turns out the answer was in this post. I was also warned here.

Note: Calling basicConfig() to configure the root logger only works if the root logger hasn’t been configured before. All logging functions automatically call basicConfig() without arguments if basicConfig() has never been called. So, for example, once you call logging.debug(), you’ll no longer be able to configure the root logger with basicConfig().

From RealPython. Turns out I was just really careless.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.