-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
43 lines (28 loc) · 1.02 KB
/
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
35
36
37
38
39
40
41
42
43
# /usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Author: samzong.lu
E-mail: [email protected]
"""
import logging
import os
from logging.handlers import RotatingFileHandler
def setup_logger(name, log_file, level=logging.INFO):
"""Function to setup as many loggers as you want"""
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler = RotatingFileHandler(log_file, maxBytes=2000000, backupCount=5) # 2MB, 5 files
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
# If you also want to see the logs in the console, add StreamHandler.
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger
# Ensure the log directory exists
log_dir = "logs"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Create global logger
logger = setup_logger('global_logger', os.path.join(log_dir, 'app.log'))