forked from sillygoose/multisma2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logfiles.py
51 lines (40 loc) · 1.51 KB
/
logfiles.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
44
45
46
47
48
49
50
51
"""Module handling the application and production log files/"""
import os
import sys
import logging
from logging.handlers import TimedRotatingFileHandler
from configuration import (
APPLICATION_LOG_LOGGER_NAME,
APPLICATION_LOG_FILE,
APPLICATION_LOG_FORMAT,
)
logger = logging.getLogger(APPLICATION_LOG_LOGGER_NAME)
#
# Public
#
def start(app_logger):
"""Create the application log."""
filename = os.path.expanduser(APPLICATION_LOG_FILE + ".log")
try:
from configuration import APPLICATION_LOG_LEVEL
except ImportError:
APPLICATION_LOG_LEVEL = 'INFO'
# Create the directory if needed
filename_parts = os.path.split(filename)
if filename_parts[0] and not os.path.isdir(filename_parts[0]):
os.mkdir(filename_parts[0])
logname = os.path.abspath(filename)
handler = TimedRotatingFileHandler(logname, when='midnight', interval=1, backupCount=10)
handler.suffix = "%Y-%m-%d"
handler.setLevel(APPLICATION_LOG_LEVEL)
formatter = logging.Formatter(APPLICATION_LOG_FORMAT)
handler.setFormatter(formatter)
app_logger.addHandler(handler)
# Add some console output for anyone watching
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(level=APPLICATION_LOG_LEVEL)
console_handler.setFormatter(logging.Formatter(APPLICATION_LOG_FORMAT))
app_logger.setLevel(level=APPLICATION_LOG_LEVEL)
app_logger.addHandler(console_handler)
# First entry
app_logger.info("Created application log %s", filename)