From: Frank Brehm Date: Mon, 7 Dec 2020 17:12:32 +0000 (+0100) Subject: Changing logging X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=f3315bcf74eaf39535c203854e129e3af70c525e;p=pixelpark%2Fldap-migration.git Changing logging --- diff --git a/lib/ldap_migration/__init__.py b/lib/ldap_migration/__init__.py index 952ab5e..5a03095 100644 --- a/lib/ldap_migration/__init__.py +++ b/lib/ldap_migration/__init__.py @@ -45,7 +45,7 @@ from .config import LDAPMigrationConfiguration from .idict import CaseInsensitiveDict from .istringset import CaseInsensitiveStringSet -__version__ = '0.8.6' +__version__ = '0.8.7' LOG = logging.getLogger(__name__) CFG_BASENAME = 'ldap-migration.ini' @@ -192,10 +192,11 @@ class LDAPMigrationApplication(BaseApplication): self.source = None self.tgt_server = None self.target = None - self.tmp_dir = None + self.log_dir = None self.all_dns_file = None self.structural_dns_file = None self.migrated_file = None + self.general_logfile = None self.lap = 0 self.total_count = 0 @@ -222,10 +223,6 @@ class LDAPMigrationApplication(BaseApplication): description=description, initialized=False, ) - self.tmp_dir = self.base_dir / 'tmp' - self.all_dns_file = self.tmp_dir / 'all-dns.txt' - self.structural_dns_file = self.tmp_dir / 'structural-dns.txt' - self.migrated_file = self.tmp_dir / 'migrated-entries.txt' self.initialized = True # ------------------------------------------------------------------------- @@ -347,6 +344,19 @@ class LDAPMigrationApplication(BaseApplication): return formatter + # ------------------------------------------------------------------------- + def _get_logfile_formatter(self): + + format_str = '[%(asctime)s]: ' + format_str += self.appname + ': ' + if self.verbose > 1: + format_str += '%(name)s(%(lineno)d) %(funcName)s() ' + else: + format_str += '%(name)s ' + format_str += '%(levelname)s - %(message)s' + + return logging.Formatter(format_str) + # ------------------------------------------------------------------------- def init_logging(self): """ @@ -358,9 +368,7 @@ class LDAPMigrationApplication(BaseApplication): """ log_level = logging.INFO - if self.verbose: - log_level = logging.DEBUG - elif self.quiet: + if self.quiet: log_level = logging.WARNING root_logger = logging.getLogger() @@ -382,6 +390,16 @@ class LDAPMigrationApplication(BaseApplication): else: paramiko_logger.setLevel(logging.INFO) + lf_formatter = self._get_logfile_formatter() + lh_file = logging.FileHandler( + str(self.general_logfile), mode='a', encoding='utf-8', delay=True) + if self.verbose: + lh_file.setLevel(logging.DEBUG) + else: + lh_file.setLevel(logging.INFO) + lh_file.setFormatter(lf_formatter) + root_logger.addHandler(lh_file) + return # ------------------------------------------------------------------------- @@ -399,6 +417,16 @@ class LDAPMigrationApplication(BaseApplication): self.initialized = False + file_timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S') + + self.log_dir = self.base_dir / 'log' + self.check_log_dir() + + self.all_dns_file = self.log_dir / 'all-dns.{}.txt'.format(file_timestamp) + self.structural_dns_file = self.log_dir / 'structural-dns.{}.txt'.format(file_timestamp) + self.migrated_file = self.log_dir / 'migrated-entries.{}.txt'.format(file_timestamp) + self.general_logfile = self.log_dir / 'migration.{}.log'.format(file_timestamp) + self.init_logging() self.perform_arg_parser() @@ -821,34 +849,34 @@ class LDAPMigrationApplication(BaseApplication): self.integer_attribute_types.as_list())) # ------------------------------------------------------------------------- - def check_tmp_dir(self): - """Checking existence of temp-dir and creating it, if necessary.""" + def check_log_dir(self): + """Checking existence of logging-dir and creating it, if necessary.""" - LOG.debug("Checking temporary directory {!r} ...".format(str(self.tmp_dir))) + LOG.debug("Checking logging directory {!r} ...".format(str(self.log_dir))) - if self.tmp_dir.exists(): - if self.tmp_dir.is_dir(): - LOG.debug("Temporary directory {!r} is already existing.".format( - str(self.tmp_dir))) + if self.log_dir.exists(): + if self.log_dir.is_dir(): + LOG.debug("Logging directory {!r} is already existing.".format( + str(self.log_dir))) else: msg = "Path {!r} is already existing, but is not a directory.".format( - str(self.tmp_dir)) + str(self.log_dir)) raise CommonLDAPMigrationError(msg) else: - LOG.info("Creating temporary directory {!r} ...".format(str(self.tmp_dir))) - self.tmp_dir.mkdir(mode=0o755, exist_ok=True) + LOG.info("Creating logging directory {!r} ...".format(str(self.log_dir))) + self.log_dir.mkdir(mode=0o755, exist_ok=True) mod2check = os.R_OK | os.W_OK | os.X_OK euid = False if os.access in os.supports_effective_ids: euid = True - if not os.access(str(self.tmp_dir), mod2check, effective_ids=euid): - msg = "Insufficient access rights to temporary directory {!r}.".format( - str(self.tmp_dir)) + if not os.access(str(self.log_dir), mod2check, effective_ids=euid): + msg = "Insufficient access rights to logging directory {!r}.".format( + str(self.log_dir)) raise CommonLDAPMigrationError(msg) if self.verbose > 1: - LOG.debug("Access to {!r} is okay.".format(str(self.tmp_dir))) + LOG.debug("Access to {!r} is okay.".format(str(self.log_dir))) # ------------------------------------------------------------------------- def lookup_for_attrtype(self, attrtype, silent=True): @@ -1464,7 +1492,6 @@ class LDAPMigrationApplication(BaseApplication): self.connect_source() self.connect_target() self.discover_target_schema() - self.check_tmp_dir() self.get_all_dns() self.get_structural_dns() self.migrate_entries() diff --git a/log/.keep b/log/.keep new file mode 100644 index 0000000..e69de29