|
a |
|
b/Configs/Logger.py |
|
|
1 |
#!/usr/bin/env python |
|
|
2 |
# -*- coding: UTF-8 -*- |
|
|
3 |
# |
|
|
4 |
# Copyright 2017 University of Westminster. All Rights Reserved. |
|
|
5 |
# |
|
|
6 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
7 |
# you may not use this file except in compliance with the License. |
|
|
8 |
# You may obtain a copy of the License at |
|
|
9 |
# |
|
|
10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
|
|
11 |
# |
|
|
12 |
# Unless required by applicable law or agreed to in writing, software |
|
|
13 |
# distributed under the License is distributed on an "AS IS" BASIS, |
|
|
14 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
15 |
# See the License for the specific language governing permissions and |
|
|
16 |
# limitations under the License. |
|
|
17 |
# ============================================================================== |
|
|
18 |
""" It configures the Python application logger. |
|
|
19 |
""" |
|
|
20 |
|
|
|
21 |
import os |
|
|
22 |
import logging |
|
|
23 |
|
|
|
24 |
__author__ = "Mohsen Mesgarpour" |
|
|
25 |
__copyright__ = "Copyright 2016, https://github.com/mesgarpour" |
|
|
26 |
__credits__ = ["Mohsen Mesgarpour"] |
|
|
27 |
__license__ = "GPL" |
|
|
28 |
__version__ = "1.0" |
|
|
29 |
__maintainer__ = "Mohsen Mesgarpour" |
|
|
30 |
__email__ = "mohsen.mesgarpour@gmail.com" |
|
|
31 |
__status__ = "Release" |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
class Logger: |
|
|
35 |
def __init__(self, |
|
|
36 |
path: str, |
|
|
37 |
app_name: str, |
|
|
38 |
ext: str = "log"): |
|
|
39 |
"""Initialise the objects and constants. |
|
|
40 |
:param path: the output directory path, where the log file will be saved. |
|
|
41 |
:param app_name: the application name, which will be used as the log file name. |
|
|
42 |
:param ext: the log file extension name. |
|
|
43 |
""" |
|
|
44 |
# create logger |
|
|
45 |
logger = logging.getLogger(app_name) |
|
|
46 |
logger.setLevel(logging.DEBUG) |
|
|
47 |
path_full = os.path.abspath(os.path.join(path, app_name + "." + ext)) |
|
|
48 |
|
|
|
49 |
# create file handler which logs even debug messages |
|
|
50 |
fh = logging.FileHandler(path_full, mode='w') |
|
|
51 |
fh.setLevel(logging.DEBUG) |
|
|
52 |
|
|
|
53 |
# create console handler with a higher log level |
|
|
54 |
ch = logging.StreamHandler() |
|
|
55 |
ch.setLevel(logging.INFO) |
|
|
56 |
|
|
|
57 |
# create formatter and add it to the handlers |
|
|
58 |
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
|
|
59 |
fh.setFormatter(formatter) |
|
|
60 |
ch.setFormatter(formatter) |
|
|
61 |
|
|
|
62 |
# add the handlers to the logger |
|
|
63 |
logger.addHandler(fh) |
|
|
64 |
logger.addHandler(ch) |
|
|
65 |
|
|
|
66 |
# output log |
|
|
67 |
logger.info("Creating '" + path_full + "' File.") |