Reputation: 321
Problem is when i use python from command "from car_logger import log_warning, log_critical, log_error, clear_logs" and execute function log_warning("THIS IS WARNING") , then it works fine i.e log get written to log file and message get displayed on screen due to print command .
But if i am using it via test.py then it print he output on screen as mentioned in function but it does not log warning message in log file ?
Where is mistake?
Here is car_logger.py
import logging
from os import remove , path
log_file = "/tmp/robocar_error.log"
my_formatter = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
# Create a custom logger
logger = logging.getLogger(__name__)
# Create handlers
f_handler = logging.FileHandler(log_file)
f_handler.setLevel(logging.WARNING)
# Create formatters and add it to handlers
f_format = logging.Formatter(my_formatter)
f_handler.setFormatter(f_format)
# Add handlers to the logger
logger.addHandler(f_handler)
def log_warning(message):
print(message)
logger.warning(message)
def log_critical(message):
print(message)
logger.critical(message)
def log_error(message):
print(message)
logger.error(message)
And here is test.py
from car_logger import log_warning, log_critical, log_error, clear_logs
log_warning("THIS IS WARNING")
Upvotes: 0
Views: 439
Reputation: 3900
From line 1 of test.py: clear_logs
is not defined.
I removed it from test.py and tested: it works fine for me.
Upvotes: 1