forked from x4nth055/pythoncode-tutorials
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_handlers.py
More file actions
Latest commit
21 lines (15 loc) · 630 Bytes
/
Copy pathlogger_handlers.py
File metadata and controls
21 lines (15 loc) · 630 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
importlogging
# return a logger with the specified name & creating it if necessary
logger=logging.getLogger(__name__)
# create a logger handler, in this case: file handler
file_handler=logging.FileHandler("file.log")
# set the level of logging to INFO
file_handler.setLevel(logging.INFO)
# create a logger formatter
logging_format=logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
# add the format to the logger handler
file_handler.setFormatter(logging_format)
# add the handler to the logger
logger.addHandler(file_handler)
# use the logger as previously
logger.critical("This is a critical message!")