I am using Anaconda and Spyder as my python environment and editor. I just learnt some very basic way to log simple print information to a log file. But it seems quite unpythonic. What I have done is down below:
import numpy as np
import pandas as pd
import logging
logger = logging.getLogger('test2')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh = logging.FileHandler('test2.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
ch.setFormatter(formatter)
logger.addHandler(ch)
X = np.array([1,2,3])
print(X)
logger.info(f'{X}')
str1 = 'hello world'
print(str1)
logger.info(str1)
df = pd.DataFrame({'col1': np.arange(40),
'col2': np.tile([0,1,2,3],10)
})
print(df)
logger.info(f'{df}')
Basically whenever I feel the need to print something, I have to add one extra line of code to log it. It may not seem like a problem in this example, but if I have to print 500+ lines of stuff in a super long script, it can be very inefficient.
My questions are the following:
1) Is there any workaround with a few lines of code so that whenever I print something to the Spyder console it will be logged to the external file? I'd like to see how it is done.
2) If the python script is run by first reading it from an external txt file and execute it, does this make logging any different than the case where the script is run normally?
3) Since I am using Anaconda as Python environment and Spyder as IDE, does this make logging any different than using other python editor and other Python environment?
Thank you in advance
EDIT1: I tried using the following code
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
but unfortunately whatever printed on Spyder console does not get copied to the external file. Maybe I have missed something.
import numpy as np
import pandas as pd
import logging
import sys
logger = logging.getLogger('test3')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh = logging.FileHandler('test3.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
X = np.array([1,2,3])
print(X)
str1 = 'hello world'
print(str1)
df = pd.DataFrame({'col1': np.arange(40),
'col2': np.tile([0,1,2,3],10)
})
print(df)