diff --git a/kmip/__init__.py b/kmip/__init__.py index b0abd90..4c951e7 100644 --- a/kmip/__init__.py +++ b/kmip/__init__.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import logging.config import os import re import sys @@ -26,45 +25,10 @@ with open(version_path, 'r') as version_file: mo = re.search(r"^.*= '(\d\.\d\.\d)'$", version_file.read(), re.MULTILINE) __version__ = mo.group(1) -path = os.path.join(os.path.dirname(__file__), 'logconfig.ini') - -if os.path.exists(path): - logging.config.fileConfig(path) -else: - minor_version = sys.version_info[1] - - if minor_version == 7: - config = { - 'version': 1, - 'disable_existing_loggers': False, - 'formatters': { - 'simpleFormatter': { - 'format': - '%(asctime)s - %(name)s - %(levelname)s - %(message)s' - } - }, - 'handlers': { - 'consoleHandler': { - 'level': 'INFO', - 'class': 'logging.StreamHandler', - 'formatter': 'simpleFormatter', - 'stream': sys.stdout - } - }, - 'loggers': { - 'root': { - 'level': 'INFO', - 'handlers': ['consoleHandler'] - } - } - } - - logging.config.dictConfig(config) - else: - logging.basicConfig() __all__ = ['core', 'demos', 'services'] + if sys.version_info[:2] == (2, 6): warnings.simplefilter("always") warnings.warn( diff --git a/kmip/demos/pie/create.py b/kmip/demos/pie/create.py index 1803d56..6123fd7 100644 --- a/kmip/demos/pie/create.py +++ b/kmip/demos/pie/create.py @@ -14,7 +14,6 @@ # under the License. import logging -import os import sys from kmip.core import enums @@ -24,6 +23,8 @@ from kmip.pie import client if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(enums.Operation.CREATE) opts, args = parser.parse_args(sys.argv[1:]) @@ -34,18 +35,12 @@ if __name__ == '__main__': # Exit early if the arguments are not specified if algorithm is None: - logging.debug('No algorithm provided, exiting early from demo') + logger.error('No algorithm provided, exiting early from demo') sys.exit() if length is None: - logging.debug("No key length provided, exiting early from demo") + logger.error("No key length provided, exiting early from demo") sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - algorithm = getattr(enums.CryptographicAlgorithm, algorithm, None) # Build the client and connect to the server diff --git a/kmip/demos/pie/create_key_pair.py b/kmip/demos/pie/create_key_pair.py index 6325b66..31b15c3 100644 --- a/kmip/demos/pie/create_key_pair.py +++ b/kmip/demos/pie/create_key_pair.py @@ -14,7 +14,6 @@ # under the License. import logging -import os import sys from kmip.core import enums @@ -23,6 +22,8 @@ from kmip.pie import client if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(enums.Operation.CREATE_KEY_PAIR) opts, args = parser.parse_args(sys.argv[1:]) @@ -33,18 +34,12 @@ if __name__ == '__main__': # Exit early if the arguments are not specified if algorithm is None: - logging.debug('No algorithm provided, exiting early from demo') + logger.error('No algorithm provided, exiting early from demo') sys.exit() if length is None: - logging.debug("No key length provided, exiting early from demo") + logger.error("No key length provided, exiting early from demo") sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - algorithm = getattr(enums.CryptographicAlgorithm, algorithm, None) # Build the client and connect to the server diff --git a/kmip/demos/pie/destroy.py b/kmip/demos/pie/destroy.py index 423837e..7e7dc33 100644 --- a/kmip/demos/pie/destroy.py +++ b/kmip/demos/pie/destroy.py @@ -14,7 +14,6 @@ # under the License. import logging -import os import sys from kmip.core import enums @@ -23,6 +22,8 @@ from kmip.pie import client if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(enums.Operation.DESTROY) opts, args = parser.parse_args(sys.argv[1:]) @@ -32,15 +33,9 @@ if __name__ == '__main__': # Exit early if the UUID is not specified if uid is None: - logging.debug('No UUID provided, exiting early from demo') + logger.error('No UUID provided, exiting early from demo') sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - # Build the client and connect to the server with client.ProxyKmipClient(config=config) as client: try: diff --git a/kmip/demos/pie/get.py b/kmip/demos/pie/get.py index ec00ee3..90fc434 100644 --- a/kmip/demos/pie/get.py +++ b/kmip/demos/pie/get.py @@ -14,7 +14,6 @@ # under the License. import logging -import os import sys from kmip.core import enums @@ -23,6 +22,8 @@ from kmip.pie import client if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(enums.Operation.GET) opts, args = parser.parse_args(sys.argv[1:]) @@ -32,15 +33,9 @@ if __name__ == '__main__': # Exit early if the UUID is not specified if uid is None: - logging.debug('No UUID provided, exiting early from demo') + logger.error('No UUID provided, exiting early from demo') sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - # Build the client and connect to the server with client.ProxyKmipClient(config=config) as client: try: diff --git a/kmip/demos/pie/get_attribute_list.py b/kmip/demos/pie/get_attribute_list.py index 88573b9..27b3dad 100644 --- a/kmip/demos/pie/get_attribute_list.py +++ b/kmip/demos/pie/get_attribute_list.py @@ -14,7 +14,6 @@ # under the License. import logging -import os import sys from kmip.core import enums @@ -23,6 +22,8 @@ from kmip.pie import client if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(enums.Operation.GET_ATTRIBUTE_LIST) opts, args = parser.parse_args(sys.argv[1:]) @@ -32,15 +33,9 @@ if __name__ == '__main__': # Exit early if the UUID is not specified if uid is None: - logging.debug('No ID provided, exiting early from demo') + logger.error('No ID provided, exiting early from demo') sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - # Build the client and connect to the server with client.ProxyKmipClient(config=config) as client: try: diff --git a/kmip/demos/pie/register_certificate.py b/kmip/demos/pie/register_certificate.py index 63882bf..ac32ff9 100644 --- a/kmip/demos/pie/register_certificate.py +++ b/kmip/demos/pie/register_certificate.py @@ -24,8 +24,9 @@ from kmip.pie import objects if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + parser = utils.build_cli_parser() - logger = logging.getLogger(__name__) opts, args = parser.parse_args(sys.argv[1:]) config = opts.config diff --git a/kmip/demos/pie/register_opaque_object.py b/kmip/demos/pie/register_opaque_object.py index bceebbf..e7fcd46 100644 --- a/kmip/demos/pie/register_opaque_object.py +++ b/kmip/demos/pie/register_opaque_object.py @@ -24,8 +24,9 @@ from kmip.pie import objects if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + parser = utils.build_cli_parser() - logger = logging.getLogger(__name__) opts, args = parser.parse_args(sys.argv[1:]) config = opts.config diff --git a/kmip/demos/pie/register_private_key.py b/kmip/demos/pie/register_private_key.py index f4ed9f8..176acca 100644 --- a/kmip/demos/pie/register_private_key.py +++ b/kmip/demos/pie/register_private_key.py @@ -24,8 +24,9 @@ from kmip.pie import objects if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + parser = utils.build_cli_parser() - logger = logging.getLogger(__name__) opts, args = parser.parse_args(sys.argv[1:]) config = opts.config diff --git a/kmip/demos/pie/register_public_key.py b/kmip/demos/pie/register_public_key.py index aa0796d..9dae926 100644 --- a/kmip/demos/pie/register_public_key.py +++ b/kmip/demos/pie/register_public_key.py @@ -24,8 +24,9 @@ from kmip.pie import objects if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + parser = utils.build_cli_parser() - logger = logging.getLogger(__name__) opts, args = parser.parse_args(sys.argv[1:]) config = opts.config diff --git a/kmip/demos/pie/register_secret_data.py b/kmip/demos/pie/register_secret_data.py index 63047cb..59faca9 100644 --- a/kmip/demos/pie/register_secret_data.py +++ b/kmip/demos/pie/register_secret_data.py @@ -25,8 +25,9 @@ from kmip.pie import objects if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + parser = utils.build_cli_parser() - logger = logging.getLogger(__name__) opts, args = parser.parse_args(sys.argv[1:]) config = opts.config diff --git a/kmip/demos/pie/register_symmetric_key.py b/kmip/demos/pie/register_symmetric_key.py index 7d51164..eea05a5 100644 --- a/kmip/demos/pie/register_symmetric_key.py +++ b/kmip/demos/pie/register_symmetric_key.py @@ -24,8 +24,9 @@ from kmip.pie import objects if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + parser = utils.build_cli_parser() - logger = logging.getLogger(__name__) opts, args = parser.parse_args(sys.argv[1:]) config = opts.config diff --git a/kmip/demos/units/activate.py b/kmip/demos/units/activate.py index 72208eb..24c8f2e 100644 --- a/kmip/demos/units/activate.py +++ b/kmip/demos/units/activate.py @@ -21,11 +21,12 @@ from kmip.demos import utils from kmip.services.kmip_client import KMIPProxy import logging -import os import sys if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.ACTIVATE) opts, args = parser.parse_args(sys.argv[1:]) @@ -35,15 +36,9 @@ if __name__ == '__main__': # Exit early if the UUID is not specified if uuid is None: - logging.debug('No UUID provided, exiting early from demo') + logger.error('No UUID provided, exiting early from demo') sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - # Build the client and connect to the server client = KMIPProxy(config=config) client.open() diff --git a/kmip/demos/units/create.py b/kmip/demos/units/create.py index dc3a677..cb527b3 100644 --- a/kmip/demos/units/create.py +++ b/kmip/demos/units/create.py @@ -35,11 +35,12 @@ from kmip.core.objects import Attribute from kmip.services.kmip_client import KMIPProxy import logging -import os import sys if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.CREATE) opts, args = parser.parse_args(sys.argv[1:]) @@ -52,18 +53,12 @@ if __name__ == '__main__': # Exit early if the arguments are not specified if algorithm is None: - logging.debug('No algorithm provided, exiting early from demo') + logger.error('No algorithm provided, exiting early from demo') sys.exit() if length is None: - logging.debug("No key length provided, exiting early from demo") + logger.error("No key length provided, exiting early from demo") sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - attribute_factory = AttributeFactory() credential_factory = CredentialFactory() @@ -88,8 +83,8 @@ if __name__ == '__main__': algorithm_enum = getattr(CryptographicAlgorithm, algorithm, None) if algorithm_enum is None: - logging.debug("{0} not found".format(algorithm)) - logging.debug("Invalid algorithm specified, exiting early from demo") + logger.debug("{0} not found".format(algorithm)) + logger.debug("Invalid algorithm specified, exiting early from demo") client.close() sys.exit() diff --git a/kmip/demos/units/create_key_pair.py b/kmip/demos/units/create_key_pair.py index 3d16e26..8ce0215 100644 --- a/kmip/demos/units/create_key_pair.py +++ b/kmip/demos/units/create_key_pair.py @@ -37,11 +37,12 @@ from kmip.core.objects import Attribute from kmip.services.kmip_client import KMIPProxy import logging -import os import sys if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.CREATE_KEY_PAIR) opts, args = parser.parse_args(sys.argv[1:]) @@ -55,28 +56,22 @@ if __name__ == '__main__': # Exit early if the arguments are not specified if algorithm is None: - logging.error('No algorithm provided, exiting early from demo') + logger.error('No algorithm provided, exiting early from demo') sys.exit() if length is None: - logging.error("No key length provided, exiting early from demo") + logger.error("No key length provided, exiting early from demo") sys.exit() if name is None: - logging.error("No key name provided, exiting early from demo") + logger.error("No key name provided, exiting early from demo") sys.exit() attribute_type = AttributeType.CRYPTOGRAPHIC_ALGORITHM algorithm_enum = getattr(CryptographicAlgorithm, algorithm, None) if algorithm_enum is None: - logging.error("Invalid algorithm specified; exiting early from demo") + logger.error("Invalid algorithm specified; exiting early from demo") sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - attribute_factory = AttributeFactory() credential_factory = CredentialFactory() diff --git a/kmip/demos/units/destroy.py b/kmip/demos/units/destroy.py index 87e1bde..cb27336 100644 --- a/kmip/demos/units/destroy.py +++ b/kmip/demos/units/destroy.py @@ -25,11 +25,12 @@ from kmip.demos import utils from kmip.services.kmip_client import KMIPProxy import logging -import os import sys if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.DESTROY) opts, args = parser.parse_args(sys.argv[1:]) @@ -41,15 +42,9 @@ if __name__ == '__main__': # Exit early if the UUID is not specified if uuid is None: - logging.debug('No UUID provided, exiting early from demo') + logger.error('No UUID provided, exiting early from demo') sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - attribute_factory = AttributeFactory() credential_factory = CredentialFactory() diff --git a/kmip/demos/units/discover_versions.py b/kmip/demos/units/discover_versions.py index f478356..6155347 100644 --- a/kmip/demos/units/discover_versions.py +++ b/kmip/demos/units/discover_versions.py @@ -23,12 +23,13 @@ from kmip.demos import utils from kmip.services.kmip_client import KMIPProxy import logging -import os import sys import re if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.DISCOVER_VERSIONS) opts, args = parser.parse_args(sys.argv[1:]) @@ -44,12 +45,6 @@ if __name__ == '__main__': protocol_versions.append(ProtocolVersion.create(int(mm[0]), int(mm[1]))) - # Build and setup logging - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - # Build the client and connect to the server client = KMIPProxy(config=config) client.open() diff --git a/kmip/demos/units/get.py b/kmip/demos/units/get.py index 6a95c9a..feb4676 100644 --- a/kmip/demos/units/get.py +++ b/kmip/demos/units/get.py @@ -28,11 +28,12 @@ from kmip.demos import utils from kmip.services.kmip_client import KMIPProxy import logging -import os import sys if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.GET) opts, args = parser.parse_args(sys.argv[1:]) @@ -45,7 +46,7 @@ if __name__ == '__main__': # Exit early if the UUID is not specified if uuid is None: - logging.debug('No UUID provided, exiting early from demo') + logger.error('No UUID provided, exiting early from demo') sys.exit() format_type_enum = None @@ -53,16 +54,10 @@ if __name__ == '__main__': format_type_enum = getattr(KeyFormatTypeEnum, format_type, None) if format_type_enum is None: - logging.error( + logger.error( "Invalid key format type specified; exiting early from demo") sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - attribute_factory = AttributeFactory() credential_factory = CredentialFactory() diff --git a/kmip/demos/units/locate.py b/kmip/demos/units/locate.py index 7aa521c..1e882ac 100644 --- a/kmip/demos/units/locate.py +++ b/kmip/demos/units/locate.py @@ -30,11 +30,12 @@ from kmip.demos import utils from kmip.services.kmip_client import KMIPProxy import logging -import os import sys if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.LOCATE) opts, args = parser.parse_args(sys.argv[1:]) @@ -46,15 +47,9 @@ if __name__ == '__main__': # Exit early if the UUID is not specified if name is None: - logging.debug('No name provided, exiting early from demo') + logger.error('No name provided, exiting early from demo') sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - attribute_factory = AttributeFactory() credential_factory = CredentialFactory() diff --git a/kmip/demos/units/query.py b/kmip/demos/units/query.py index 300e044..144d86d 100644 --- a/kmip/demos/units/query.py +++ b/kmip/demos/units/query.py @@ -14,7 +14,6 @@ # under the License. import logging -import os import sys from six.moves import xrange @@ -31,6 +30,8 @@ from kmip.services.kmip_client import KMIPProxy if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.QUERY) opts, args = parser.parse_args(sys.argv[1:]) @@ -39,12 +40,6 @@ if __name__ == '__main__': password = opts.password config = opts.config - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - # Build query function list. query_functions = list() query_functions.append( diff --git a/kmip/demos/units/register.py b/kmip/demos/units/register.py index 7a1717c..48fbb39 100644 --- a/kmip/demos/units/register.py +++ b/kmip/demos/units/register.py @@ -25,11 +25,12 @@ from kmip.demos import utils from kmip.services.kmip_client import KMIPProxy import logging -import os import sys if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + parser = utils.build_cli_parser(Operation.REGISTER) opts, args = parser.parse_args(sys.argv[1:]) @@ -42,20 +43,14 @@ if __name__ == '__main__': # Exit early if the arguments are not specified object_type = getattr(ObjectType, object_type, None) if object_type is None: - logging.error("Invalid object type specified; exiting early from demo") + logger.error("Invalid object type specified; exiting early from demo") sys.exit() key_format_type = getattr(KeyFormatType, format_type, None) if key_format_type is None: - logging.error( + logger.error( "Invalid key format type specified; exiting early from demo") - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - # Create the template attribute for the secret and then build the secret usage_mask = utils.build_cryptographic_usage_mask(logger, object_type) attributes = [usage_mask] diff --git a/kmip/demos/units/revoke.py b/kmip/demos/units/revoke.py index ba4fb69..800918b 100644 --- a/kmip/demos/units/revoke.py +++ b/kmip/demos/units/revoke.py @@ -22,11 +22,12 @@ from kmip.demos import utils from kmip.services.kmip_client import KMIPProxy import logging -import os import sys if __name__ == '__main__': + logger = utils.build_console_logger(logging.INFO) + # Build and parse arguments parser = utils.build_cli_parser(Operation.REVOKE) opts, args = parser.parse_args(sys.argv[1:]) @@ -36,15 +37,9 @@ if __name__ == '__main__': # Exit early if the UUID is not specified if uuid is None: - logging.debug('No UUID provided, exiting early from demo') + logger.error('No UUID provided, exiting early from demo') sys.exit() - # Build and setup logging and needed factories - f_log = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, - 'logconfig.ini') - logging.config.fileConfig(f_log) - logger = logging.getLogger(__name__) - # Build the client and connect to the server client = KMIPProxy(config=config) client.open() diff --git a/kmip/demos/utils.py b/kmip/demos/utils.py index 18a4d7b..7212e0c 100644 --- a/kmip/demos/utils.py +++ b/kmip/demos/utils.py @@ -13,6 +13,11 @@ # License for the specific language governing permissions and limitations # under the License. +import binascii +import logging +import optparse +import sys + from kmip.core.attributes import CryptographicAlgorithm from kmip.core.attributes import CryptographicLength @@ -38,9 +43,17 @@ from kmip.core.secrets import PublicKey from kmip.core.secrets import SymmetricKey from kmip.core.secrets import SecretData -import binascii -import optparse -import sys + +def build_console_logger(level): + logger = logging.getLogger('demo') + logger.setLevel(level) + handler = logging.StreamHandler() + formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + ) + handler.setFormatter(formatter) + logger.addHandler(handler) + return logger def build_cli_parser(operation=None): diff --git a/kmip/logconfig.ini b/kmip/logconfig.ini deleted file mode 100644 index 190d62c..0000000 --- a/kmip/logconfig.ini +++ /dev/null @@ -1,21 +0,0 @@ -[loggers] -keys=root - -[handlers] -keys=consoleHandler - -[formatters] -keys=simpleFormatter - -[logger_root] -level=INFO -handlers=consoleHandler - -[handler_consoleHandler] -class=StreamHandler -level=INFO -formatter=simpleFormatter -args=(sys.stdout,) - -[formatter_simpleFormatter] -format=%(asctime)s - %(name)s - %(levelname)s - %(message)s