santanude | afc967b | 2021-11-01 18:32:18 +0530 | [diff] [blame] | 1 | # Copyright 2021 Xoriant Corporation |
| 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 | |
| 16 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
| 17 | import configparser |
| 18 | import logging.handlers |
| 19 | |
| 20 | |
| 21 | class AppConfig: |
| 22 | kafka_broker = "" |
| 23 | logger = logging.getLogger() |
| 24 | |
| 25 | def __init__(self): |
| 26 | parser = ArgumentParser(description="", |
| 27 | formatter_class=ArgumentDefaultsHelpFormatter) |
| 28 | parser.add_argument('-c', '--config', |
| 29 | dest='config', |
| 30 | default='/opt/ves/adapter/config/adapter.conf', |
| 31 | help='Use this config file.') |
| 32 | parser.add_argument('-s', '--section', |
| 33 | dest='section', |
| 34 | default='default', |
| 35 | metavar='<section>', |
| 36 | help='section to use in the config file') |
| 37 | |
| 38 | args = parser.parse_args() |
| 39 | config_file = args.config |
| 40 | config_section = args.section |
| 41 | |
| 42 | overrides = {} |
| 43 | config = configparser.ConfigParser() |
| 44 | config.read(config_file) |
| 45 | |
santanude | 5ea5016 | 2021-12-07 15:31:41 +0530 | [diff] [blame^] | 46 | self.kafka_broker = config.get(config_section, 'kafka_broker', vars=overrides) |
santanude | afc967b | 2021-11-01 18:32:18 +0530 | [diff] [blame] | 47 | log_file = config.get(config_section, 'log_file', vars=overrides) |
| 48 | log_level = config.get(config_section, 'log_level', vars=overrides) |
| 49 | |
santanude | 5ea5016 | 2021-12-07 15:31:41 +0530 | [diff] [blame^] | 50 | self.setLogger(log_file, log_level) |
santanude | afc967b | 2021-11-01 18:32:18 +0530 | [diff] [blame] | 51 | |
| 52 | def getKafkaBroker(self): |
| 53 | return self.kafka_broker |
| 54 | |
| 55 | def getLogger(self): |
| 56 | return self.logger |
santanude | 5ea5016 | 2021-12-07 15:31:41 +0530 | [diff] [blame^] | 57 | |
| 58 | def setLogger(self, log_file, log_level): |
| 59 | rfh = logging.handlers.RotatingFileHandler( |
| 60 | filename=log_file, |
| 61 | mode='w', |
| 62 | maxBytes=1000000, |
| 63 | backupCount=10, |
| 64 | encoding=None, |
| 65 | delay=0 |
| 66 | ) |
| 67 | |
| 68 | logging.basicConfig( |
| 69 | format="%(asctime)s %(name)-8s %(levelname)-8s %(message)s", |
| 70 | datefmt="%Y-%m-%d %H:%M:%S.%f %z", |
| 71 | handlers=[rfh] |
| 72 | ) |
| 73 | |
| 74 | logger = logging.getLogger("DMaaP") |
| 75 | |
| 76 | # we are going to set the log level |
| 77 | if (log_level == 'DEBUG'): |
| 78 | logger.setLevel(logging.DEBUG) |
| 79 | elif (log_level == 'ERROR'): |
| 80 | logger.setLevel(logging.ERROR) |
| 81 | else: |
| 82 | logger.setLevel(logging.INFO) |
| 83 | |
| 84 | logger.info('Log level {} and log file {} : '.format(log_level, log_file)) |
| 85 | self.logger = logger |