1import logging
2logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
3logging.debug('This is debug message')
4logging.info('This is information message')
5logging.warning('This is warning message')
6logging.error('This is warning message')
1logging.basicConfig(filename="logfilename.log", level=logging.INFO)
2# Log Creation
3
4logging.info('your text goes here')
5logging.error('your text goes here')
6logging.debug('your text goes here')
7
1# logging_example.py
2
3import logging
4
5# Create a custom logger
6logger = logging.getLogger(__name__)
7
8# Create handlers
9c_handler = logging.StreamHandler()
10f_handler = logging.FileHandler('file.log')
11c_handler.setLevel(logging.WARNING)
12f_handler.setLevel(logging.ERROR)
13
14# Create formatters and add it to handlers
15c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
16f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
17c_handler.setFormatter(c_format)
18f_handler.setFormatter(f_format)
19
20# Add handlers to the logger
21logger.addHandler(c_handler)
22logger.addHandler(f_handler)
23
24logger.warning('This is a warning')
25logger.error('This is an error')
26
1import logging
2logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
3logging.debug('This message should go to the log file')
4logging.info('So should this')
5logging.warning('And this, too')
6logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
7