-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
34 lines (26 loc) · 975 Bytes
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''
This module sets up a logger that can be used to write log messages to a
file.
The logger writes log messages at various severity levels (DEBUG, INFO,
WARNING, ERROR, CRITICAL) and writes them to a file called 'myapp.log'
in the current working directory. The logger can be imported and used in
other Python modules in the same project to write log messages.
Example usage:
# Import the logger
from logger import logger
# Write log messages
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')
'''
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('myapp.log')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)