Skip to content

Instantly share code, notes, and snippets.

View Abhayparashar31's full-sized avatar
:octocat:
Learning New Things

Abhay Parashar Abhayparashar31

:octocat:
Learning New Things
View GitHub Profile
'''
Execution Time Of A CodeBlock May Differ At Each Exection, because of timeit.
timeit runs your CodeBlock millions of times (default value is 1000000) so that you get the statistically most relevant
measurement of code execution time!
'''
import timeit
start = timeit.default_timer()
## Paste Your Code Here
stop = timeit.default_timer()
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
logging.debug('This is Debug Message')
logging.info('This is an info message')
-------------------------CONSOLE OUTPUT------------------
12/05/2021 20:46:41 - root - DEBUG - This is Debug Message
12/05/2021 20:46:41 - root - INFO - This is an info message
import logging
logging.basicConfig(level=logging.DEBUG,filename='data.log', filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
name = str(input("Enter Your Name:\n"))
logging.info(f"{name} has logged in successfully !!")
-----------------data.log file output-------------
12/05/2021 21:01:37 - root - INFO - Abhay Parashar has logged in successfully !!
12/05/2021 21:02:47 - root - INFO - Karl has logged in successfully !!
12/05/2021 21:03:27 - root - INFO - Rahul has logged in successfully !!
import logging
a = 10
b = 0
try:
c = a / b
except Exception as e:
logging.error("Exception Occurred", exc_info=True) ## At default it is True
----------------------------------CONSOLE OUTPUT------------------------
ERROR:root:Exception Occurred ## If exc_info=False then only this message will print
import logging
# custom logger
logger = logging.getLogger(__name__)
# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
c_handler.setLevel(logging.WARNING)
f_handler.setLevel(logging.ERROR)
import logging
import logging.config
## Loads The Config File
logging.config.fileConfig('logging.conf')
# create a logger with the name from the config file.
# This logger now has StreamHandler with DEBUG Level and the specified format in the logging.conf file
logger = logging.getLogger('simpleExample')
import logging
from logging.handlers import RotatingFileHandler
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# roll over after 2KB, and keep backup logs app.log.1, app.log.2 , etc.
handler = RotatingFileHandler('app.log', maxBytes=2000, backupCount=5)
logger.addHandler(handler)
import logging
import time
from logging.handlers import TimedRotatingFileHandler
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
## Create a log file every seconds with a new log detail at the inteval of 2
handler = TimedRotatingFileHandler('app.log', when='s', interval=2, backupCount=3)
logger.addHandler(handler)
class Employee:
## class attribute
alias = 'Data Management'
def __init__(self, position,age,salary,experience):
## Instance attributes
self.position = position
self.age = age
self.salary = salary