Python 3 Script to Add Logging to Stdout Using logging Module Full Project For Beginners

 

 

In order to get started you need to install the below library using the pip command as shown below

 

 

pip install logging

 

 

After installing this library you need to make an app.py file and copy paste the following code

See also  Python 3 Script to Get Realtime Price of Mutual Funds Using mftool Library Full Project For Beginners

 

 

app.py

 

 

import logging

def main():
    # Configure the logging system
    logging.basicConfig(filename ='app.log',
                        level = logging.ERROR)
    
    # Variables (to make the calls that follow work)
    hostname = 'www.python.org'
    item = 'spam'
    filename = 'data.csv'
    mode = 'r'
    
    # Example logging calls (insert into your program)
    logging.critical('Host %s unknown', hostname)
    logging.error("Couldn't find %r", item)
    logging.warning('Feature is deprecated')
    logging.info('Opening file %r, mode = %r', filename, mode)
    logging.debug('Got here')
    
if __name__ == '__main__':
main()

Leave a Reply