The document explains Python's logging module, which allows for flexible logging at various levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) and can be customized for output to the console or files. It also describes Python's automatic memory management through reference counting and garbage collection, which helps manage memory allocation and prevent leaks. Overall, it provides an overview of how to implement logging and understand memory management in Python.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views9 pages
Logging
The document explains Python's logging module, which allows for flexible logging at various levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) and can be customized for output to the console or files. It also describes Python's automatic memory management through reference counting and garbage collection, which helps manage memory allocation and prevent leaks. Overall, it provides an overview of how to implement logging and understand memory management in Python.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Logging
• In Python, the logging module provides a flexible and powerful
framework for emitting log messages from programs. • It follows the log levels defined in the syslog standard (DEBUG, INFO, WARNING, ERROR, and CRITICAL). • DEBUG: Detailed information, typically useful for debugging. Example: logging.debug('This is a debug message’) • INFO: General information about the program's progress. Example: logging.info('This is an info message') • WARNING: Indicates a potential issue or something unexpected that might need attention. Example: logging.warning('This is a warning message’) ERROR: Indicates a more serious issue or error that occurred in the program. Example: logging.error('This is an error message’) CRITICAL: Indicates a critical error that may lead to the program's termination. Example: logging.critical('This is a critical message') • import logging • The configuration can be customized according to your needs. If not configured, the default settings are used. • logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - % (levelname)s - %(message)s’) • import logging • logging.basicConfig(format='%(asctime)s %(message)s') • logger=logging.getLogger() # creating an object • logger.setLevel(logging.DEBUG) • logger.debug('hi friends') • logger.info("small information") • logger.warning("I am giving warning") • logger.error("i am an error") • logger.critical("Internet is down") configure log file in over writing mode • If you want to configure the logging module to write log messages to a file in overwriting mode (where the log file is overwritten each time the program runs), you can use the FileHandler with the mode parameter set to ‘w’. • logging.basicConfig(level=logging.DEBUG, • format='%(asctime)s - %(levelname)s - %(message)s', • handlers=[logging.FileHandler('example.log', mode='w')]) Garbage Collection • Python’s memory allocation and deallocation method is automatic. The user does not have to preallocate or deallocate memory similar to using dynamic memory allocation in languages such as C or C++. • Python uses two strategies for memory allocation: 1.Reference counting 2.Garbage collection • Reference counting Reference Counting • Python employ reference counting, a memory management approach, to automatically manage memory by tracking how many times an object is referenced. • A reference count, or the number of references that point to an object, is a property of each object in the Python language. • When an object’s reference count reaches zero, it becomes un- referenceable and its memory can be freed up. Garbage collection • Garbage collection is a memory management technique used in programming languages to automatically reclaim memory that is no longer accessible or in use by the application. • It helps prevent memory leaks, optimize memory usage, and ensure efficient memory allocation for the program.