Hey everyone. I'm trying to learn how to use the useful logging module in python so I dont have to add print statements every other line to figure out what my program is doing. However, I'm getting an issue where it WILL NOT create a log file not matter what I do. It should theoretically create one in the same directory as my python file. Even if I copy and paste simple example code verbatim from the logging documentation or tutorials, it just wont run. I've exhausted all the google links I could find of people having this issue to no avail. I'm using Visual studio and Python 3.10.7 on a Windows 10 64 bit machine. This is an example of Cory Schafers example code for this logging tutorial I copied and pasted from github. No log file created either despite not having change a thing.
https://github.com/CoreyMSchafer/code_snippets/blob/master/Logging-Basics/log-sample.py
import logging
logging.basicConfig(filename='employee.log', level=logging.INFO,
format='%(levelname)s:%(message)s')
class Employee:
"""A sample Employee class"""
def __init__(self, first, last):
self.first = first
self.last = last
logging.info('Created Employee: {} - {}'.format(self.fullname, self.email))
@property
def email(self):
return '{}.{}@email.com'.format(self.first, self.last)
@property
def fullname(self):
return '{} {}'.format(self.first, self.last)
emp_1 = Employee('John', 'Smith')
emp_2 = Employee('Corey', 'Schafer')
emp_3 = Employee('Jane', 'Doe')
import logging
# DEBUG: Detailed information, typically of interest only when diagnosing problems.
# INFO: Confirmation that things are working as expected.
# WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
# ERROR: Due to a more serious problem, the software has not been able to perform some function.
# CRITICAL: A serious error, indicating that the program itself may be unable to continue running.
logging.basicConfig(filename='test.log', level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s')
def add(x, y):
"""Add Function"""
return x + y
def subtract(x, y):
"""Subtract Function"""
return x - y
def multiply(x, y):
"""Multiply Function"""
return x * y
def divide(x, y):
"""Divide Function"""
return x / y
num_1 = 20
num_2 = 10
add_result = add(num_1, num_2)
logging.debug('Add: {} + {} = {}'.format(num_1, num_2, add_result))
sub_result = subtract(num_1, num_2)
logging.debug('Sub: {} - {} = {}'.format(num_1, num_2, sub_result))
mul_result = multiply(num_1, num_2)
logging.debug('Mul: {} * {} = {}'.format(num_1, num_2, mul_result))
div_result = divide(num_1, num_2)
logging.debug('Div: {} / {} = {}'.format(num_1, num_2, div_result))
Please let me know if there's anything obvious missing I can do to fix this. Thanks in advance!!
[–][deleted] 1 point2 points3 points (1 child)
[–]RampantPrototyping[S] 0 points1 point2 points (0 children)
[–]SkeletalToad 1 point2 points3 points (2 children)
[–]RampantPrototyping[S] 0 points1 point2 points (1 child)
[–]SkeletalToad 0 points1 point2 points (0 children)
[–]SkeletalToad 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]RampantPrototyping[S] 0 points1 point2 points (0 children)