Best Python code snippet using slash
log.py
Source:log.py  
...133    @contextmanager134    def _get_file_logging_context(self, filename_template, symlink):135        with ExitStack() as stack:136            handler = stack.enter_context(self._file_handler_cleanup_context(\137                                                self._log_file_handler_context(filename_template, symlink, \138                                                                               use_compression=config.root.log.compression.enabled)))139            if config.root.log.compression.enabled and config.root.log.compression.use_rotating_raw_file:140                stack.enter_context(self._file_handler_cleanup_context(self._log_file_handler_context(filename_template, symlink, \141                                                                       bubble=True, use_rotation=True)))142            stack.enter_context(self.console_handler.applicationbound())143            stack.enter_context(self.warnings_handler.applicationbound())144            stack.enter_context(self._file_handler_cleanup_context(self._get_error_logging_context()))145            stack.enter_context(self._get_silenced_logs_context())146            if config.root.log.unittest_mode:147                stack.enter_context(logbook.StreamHandler(sys.stderr, bubble=True, level=logbook.TRACE))148            for extra_handler in _extra_handlers:149                stack.enter_context(extra_handler.applicationbound())150            if config.root.log.unified_session_log and self.session_log_handler is not None:151                stack.enter_context(_make_bubbling_handler(self.session_log_handler))152            path = handler.stream.name if isinstance(handler, logbook.FileHandler) else None153            yield handler, path154    def _should_delete_log(self, result):155        return (not config.root.log.cleanup.keep_failed) or \156               (not result.is_global_result() and result.is_success(allow_skips=True)) or \157               (result.is_global_result() and self.session.results.is_success(allow_skips=True))158    @contextmanager159    def _get_error_logging_context(self):160        with ExitStack() as stack:161            path = config.root.log.errors_subpath162            if path:163                warn_deprecation('log.errors_subpath configuration is deprecated since 1.5.0. '164                                 'Please use log.highlights_subpath instead')165            else:166                path = config.root.log.highlights_subpath167            def _error_added_filter(record, handler): # pylint: disable=unused-argument168                return record.extra.get('highlight')169            handler = stack.enter_context(self._log_file_handler_context(path, symlink=None, bubble=True, filter=_error_added_filter))170            log_path = handler.stream.name if isinstance(handler, logbook.FileHandler) else None171            if log_path and self.session.results.current is self.session.results.global_result:172                self.session.results.global_result.add_extra_log_path(log_path)173            yield handler174    def _get_silenced_logs_context(self):175        if not config.root.log.silence_loggers:176            return ExitStack()177        return SilencedLoggersHandler(config.root.log.silence_loggers).applicationbound()178    @contextmanager179    def _file_handler_cleanup_context(self, handler_ctx):180        result = context.result181        path = None182        try:183            with handler_ctx as handler:184                path = handler.stream.name if isinstance(handler, logbook.FileHandler) else None185                with handler.applicationbound():186                    yield handler187        finally:188            if path is not None and self._log_path_to_handler[path] is None:189                hooks.log_file_closed(path=path, result=result)  # pylint: disable=no-member190                if config.root.log.cleanup.enabled and self._should_delete_log(result):191                    os.remove(path)192                    dir_path = os.path.dirname(path)193                    logs_root_dir = self._normalize_path(config.root.log.root)194                    if not os.listdir(dir_path) and logs_root_dir != dir_path:195                        os.rmdir(dir_path)196    def _get_log_file_path(self, subpath, use_compression):197        log_path = self._normalize_path(os.path.join(config.root.log.root, _format_log_path(subpath)))198        if use_compression:199            if config.root.log.compression.algorithm == "gzip":200                log_path += ".gz"201            elif config.root.log.compression.algorithm == "brotli":202                log_path += ".brotli"203            else:204                raise InvalidConfiguraion("Unsupported compression method: {}".format(config.root.log.compression.algorithm))205        return log_path206    def _create_log_file_handler(self, log_path, bubble=False, filter=_slash_logs_filter, use_compression=False, use_rotation=False):207        kwargs = {"bubble": bubble, "filter": filter}208        if use_compression:209            if config.root.log.compression.algorithm == "gzip":210                handler_class = logbook.GZIPCompressionHandler211            elif config.root.log.compression.algorithm == "brotli":212                handler_class = logbook.BrotliCompressionHandler213        elif use_rotation:214            kwargs.update({"max_size": 4*1024**2, "backup_count": 1})215            handler_class = logbook.RotatingFileHandler216        elif config.root.log.colorize:217            handler_class = ColorizedFileHandler218        else:219            handler_class = logbook.FileHandler220        return handler_class(log_path, **kwargs)221    @contextmanager222    def _log_file_handler_context(self, subpath, symlink, bubble=False, filter=_slash_logs_filter, use_compression=False, use_rotation=False):223        if subpath is None or config.root.log.root is None:224            if bubble:225                handler = NoopHandler()226            else:227                handler = logbook.NullHandler(filter=filter)228            yield handler229        else:230            log_path = self._get_log_file_path(subpath, use_compression)231            handler = self._log_path_to_handler.get(log_path, None)232            if handler is not None:233                yield handler234            else:235                ensure_containing_directory(log_path)236                if symlink:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
