Skip to content

Commit f8f634b

Browse files
rotating the log file at certain timed intervals.
1 parent 2a3abca commit f8f634b

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

log_config/log_config.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
# formatter
1818
FORMATTER = "%(asctime)s [%(threadName)s] [%(filename)s:%(funcName)s:%(lineno)d] " \
1919
"%(levelname)s %(message)s"
20-
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
20+
DATE_FORMAT = "%Y%m%d_%H%M%S"
2121

2222
# log file args
2323
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
24-
LOG_FILE_NAME = "debug_{}.log".format(datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) # your_log_file_name.log
24+
LOG_FILE_NAME = "debug_{}.log".format(datetime.now().strftime("%Y%m%d_%H%M%S")) # your_log_file_name.log
2525
LOG_FILE_PATH = os.path.join(BASE_DIR, LOG_FILE_NAME) # your log full path
2626
LOG_FILE_SIZE = 10 * 1024 * 1024 # the limit of log file size
2727
LOG_BACKUP_COUNT = 5 # backup counts
@@ -42,7 +42,7 @@
4242

4343
def init_log_config(log_dir="", file_prefix="debug", file_size_limit=10 * 1024 * 1024, backup_count=10,
4444
use_mail=False, console_level=LOG_CONSOLE_LEVEL,
45-
file_level=LOG_FILE_LEVEL, mail_level=LOG_MAIL_LEVEL, everyday=True):
45+
file_level=LOG_FILE_LEVEL, mail_level=LOG_MAIL_LEVEL, when="D", interval=1):
4646
'''
4747
Do basic configuration for the logging system. support ConsoleHandler, RotatingFileHandler and SMTPHandler
4848
:param log_dir: the dir where to save log files, default "{current_path}/logs"
@@ -53,6 +53,8 @@ def init_log_config(log_dir="", file_prefix="debug", file_size_limit=10 * 1024 *
5353
:param console_level: the level of output log in console, default logging.DEBUG
5454
:param file_level: the level of log file, default logging.INFO
5555
:param mail_level: the level of log mail, default logging.ERROR
56+
:param when: rotating the log file at certain timed intervals. 'D'-days, 'H'-hours, 'M'-minutes
57+
:param interval: rotating the log file at certain timed intervals.
5658
:return: None
5759
'''
5860

@@ -69,12 +71,12 @@ def init_log_config(log_dir="", file_prefix="debug", file_size_limit=10 * 1024 *
6971
os.mkdir(log_dir)
7072

7173
# default log file name like: debug_2018-08-27_15-40-52.log
72-
log_file_path = os.path.join(log_dir, "{}_{}.log".format(file_prefix, datetime.now().strftime("%Y-%m-%d_%H-%M-%S")))
74+
log_file_path = os.path.join(log_dir, "{}_{}.log".format(file_prefix, datetime.now().strftime("%Y%m%d_%H%M%S")))
7375

7476
# add rotating file handler
7577
# rf_handler = handlers.RotatingFileHandler(log_file_path, maxBytes=file_size_limit, backupCount=backup_count, encoding="utf-8")
76-
if everyday:
77-
rf_handler = handlers.TimedRotatingFileHandler(log_file_path, when="D", backupCount=backup_count, interval=1, encoding="utf-8")
78+
if when:
79+
rf_handler = handlers.TimedRotatingFileHandler(log_file_path, when=when, backupCount=backup_count, interval=interval, encoding="utf-8")
7880
else:
7981
rf_handler = handlers.RotatingFileHandler(log_file_path, maxBytes=file_size_limit, backupCount=backup_count,
8082
encoding="utf-8")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='log_config',
10-
version="1.7",
10+
version="2.0",
1111
description=(
1212
"Provide a function for anyone who want to configure log parameters easily, just call init_log_config when your app start up, the you can use the module of logging which build-in python3 without any other configtur. This module support ConsoleHandler, RotatingFileHandler and SMTPHandler and You can change the configuration parameters according to your requirements."
1313
),

0 commit comments

Comments
 (0)