Python logzero.logger.error() Examples

The following are 30 code examples of logzero.logger.error(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module logzero.logger , or try the search function .
Example #1
Source File: BinaryReader.py    From neo-python-core with MIT License 6 votes vote down vote up
def ReadByte(self, do_ord=True):
        """
        Read a single byte.

        Args:
            do_ord (bool): (default True) convert the byte to an ordinal first.

        Returns:
            bytes: a single byte if successful. 0 (int) if an exception occurred.
        """
        try:
            if do_ord:
                return ord(self.stream.read(1))
            return self.stream.read(1)
        except Exception as e:
            logger.error("ord expected character but got none")
        return 0 
Example #2
Source File: settings.py    From chaostoolkit-lib with Apache License 2.0 6 votes vote down vote up
def load_settings(settings_path: str = CHAOSTOOLKIT_CONFIG_PATH) -> Settings:
    """
    Load chaostoolkit settings as a mapping of key/values or return `None`
    when the file could not be found.
    """
    if not os.path.exists(settings_path):
        logger.debug("The Chaos Toolkit settings file could not be found at "
                     "'{c}'.".format(c=settings_path))
        return

    with open(settings_path) as f:
        try:
            settings = yaml.safe_load(f.read())
            loaded_settings.set(settings)
            return settings
        except yaml.YAMLError as ye:
            logger.error("Failed parsing YAML settings: {}".format(str(ye))) 
Example #3
Source File: utils.py    From ansible-tools with Apache License 2.0 6 votes vote down vote up
def download_versions(self, version=None):
        rr = requests.get(self.RELEASES_URL)
        soup = BeautifulSoup(rr.text, features='html.parser')
        hrefs = soup.findAll('a')
        hrefs = [x.attrs['href'] for x in hrefs]
        hrefs = [x for x in hrefs if x.endswith('.gz')]
        hrefs = [x for x in hrefs if 'latest' not in x]

        # filter by specific version if requested
        if version:
            hrefs = [x for x in hrefs if version in x]

        for href in hrefs:
            dst = os.path.join(self.cachedir, 'tars', href)
            src = os.path.join(self.RELEASES_URL, href)
            if not os.path.exists(dst):
                logger.debug('%s -> %s' % (dst,src))
                res = curl('-o', dst, src)
                if res.exit_code != 0:
                    logger.error('Failed to download %s to %s' % (src, dst))
                    logger.error(res.stdout)
                    logger.error(res.stderr)
                    sys.exit(1) 
Example #4
Source File: cli.py    From chaostoolkit with Apache License 2.0 6 votes vote down vote up
def validate(ctx: click.Context, source: str,
             no_verify_tls: bool = False) -> Experiment:
    """Validate the experiment at SOURCE."""
    settings = load_settings(ctx.obj["settings_path"])

    try:
        experiment = load_experiment(
            source, settings, verify_tls=not no_verify_tls)
    except InvalidSource as x:
        logger.error(str(x))
        logger.debug(x)
        ctx.exit(1)

    try:
        notify(settings, ValidateFlowEvent.ValidateStarted, experiment)
        ensure_experiment_is_valid(experiment)
        notify(settings, ValidateFlowEvent.ValidateCompleted, experiment)
        logger.info("experiment syntax and semantic look valid")
    except ChaosException as x:
        notify(settings, ValidateFlowEvent.ValidateFailed, experiment, x)
        logger.error(str(x))
        logger.debug(x)
        ctx.exit(1)

    return experiment 
Example #5
Source File: idb.py    From atxserver2-ios-provider with MIT License 6 votes vote down vote up
def wda_screenshot_ok(self):
        """
        Check if screenshot is working
        Returns:
            bool
        """
        try:
            request = httpclient.HTTPRequest(self.wda_device_url +
                                             "/screenshot",
                                             connect_timeout=3,
                                             request_timeout=15)
            client = httpclient.AsyncHTTPClient()
            resp = await client.fetch(request)
            data = json.loads(resp.body)
            raw_png_data = base64.b64decode(data['value'])
            png_header = b"\x89PNG\r\n\x1a\n"
            if not raw_png_data.startswith(png_header):
                return False
            return True
        except Exception as e:
            logger.warning("%s wda screenshot error: %s", self, e)
            return False 
Example #6
Source File: idb.py    From atxserver2-ios-provider with MIT License 6 votes vote down vote up
def wda_status(self):
        """
        Returns:
            dict or None
        """
        try:
            request = httpclient.HTTPRequest(self.wda_device_url + "/status",
                                             connect_timeout=3,
                                             request_timeout=15)
            client = httpclient.AsyncHTTPClient()
            resp = await client.fetch(request)
            info = json.loads(resp.body)
            self.__wda_info = info
            return info
        except httpclient.HTTPError as e:
            logger.debug("%s request wda/status error: %s", self, e)
            return None
        except (ConnectionResetError, ConnectionRefusedError):
            logger.debug("%s waiting for wda", self)
            return None
        except Exception as e:
            logger.warning("%s ping wda unknown error: %s %s", self, type(e),
                           e)
            return None 
Example #7
Source File: test_storage.py    From neo-boa with MIT License 5 votes vote down vote up
def tearDownClass(cls):
        super(BoaFixtureTest, cls).tearDownClass()
        try:
            if os.path.exists(settings.debug_storage_leveldb_path):

                shutil.rmtree(settings.debug_storage_leveldb_path)
            else:
                logger.error("debug storage path doesn't exist")
        except Exception as e:
            logger.error("couldn't remove debug storage %s " % e) 
Example #8
Source File: ECCurve.py    From neo-python-core with MIT License 5 votes vote down vote up
def sqrtCQ(val, CQ):
    if test_bit(CQ, 1):
        z = modpow(val, (CQ >> 2) + 1, CQ)
        zsquare = (z * z) % CQ
        if (z * z) % CQ == val:
            return z
        else:
            return None

    qMinusOne = CQ - 1
    legendreExponent = qMinusOne >> 1
    if modpow(val, legendreExponent, CQ) != 1:
        logger.error("legendaire exponent error")
        return None

    u = qMinusOne >> 2
    k = (u << 1) + 1
    Q = val
    fourQ = (Q << 2) % CQ
    U = None
    V = None

    while U == 1 or U == qMinusOne:

        P = next_random_integer(CQ.bit_length())
        while P >= CQ or modpow(P * P - fourQ, legendreExponent, CQ) != qMinusOne:
            P = next_random_integer(CQ.bit_length())

        U, V = _lucas_sequence(CQ, P, Q, k)
        if (V * V) % CQ == fourQ:

            if test_bit(V, 0):
                V += CQ

            V >>= 1
            assert (V * V) % CQ == val
            return V

    return None 
Example #9
Source File: Crypto.py    From neo-python-core with MIT License 5 votes vote down vote up
def VerifySignature(message, signature, public_key, unhex=True):
        """
        Verify the integrity of the message.

        Args:
            message (str): the message to verify.
            signature (bytearray): the signature belonging to the message.
            public_key (ECPoint|bytes): the public key to use for verifying the signature. If `public_key` is of type bytes then it should be raw bytes (i.e. b'\xAA\xBB').
            unhex (bool): whether the message should be unhexlified before verifying

        Returns:
            bool: True if verification passes. False otherwise.
        """

        if type(public_key) is EllipticCurve.ECPoint:
            pubkey_x = public_key.x.value.to_bytes(32, 'big')
            pubkey_y = public_key.y.value.to_bytes(32, 'big')

            public_key = pubkey_x + pubkey_y

        if unhex:
            try:
                message = binascii.unhexlify(message)
            except Exception as e:
                logger.error("could not get m: %s" % e)
        elif isinstance(message, str):
            message = message.encode('utf-8')

        if len(public_key) == 33:
            public_key = bitcoin.decompress(public_key)
            public_key = public_key[1:]

        try:
            vk = VerifyingKey.from_string(public_key, curve=NIST256p, hashfunc=hashlib.sha256)
            res = vk.verify(signature, message, hashfunc=hashlib.sha256)
            return res
        except Exception as e:
            pass

        return False 
Example #10
Source File: BinaryReader.py    From neo-python-core with MIT License 5 votes vote down vote up
def ReadSerializableArray(self, class_name, max=sys.maxsize):
        """
        Deserialize a stream into the object specific by `class_name`.

        Args:
            class_name (str): a full path to the class to be deserialized into. e.g. 'neo.Core.Block.Block'
            max (int): (Optional) maximum number of bytes to read.

        Returns:
            list: list of `class_name` objects deserialized from the stream.
        """
        module = '.'.join(class_name.split('.')[:-1])
        klassname = class_name.split('.')[-1]
        klass = getattr(importlib.import_module(module), klassname)
        length = self.ReadVarInt(max=max)
        items = []
        #        logger.info("READING ITEM %s %s " % (length, class_name))
        try:
            for i in range(0, length):
                item = klass()
                item.Deserialize(self)
                #                logger.info("deserialized item %s %s " % ( i, item))
                items.append(item)
        except Exception as e:
            logger.error("Couldn't deserialize %s " % e)

        return items 
Example #11
Source File: run_ssh_cmd.py    From ansible-tools with Apache License 2.0 5 votes vote down vote up
def error(self, msg):
        print(msg) 
Example #12
Source File: utils.py    From ansible-tools with Apache License 2.0 5 votes vote down vote up
def extract_versions(self, version=None):
        tarballs = glob.glob('%s/tars/*.gz' % self.cachedir)
        if version:
            tarballs = [x for x in tarballs if version in x]
        for tarball in tarballs:
            dst = os.path.join(
                self.extractdir,
                os.path.basename(tarball).replace('.tar.gz', '')
            )
            if not os.path.exists(dst):
                # extract to temp dir first to avoid clobbering
                temp_dst = dst + '.tmp'
                if os.path.exists(temp_dst):
                    shutil.rmtree(temp_dst)
                os.makedirs(temp_dst)
                logger.debug('tar xzf %s -C %s' % (tarball, temp_dst))
                try:
                    res = tar('xzf', tarball, '-C', temp_dst)
                except Exception as e:
                    logger.error(e)
                    sys.exit(1)
                # what was the extracted root path?
                edirs = glob.glob('%s/*' % temp_dst)
                srcdir = edirs[0]

                # move the extract to the right place
                shutil.move(srcdir, dst)
                shutil.rmtree(temp_dst) 
Example #13
Source File: utils.py    From ansible-tools with Apache License 2.0 5 votes vote down vote up
def build_cache_dirs(self):
        cachedirs = [
            self.cachedir,
            os.path.join(self.cachedir, 'tars'),
            os.path.join(self.cachedir, 'extracted')
        ]
        for cachedir in cachedirs:
            if not os.path.exists(cachedir):
                try:
                    os.makedirs(cachedir)
                except PermissionError as e:
                    logger.error('You must manually create the path "%s"' % cachedir)
                    sys.exit(1) 
Example #14
Source File: utils.py    From svtools with MIT License 5 votes vote down vote up
def verify_download(self, localpath, cloud_md5):
        logger.info("Verifying download")
        if not self.md5s_match(localpath, cloud_md5):
            msg = "Corrupted download: {} -- expected MD5: {}"
            msg = msg.format(localpath, cloud_md5)
            logger.error(msg)
            sys.exit("[err] : {}".format(msg)) 
Example #15
Source File: idb.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def watch_wda_status(self):
        """
        check WebDriverAgent all the time
        """
        # check wda_status every 30s
        fail_cnt = 0
        last_ip = self.device_ip
        while not self._stop.is_set():
            if await self.wda_status():
                if fail_cnt != 0:
                    logger.info("wda ping recovered")
                    fail_cnt = 0
                if last_ip != self.device_ip:
                    last_ip = self.device_ip
                    await self._callback(self.status_ready, self.__wda_info)
                await self._sleep(60)
                logger.debug("%s is fine", self)
            else:
                fail_cnt += 1
                logger.warning("%s wda ping error: %d", self, fail_cnt)
                if fail_cnt > 3:
                    logger.warning("ping wda fail too many times, restart wda")
                    break
                await self._sleep(10)

        self.destroy() 
Example #16
Source File: idb.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def run_wda_forever(self):
        """
        Args:
            callback
        """
        wda_fail_cnt = 0
        while not self._stop.is_set():
            await self._callback(self.status_preparing)
            start = time.time()
            ok = await self.run_webdriveragent()
            if not ok:
                self.destroy()

                wda_fail_cnt += 1
                if wda_fail_cnt > 3:
                    logger.error("%s Run WDA failed. -_-!", self)
                    break

                if time.time() - start < 3.0:
                    logger.error("%s WDA unable to start", self)
                    break
                logger.warning("%s wda started failed, retry after 10s", self)
                if not await self._sleep(10):
                    break
                continue

            wda_fail_cnt = 0
            logger.info("%s wda lanuched", self)

            # wda_status() result stored in __wda_info
            await self._callback(self.status_ready, self.__wda_info)
            await self.watch_wda_status()

        await self._callback(self.status_fatal)
        self.destroy()  # destroy twice to make sure no process left
        self._finished.set()  # no need await 
Example #17
Source File: idb.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def runcommand(*args) -> str:
    try:
        output = subprocess.check_output(args)
        return output.strip().decode('utf-8')
    except (subprocess.CalledProcessError, FileNotFoundError):
        return ""
    except Exception as e:
        logger.warning("unknown error: %s", e)
        return "" 
Example #18
Source File: main.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def post(self, udid=None):
        udid = udid or self.get_argument('udid', None)
        assert udid
        d = idevices.get(udid)
        try:
            if not d:
                raise Exception("Device not found")

            d.restart_wda_proxy() # change wda public port
            wda_url = "http://{}:{}".format(current_ip(), d.public_port)
            await d.wda_healthcheck()
            await hbc.device_update({
                "udid": udid,
                "colding": False,
                "provider": {
                    "wdaUrl": wda_url,
                }
            })
            self.write({
                "success": True,
                "description": "Device successfully colded"
            })
        except Exception as e:
            logger.warning("colding procedure got error: %s", e)
            self.set_status(400)  # bad request
            self.write({
                "success": False,
                "description": "udid: %s not found" % udid
            }) 
Example #19
Source File: CThermal.py    From Thermal_Image_Analysis with MIT License 5 votes vote down vote up
def get_thermal_image_from_file(thermal_input, thermal_class=CFlir, colormap=None):
    '''
    Function to get the image associated with each RJPG file using the FLIR Thermal base class CFlir
    Saves the thermal images in the same place as the original RJPG
    '''
    CThermal = thermal_class

    inputpath = Path(thermal_input)
    if Path.is_dir(inputpath):
        rjpg_img_paths = list(Path(input_folder).glob('*R.JPG'))
        fff_file_paths = list(Path(input_folder).glob('*.fff'))
        if len(rjpg_img_paths)>0:
            for rjpg_img in tqdm(rjpg_img_paths, total=len(rjpg_img_paths)):
                thermal_obj = CThermal(rjpg_img, color_map=colormap)
                path_wo_ext = str(rjpg_img).replace('_R'+rjpg_img.suffix,'') 
                thermal_obj.save_thermal_image(path_wo_ext+'.jpg')

        elif len(fff_file_paths)>0:
            for fff in tqdm(fff_file_paths, total=len(fff_file_paths)):
                save_image_path = str(fff).replace('.fff','.jpg')
                thermal_obj = CThermal(fff, color_map=colormap)
                thermal_obj.save_thermal_image(save_image_path)
        else:
            logger.error('Input folder contains neither fff or RJPG files')

    elif Path.is_file(inputpath):
        thermal_obj = CThermal(thermal_input, color_map=colormap)
        path_wo_ext = Path.as_posix(inputpath).replace(inputpath.suffix,'')
        thermal_obj.save_thermal_image(path_wo_ext+'.jpg')

    else:
        logger.error('Path given is neither file nor folder. Please check') 
Example #20
Source File: botlist.py    From BotListBot with MIT License 5 votes vote down vote up
def send_or_edit(self, text, message_id, reply_markup=None):
        sleep(3)
        try:
            if self.resend:
                return util.send_md_message(self.bot, self.channel.chat_id, text, timeout=120,
                                            disable_notification=True, reply_markup=reply_markup)
            else:
                if reply_markup:
                    return self.bot.formatter.send_or_edit(self.channel.chat_id, text,
                                                           to_edit=message_id,
                                                           timeout=120,
                                                           disable_web_page_preview=True,
                                                           disable_notification=True,
                                                           reply_markup=reply_markup)
                else:
                    return self.bot.formatter.send_or_edit(self.channel.chat_id, text,
                                                           to_edit=message_id,
                                                           timeout=120,
                                                           disable_web_page_preview=True,
                                                           disable_notification=True)
        except BadRequest as e:
            if 'chat not found' in e.message.lower():
                self.notify_admin_err(
                    "I can't reach BotList Bot with chat-id `{}` (CHAT NOT FOUND error). "
                    "There's probably something wrong with the database.".format(
                        self.channel.chat_id))
                raise e
            if 'message not modified' in e.message.lower():
                return None
            else:
                log.error(e)
                raise e 
Example #21
Source File: botchecker.py    From BotListBot with MIT License 5 votes vote down vote up
def download_profile_photo(self, bot: BotModel, photo_path):
        tmp_file = os.path.join(TMP_DIR, bot.username.replace('@', '') + '.jpg')
        photos = self.get_user_profile_photos(bot.chat_id).photos
        if photos:
            photo_size_object = photos[0][-1]

            await self.__photos_lock.acquire()
            try:
                try:
                    self.download_media(
                        photo_size_object,
                        file_name=tmp_file,
                        block=True
                    )
                except FloodWait as e:
                    # TODO: as the error happens inside of the update worker, this won't work (yet)
                    # Leaving it in as the default behavior should be to raise the FloodWait
                    # when block=True
                    log.debug(f"FloodWait for downloading media ({e.x})")

                if os.path.exists(tmp_file):
                    try:
                        similar = filecmp.cmp(tmp_file, photo_path, shallow=False)
                    except FileNotFoundError:
                        similar = False

                    if not similar:
                        shutil.copy(tmp_file, photo_path)
            finally:
                self.__photos_lock.release() 
Example #22
Source File: botchecker.py    From BotListBot with MIT License 5 votes vote down vote up
def resolve_bot(self, bot: BotModel):
        if bot.chat_id:
            try:
                return self.resolve_peer(bot.chat_id)
            except:
                pass

        try:
            results = self.send(Search(bot.username, limit=3))
            if results.users:
                try:
                    return next(
                        s for s in results.users if s.username.lower() == bot.username[1:].lower())
                except StopIteration:
                    pass
        except QueryTooShort:
            pass

        if self.username_flood_until:
            if self.username_flood_until < datetime.now():
                self.username_flood_until = None
        else:
            try:
                return self.resolve_peer(bot.username)
            except FloodWait as e:
                self.username_flood_until = datetime.now() + timedelta(
                    seconds=e.x)
                log.warning("Flood wait for ResolveUsername: {}s (until {})".format(
                    e.x, self.username_flood_until))
            except UsernameInvalid as e:
                log.error(e)  # TODO

        return None 
Example #23
Source File: botchecker.py    From BotListBot with MIT License 5 votes vote down vote up
def schedule_conversation_deletion(self, peer, delay=5):
        await asyncio.sleep(delay)
        self.send(DeleteHistory(await self.resolve_peer(peer), max_id=999999999, just_clear=True))
        log.debug("Deleted conversation with {}".format(peer))

    # def delete_all_conversations(self):
    #     all_peers = [utils.resolve_id(x[0]) for x in self.session.entities.get_input_list()]
    #     for peer in all_peers:
    #         log.debug("Deleting conversation with {}...".format(peer))
    #         try:
    #             input_entity = self.client.session.entities.get_input_entity(peer[0])
    #             self.client(DeleteHistoryRequest(input_entity, max_id=9999999999999999))
    #         except:
    #             log.error("Couldn't find {}".format(peer[0])) 
Example #24
Source File: helpers.py    From BotListBot with MIT License 5 votes vote down vote up
def get_commands():
    commands = ""
    try:
        with open("files/commands.txt", "rb") as file:
            for command in file.readlines():
                commands += "/" + command.decode("utf-8")
        return commands
    except FileNotFoundError:
        log.error("File could not be opened.") 
Example #25
Source File: test_ico_template.py    From neo-boa with MIT License 5 votes vote down vote up
def tearDownClass(cls):
        super(BoaFixtureTest, cls).tearDownClass()
        try:
            if os.path.exists(settings.debug_storage_leveldb_path):

                shutil.rmtree(settings.debug_storage_leveldb_path)
            else:
                logger.error("debug storage path doesn't exist")
        except Exception as e:
            logger.error("couldn't remove debug storage %s " % e) 
Example #26
Source File: test_contract.py    From neo-boa with MIT License 5 votes vote down vote up
def tearDownClass(cls):
        super(BoaFixtureTest, cls).tearDownClass()
        try:
            if os.path.exists(settings.debug_storage_leveldb_path):

                shutil.rmtree(settings.debug_storage_leveldb_path)
            else:
                logger.error("debug storage path doesn't exist")
        except Exception as e:
            logger.error("couldn't remove debug storage %s " % e) 
Example #27
Source File: main.py    From atxserver2-ios-provider with MIT License 4 votes vote down vote up
def _device_callback(d: idb.WDADevice,
                           status: str,
                           info: Union[dict, None] = None):
    """ monitor device status """
    wd = idb.WDADevice

    if status == wd.status_preparing:
        await hbc.device_update({
            "udid": d.udid,
            "provider": None,  # no provider indicate not present
            "colding": False,
            "properties": {
                "name": d.name,
                "product": d.product,
                "brand": "Apple",
            }
        })
    elif status == wd.status_ready:
        logger.debug("%s %s", d, "healthcheck passed")

        assert isinstance(info, dict)
        info = defaultdict(dict, info)

        await hbc.device_update({
            # "colding": False,
            "udid": d.udid,
            "provider": {
                "wdaUrl": "http://{}:{}".format(current_ip(), d.public_port)
            },
            "properties": {
                "ip": info['value']['ios']['ip'],
                "version": info['value']['os']['version'],
                "sdkVersion": info['value']['os']['sdkVersion'],
            }
        })  # yapf: disable
    elif status == wd.status_fatal:
        await hbc.device_update({
            "udid": d.udid,
            "provider": None,
        })
    else:
        logger.error("Unknown status: %s", status) 
Example #28
Source File: __init__.py    From ver-observer with GNU General Public License v3.0 4 votes vote down vote up
def format(self, record):
        try:
            message = record.getMessage()
            assert isinstance(message,
                              basestring_type)  # guaranteed by logging
            # Encoding notes:  The logging module prefers to work with character
            # strings, but only enforces that log messages are instances of
            # basestring.  In python 2, non-ascii bytestrings will make
            # their way through the logging framework until they blow up with
            # an unhelpful decoding error (with this formatter it happens
            # when we attach the prefix, but there are other opportunities for
            # exceptions further along in the framework).
            #
            # If a byte string makes it this far, convert it to unicode to
            # ensure it will make it out to the logs.  Use repr() as a fallback
            # to ensure that all byte strings can be converted successfully,
            # but don't do it by default so we don't add extra quotes to ascii
            # bytestrings.  This is a bit of a hacky place to do this, but
            # it's worth it since the encoding errors that would otherwise
            # result are so useless (and tornado is fond of using utf8-encoded
            # byte strings whereever possible).
            record.message = _safe_unicode(message)
        except Exception as e:
            record.message = "Bad message (%r): %r" % (e, record.__dict__)

        record.asctime = self.formatTime(record, self.datefmt)

        if record.levelno in self._colors:
            record.color = self._colors[record.levelno]
            record.end_color = self._normal
        else:
            record.color = record.end_color = ''

        formatted = self._fmt % record.__dict__

        if record.exc_info:
            if not record.exc_text:
                record.exc_text = self.formatException(record.exc_info)
        if record.exc_text:
            # exc_text contains multiple lines.  We need to _safe_unicode
            # each line separately so that non-utf8 bytes don't cause
            # all the newlines to turn into '\n'.
            lines = [formatted.rstrip()]
            lines.extend(
                _safe_unicode(ln) for ln in record.exc_text.split('\n'))
            formatted = '\n'.join(lines)
        return formatted.replace("\n", "\n    ") 
Example #29
Source File: __init__.py    From logzero with MIT License 4 votes vote down vote up
def format(self, record):
        try:
            message = record.getMessage()
            assert isinstance(message,
                              basestring_type)  # guaranteed by logging
            # Encoding notes:  The logging module prefers to work with character
            # strings, but only enforces that log messages are instances of
            # basestring.  In python 2, non-ascii bytestrings will make
            # their way through the logging framework until they blow up with
            # an unhelpful decoding error (with this formatter it happens
            # when we attach the prefix, but there are other opportunities for
            # exceptions further along in the framework).
            #
            # If a byte string makes it this far, convert it to unicode to
            # ensure it will make it out to the logs.  Use repr() as a fallback
            # to ensure that all byte strings can be converted successfully,
            # but don't do it by default so we don't add extra quotes to ascii
            # bytestrings.  This is a bit of a hacky place to do this, but
            # it's worth it since the encoding errors that would otherwise
            # result are so useless (and tornado is fond of using utf8-encoded
            # byte strings wherever possible).
            record.message = _safe_unicode(message)
        except Exception as e:
            record.message = "Bad message (%r): %r" % (e, record.__dict__)

        record.asctime = self.formatTime(record, self.datefmt)

        if record.levelno in self._colors:
            record.color = self._colors[record.levelno]
            record.end_color = self._normal
        else:
            record.color = record.end_color = ''

        formatted = self._fmt % record.__dict__

        if record.exc_info:
            if not record.exc_text:
                record.exc_text = self.formatException(record.exc_info)
        if record.exc_text:
            # exc_text contains multiple lines.  We need to _safe_unicode
            # each line separately so that non-utf8 bytes don't cause
            # all the newlines to turn into '\n'.
            lines = [formatted.rstrip()]
            lines.extend(
                _safe_unicode(ln) for ln in record.exc_text.split('\n'))
            formatted = '\n'.join(lines)
        return formatted.replace("\n", "\n    ") 
Example #30
Source File: cli.py    From chaostoolkit with Apache License 2.0 4 votes vote down vote up
def run(ctx: click.Context, source: str, journal_path: str = "./journal.json",
        dry: bool = False, no_validation: bool = False,
        no_exit: bool = False, no_verify_tls: bool = False) -> Journal:
    """Run the experiment loaded from SOURCE, either a local file or a
       HTTP resource. SOURCE can be formatted as JSON or YAML."""
    settings = load_settings(ctx.obj["settings_path"]) or {}
    has_deviated = False
    has_failed = False

    load_global_controls(settings)

    try:
        experiment = load_experiment(
            source, settings, verify_tls=not no_verify_tls)
    except InvalidSource as x:
        logger.error(str(x))
        logger.debug(x)
        ctx.exit(1)

    notify(settings, RunFlowEvent.RunStarted, experiment)

    if not no_validation:
        try:
            ensure_experiment_is_valid(experiment)
        except ChaosException as x:
            logger.error(str(x))
            logger.debug(x)
            ctx.exit(1)

    experiment["dry"] = dry

    journal = run_experiment(experiment, settings=settings)
    has_deviated = journal.get("deviated", False)
    has_failed = journal["status"] != "completed"

    with io.open(journal_path, "w") as r:
        json.dump(
            journal, r, indent=2, ensure_ascii=False, default=encoder)

    if journal["status"] == "completed":
        notify(settings, RunFlowEvent.RunCompleted, journal)
    elif has_failed:
        notify(settings, RunFlowEvent.RunFailed, journal)

        if has_deviated:
            notify(settings, RunFlowEvent.RunDeviated, journal)

    if (has_failed or has_deviated) and not no_exit:
        ctx.exit(1)

    return journal