I am building some data integration flows with Python 2.7 on Windows and want to standardize logging. Right now, I have many python scripts running and logging is specified in each. I would like to create a separate class and import it into each .py script so logging can be standardized.
I am trying to set this up but getting an error in my test. Here my separate logging file: my_logger.py.
import logging
import logging.handlers
def setup_logging():
logging.basicConfig(level = logging.INFO)
logger = logging.getLogger(__name__)
handler = logging.handlers.RotatingFileHandler('C:\\Users\\Documents\\log_test.log', maxBytes=1024000, backupCount=5)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
Here is a small test file (test_logger.py) that is in the same directory as my_logger.py.
import numpy as np
import pandas as pd
import my_logger
my_logger.setup_logging()
def generate_random_data():
logger.info("starting test")
test_df = pd.DataFrame(np.random.randn(5,4), columns=['a','b','c','d'])
print test_df
generate_random_data()
How can I set this up so I can reference separate logging class and set logging messages with a single line?
UPDATE : I removed the class and globals per @tdelaney's suggestion. I don't get the errors anymore and the info message prints in console. The log file also gets created. However, the info message does not show up in the log.
What am I missing to get the info message set in the log file?