Python : Part 4 - Logging
Published: 2024-03-19 14:40:35


**Logging facility for Python**

  ##### Logging in Python - Table Of Contents
        - [Format 1](#format-1)
        - [Format 2](#format-2)


- This module defines functions and classes which implement a flexible event logging system for applications and libraries.
- To work with logging, need to logging module and basicConfig to be added

### Format 1 - with out log file<a name="foramt-1"></a>

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


### Format 2-with log file<a name="format-2"></a>

```
import logging
logging.basicConfig(filename='mylog.txt',level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')
```

**%s in a string format in Python**
Basically, the % symbol is used with a large variety of data having many data types and configurations in Python.

Talking about %s, it is specifically used to perform concatenation of two or more strings together in Python. The %s allow us to format or place a string or numerical value within a given string. In simple language, the %s in Python is used to incorporate a given string within another string. 

**Example:**
```
# Define a string value   
str = "JavaTpoint!"  
# using %s to appending string  
print("Hello Python developers! Welcome to, %s!" % str)  
```

**Example : Buggy Factorial Program**
###### Scenario :1

```
#fact.py
def factorial (n):
    total = 1
    for i in range(n+1):
         total * = i
    return total

print(factorial(5))
```

> o/p : 0


###### Scenario :2
```
import logging
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')
#logging.basicConfig(filename='mylog.txt' level=logging.DEBUG,format='%(asctime)s - %(levelname)s - %(message)s')

#logging.disable(logging.CRITICAL) # it will disable all messages
# if specific messages to be disabled then try below
#logging.disable(logging.WARNING) 

logging.debug('Start of program')
def factorial (n):
    total = 1
    for i in range(n+1):
    #for i in range(1,n+1):
         total *= i
	 logging.debug('i is %s, total is %s' % (i,total))
    logging.debug('Return value is %s' % (total))
    return total

print(factorial(5))
logging.debug('End of program')
```

`o/p : 120`


**Log Levels**

- debug(lowest)   
- info
- warning
- error
- critical(highest)

Recap :

- The logging module lets you display logging messages.
- Log messages create a "breadcrumb trail" of what your program is doing.
- After calling logging.basicConfig() to set up logging, call logging.debug(‘This is the message') to create a log message.
- When done, you can disable the log messages with logging.disable(logging.CRITICAL)
- Don't use print() for log messages: It's hard to remove the mall when you're done debugging.
- The five log levels are: DEBUG, INFO, WARNING, ERROR, and CRITICAL.
- You can also log to a file instead of the screen with the filename keyword argument in the logging.basicConfig() function.

**References **:  https://www.javatpoint.com/python-s-string-formatting#:~:text=Talking%20about%20%25s%2C%20it%20is,given%20string%20within%20another%20string.

**Conclusion** : Discussed how to use logging in python and used IDLE shell command for running the python code

💬 If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it 😀 and share this blog with ur friends and follow me in [linkedin](https://www.linkedin.com/in/srinivasuluparanduru)