-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_util.py
28 lines (22 loc) · 1 KB
/
log_util.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
import logging
from logging.handlers import RotatingFileHandler
LOG_FILE = 'app.log'
logging.basicConfig(filename=LOG_FILE, filemode='w', format='%(name)s - %(levelname)s - %(message)s')
def setup_logging():
# Create a logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # Set to DEBUG to capture all levels of logs
# Create handlers (file and console)
file_handler = RotatingFileHandler('app.log', maxBytes=1024 * 1024 * 5, backupCount=2)
console_handler = logging.StreamHandler()
# Set logging level for handlers
file_handler.setLevel(logging.INFO)
console_handler.setLevel(logging.DEBUG)
# Create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add the handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)