Added seed code for access-management.
[ta/access-management.git] / src / access_management / backend / restlogger.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16 from logging.handlers import RotatingFileHandler
17
18 restlogger = None
19
20 class RestLogger(object):
21     def __init__(self, config):
22         self.logger = logging.getLogger("AM")
23         self.logger.setLevel(config["Logging"]["loglevel"])
24         self.filehandler = self._get_filehandler(config["Logging"]["logdir"]+"/am.log")
25         self.logger.addHandler(self.filehandler)
26
27     @staticmethod
28     def _get_filehandler(filename):
29         rfh = RotatingFileHandler(filename, mode='a', maxBytes=1000000, backupCount=10, encoding=None, delay=0)
30         formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(pathname)s:%(funcName)s:%(lineno)d:%(message)s')
31         rfh.setFormatter(formatter)
32         return rfh
33
34     def get_logger(self):
35         return self.logger
36
37     def get_handler(self):
38         return self.filehandler
39
40 def get_logger(config):
41     global restlogger
42     if not restlogger:
43         restlogger = RestLogger(config)
44     return restlogger.get_logger()
45
46 def get_log_handler(config):
47     global restlogger
48     if not restlogger:
49         restlogger = RestLogger(config)
50     return restlogger.get_handler()