3

To Python Experts:

I'm new to programming and learning logging python package.

According to logging document, I set the format of my logging message as follows:

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

Here is the output of logging message:

2016-08-09 19:57:08,363 - DEBUG -             A   B
2016-01-31  0  12
2016-02-29 -1  12

How to make A B (the column names of the datafram) to the next line?:

Also, would you please help me understand %(asctime)s - %(levelname)s - %(message)s? why put % before bracket and s after bracket?

2016-08-09 19:57:08,363 - DEBUG -  
                A   B
    2016-01-31  0  12
    2016-02-29 -1  12
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lisa
  • 4,126
  • 12
  • 42
  • 71

1 Answers1

3

For your first question:

Just add an extra newline to your call of logging.debug:

For example, if your logger is called logger, and you are logging the message through logging.debug(msg), you can call logging.debug('\n' + msg) to move the message onto the newline.

As for the format: the % indicates that the upcoming text should be parsed as a format specifier. The parentheses, (), surround the name of the specifier, and the trailing s converts the input data into a string. See: difference between str.format and %.

So, %(name)s allows you to specify name which will be converted to a string. The different names, asctime, levelname, message are specified by the logging framework.

Graham
  • 7,431
  • 18
  • 59
  • 84
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
  • Thanks. What if my msg is a DataFrame? df = pd.DataFrame({'A': [0, -1, 3, 5, 4, 2, 1], 'B': [12, 12, 14, 15, 14, 16, 200]}) logging.debug('\n'+ df) doesn't work..@Rushy Panchal – Lisa Aug 10 '16 at 04:14
  • @Lisa You can try using `str(df)`, which will convert it to a string first. However, the representation of that is defined by the DataFrame which may not be what you want. – Rushy Panchal Aug 12 '16 at 04:29