analyticsPierce
analyticsPierce

Reputation: 3025

How to set logging with RotatingFileHandler?

I have an analytics routine (python 2.7) that I am adding logging to. My goal is to add INFO messages to the start and end of each function so I can see how long each is taking. What I have below is a simplified version. When I run this example, nothing writes to the log file I specify. I would like to get the messages with time stamps writing to that file.

import numpy as np
import pandas as pd
import logging
import logging.handlers

def perform_process():
    setup_logging()
    baseline()
    calculations()

def setup_logging():
    logging.basicConfig(level = logging.INFO)
    global logger
    global handler
    global formatter
    logger = logging.getLogger(__name__)
    handler = logging.handlers.RotatingFileHandler('C:\\Users\\perform_log.log', maxBytes=2000, backupCount=5)
    handler.setLevel(logging.info)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.info('I am logging')

def baseline():
    logger.info('start baseline function')
    baseline_df = pd.DataFrame(np.random.randn(10,4), columns=['a','b','c','d'])
    global output_df
    output_df = baseline_df

def calculations():
    print output_df.head()

perform_process()

When I run this file, I get this output in the console:

          a         b         c         d
0 -0.686909  0.279976  0.219521 -0.027359
1 -0.718949  0.714682  1.202500  0.935868
2  0.454883  1.205500  0.079626 -1.370491
3 -0.743507 -1.353939  0.677011 -0.847376
4 -0.464742  1.034433 -0.779324  0.930626

[5 rows x 4 columns]
INFO:__main__:I am logging
INFO:__main__:start baseline function

However, no data appears in the log file I specified. What is wrong with my logging setup to prevent messages from being written with the format I specified to the log file I specified?

Regarding the output above, why is 'print output_df.head()' executed before the two logging calls? I would like to see the log messages in order of execution.

Upvotes: 2

Views: 1177

Answers (1)

Ashoka Lella
Ashoka Lella

Reputation: 6729

In this line

handler.setLevel(logging.info)

logging.info returns a function not a level. You'll have to correct that to logging.INFO (info is capitalized)

Upvotes: 2

Related Questions