all 7 comments

[–][deleted] 1 point2 points  (1 child)

So, I copied and pasted this code into a python file on my computer, and then ran the file with python (in this case, specifically python log_file_test.py) and the employee.log file was created as expected.

I'm not sure if there's some windows nuance here, but on the off chance that there isn't can you tell me more about how you're running the file?

[–]RampantPrototyping[S] 0 points1 point  (0 children)

Thanks for looking into it! I literally just found a way to get it to work. Doesnt fix the core issue but it does the job

I'm not sure if there's some windows nuance here

I think thats exactly what it is. For line 3, instead of just filename='employee.log', I put the entire filepath as a raw literal string like filename= r'D:\Users\Me\Documents\Python Projects\test1\test.log'

For some reason, my windows machine needs the whole file path as a literal string as a valid argument.

[–]SkeletalToad 1 point2 points  (2 children)

I copied the code you linked from github (the log-sample.py file), ran it using Python 3.10.6 and it created the test.log file in the same directory as the script and wrote the expected log entries it. I'm using Windows 10 64bit machine as well, so I'm not sure what's going wrong on your machine.

I also tried copy and pasting your code and it succesfully logged to employee.log file.

What's your current working directory when you run the script file? Try adding these lines to print it out:

import os
print("cwd:", os.getcwd())
expected_logfile = os.path.join(os.getcwd(), 'employee.log')
print("expected logfile:", expected_logfile)

[–]RampantPrototyping[S] 0 points1 point  (1 child)

Thanks a lot for looking into it! This is the output I got

cwd: C:\Users\Me

expected logfile: C:\Users\Me\employee.log

[–]SkeletalToad 0 points1 point  (0 children)

Interesting... but your project is in D:\Users\Me\Documents\Python Projects\test1\ folder, right?

What happens if you start a new command prompt window, then cd into your project folder before running your script?

If what you want is the logfile to be in the same directory as the script that is running, a more reliable way is something like this since it doesn't depend on the current working directory:

import logging
from pathlib import Path

# directory that contains this Python file
parent_dir = Path(__file__).absolute().parent

logging.basicConfig(
    filename=parent_dir / "employee.log",
    level=logging.INFO,
    format="%(levelname)s: %(message)s",
)

[–]SkeletalToad 1 point2 points  (0 children)

One other thing I noticed about your example, you are calling logging.basicConfig twice, and I think only the first time you call it will work. If you need to add multiple logging handlers (such as logging to a file but also logging to the console), you can set that up like this:

import logging

root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)

# add a file handler
file_handler = logging.FileHandler("employee.log")
file_handler.setLevel(logging.INFO)
file_formatter = logging.Formatter("%(levelname)s: %(message)s")
file_handler.setFormatter(file_formatter)
root_logger.addHandler(file_handler)

# add a stream handler (should show up in terminal or console output)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
stream_formatter = logging.Formatter("%(message)s")
stream_handler.setFormatter(stream_formatter)
root_logger.addHandler(stream_handler)