def _stop_and_exec_error_dialog(self, err): self.mdbx.stop_sync() share, auto_share = show_stacktrace_dialog( err["traceback"], ask_share=not self.mdbx.get_conf("app", "analytics")) if share: import bugsnag bugsnag.configure( api_key="081c05e2bf9730d5f55bc35dea15c833", app_version=__version__, auto_notify=False, auto_capture_sessions=False, ) bugsnag.notify(RuntimeError(err["type"]), meta_data={ "system": { "platform": platform.platform(), "python": platform.python_version(), "gui": QtCore.PYQT_VERSION_STR, "desktop": DESKTOP, }, "error": err, }) if auto_share: self.mdbx.set_conf("app", "analytics", True)
def setUp(self): super(TestWSGI, self).setUp() bugsnag.configure(endpoint=self.server.url, api_key='3874876376238728937', notify_release_stages=['dev'], release_stage='dev', asynchronous=False)
def _enable_bugsnag_logger(formatter: logging.Formatter) -> None: """ Attaches a `bugsnag.handlers.BugsnagHandler` to the current `logging.Logger`. The handler is configured based on the following environment variables: - `BUGSNAG_API_KEY` - `BUGSNAG_RELEASE_STAGE` The logging level of this handler is set to `logging.WARNING`. No handler is configured or attached if the `BUGSNAG_ENABLE` environment variable is set to `'false'`. :param logging.Formatter formatter: The logging formatter to attach to the handler """ if 'false' == os.getenv('BUGSNAG_ENABLE'): return import bugsnag bugsnag.configure( api_key=os.getenv('BUGSNAG_API_KEY'), app_version=get_version(), project_root=root, release_stage=os.getenv('BUGSNAG_RELEASE_STAGE'), notify_release_stages=['production', 'staging', 'testing']) bugsnag_logger = bugsnag.handlers.BugsnagHandler() bugsnag_logger.setLevel(logging.WARNING) bugsnag_logger.setFormatter(formatter) logging.getLogger().addHandler(bugsnag_logger)
def setUp(self): super(TestBottle, self).setUp() bugsnag.configure(endpoint=self.server.url, session_endpoint=self.server.url, auto_capture_sessions=False, api_key='3874876376238728937', asynchronous=False)
def start_mqtt(): global mqtt_client try: with open('env.json') as config_file: all_configs = json.load(config_file) main_env = all_configs["main"] bugsnag.configure(api_key=main_env["BUGSNAG_KEY"]) mqtt_username = main_env["MQTT_USERNAME"] mqtt_password = main_env["MQTT_PASSWORD"] mqtt_host = main_env["MQTT_HOST"] mqtt_port = main_env["MQTT_PORT"] mqtt_topics = main_env["MQTT_TOPICS"] mqtt_publish_topic = main_env["PUBLISH_TOPIC"] mqtt_client = MqttClient("pi_connect", "Random", [ (i, 0) for i in mqtt_topics], mqtt_publish_topic) except IOError as error: # if not config file found exit print("ENV Json file not found, Exiting") print(error) exit(1) except: print("Error while loading config file from json") exit(1) # print(main_env) mqtt_client.create_client() mqtt_client.client.username_pw_set(mqtt_username, mqtt_password) mqtt_client.client.on_connect = mqtt_client.on_connect_handler mqtt_client.client.on_disconnect = mqtt_client.on_disconnect_handler mqtt_client.client.on_subscribe = mqtt_client.on_subscribe_handler mqtt_client.client.on_message = mqtt_client.on_message_handler mqtt_client.client.connect(mqtt_host, mqtt_port, keepalive=60) mqtt_client.start()
def configure(): # Import Bugsnag settings from settings.py django_bugsnag_settings = getattr(settings, 'BUGSNAG', {}) bugsnag.configure(**django_bugsnag_settings) # Ignore django 404s by default bugsnag.configuration.ignore_classes.append("django.http.Http404")
def _stop_and_exec_error_dialog(self, err): self.mdbx.stop_sync() share, auto_share = show_stacktrace_dialog( err['traceback'], ask_share=not self.mdbx.analytics ) if share: import bugsnag bugsnag.configure( api_key='081c05e2bf9730d5f55bc35dea15c833', app_version=__version__, auto_notify=False, auto_capture_sessions=False, ) bugsnag.notify( RuntimeError(err['type']), meta_data={ 'system': { 'platform': platform.platform(), 'python': platform.python_version(), 'gui': f'Qt {QtCore.PYQT_VERSION_STR}', 'desktop': DESKTOP, }, 'original exception': err, } ) self.mdbx.analytics = self.mdbx.analytics or auto_share
def test_uncaught_exception_on_thread_sends_event(bugsnag_server): """ Python 3.8+ has an excepthook for spawned threads. This test checks that exceptions thrown by threads are handled. """ def process(): raise Terrible('oh no') def my_program(*args): return process() def callback(event): event.severity = "info" bugsnag.configure(app_type='dispatch') bugsnag.before_notify(callback) thread = threading.Thread(target=my_program) thread.start() thread.join() bugsnag_server.wait_for_request() event = bugsnag_server.received[0]['json_body']['events'][0] exception = event['exceptions'][0] assert 'dispatch' == event['app']['type'] assert 'info' == event['severity'] assert 'test_thread_excepthook.Terrible' == exception['errorClass'] assert 'oh no' == exception['message'] assert 'process' == exception['stacktrace'][0]['method'] assert exception['stacktrace'][0]['inProject'] assert 'my_program' == exception['stacktrace'][1]['method'] assert exception['stacktrace'][1]['inProject'] assert not exception['stacktrace'][2]['inProject']
def setUp(self): super(TestBugsnag, self).setUp() bugsnag.configure(endpoint=self.server.url, api_key='tomatoes', notify_release_stages=['dev'], release_stage='dev', asynchronous=False)
def configure_exceptions(app): if app.config['BUGSNAG_API_KEY']: # pragma: no cover bugsnag.configure( api_key=app.config['BUGSNAG_API_KEY'], project_root=app.config['ROOT'], ) handle_exceptions(app)
def __init__(self): settings = opt.options.group_dict('app') routes = [(ProtocolMatches('^http[s]?$'), list(self.http_routes)), (ProtocolMatches('^ws[s]?$'), list(self.ws_routes))] super(Application, self).__init__(routes, **settings) if settings.get('bugsnag'): # pragma: no cover bugsnag.configure(api_key=settings['bugsnag']['api_key'], project_root=settings['bugsnag']['project_root']) self.executor = tornado.concurrent.futures.ThreadPoolExecutor( settings['num_executors']) self.redis_pool = RedisPool(settings['redis_host'], settings['redis_port'], settings['redis_db'], settings['redis_password']) self.database = Database(settings['postgres_user'], settings['postgres_password'], settings['postgres_host'], settings['postgres_port'], settings['postgres_db']) self.event_mapper = EventMapper(self.database, self.redis_pool)
def create_celery_app(app=None, config="worker"): """ adapted from http://flask.pocoo.org/docs/0.10/patterns/celery/ """ app = app or create_app(config=config) celery.main = app.import_name celery.conf["BROKER_URL"] = app.config["CELERY_BROKER_URL"] celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask if not app.config["TESTING"]: connect_failure_handler() bugsnag.configure(ignore_classes=[ "webhookdb.exceptions.StaleData", "webhookdb.exceptions.NothingToDo", "webhookdb.exceptions.RateLimited", ]) return celery
def init(): # Configure the email exceptions info = cfg.email_exceptions if info and 'smtp_host' in info: mailhost = info['smtp_host'] mailport = info.get('smtp_port') if mailport: mailhost = (mailhost, mailport) smtp_secure = info.get('smtp_secure', None) secure_args = _adapt_smtp_secure(smtp_secure) mail_handler = logging.handlers.SMTPHandler( mailhost=mailhost, fromaddr=info['from_addr'], toaddrs=[info['to_addr']], subject='Docker registry exception', credentials=(info['smtp_login'], info['smtp_password']), secure=secure_args) mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler) # Configure bugsnag info = cfg.bugsnag if info: if not bugsnag: raise _bugsnag_import_error root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) bugsnag.configure(api_key=info, project_root=root_path, release_stage=cfg.flavor, notify_release_stages=[cfg.flavor], app_version=__version__ ) bugsnag.flask.handle_exceptions(app)
def init(): # Configure the email exceptions info = cfg.email_exceptions if info and info.smtp_host: mailhost = info.smtp_host mailport = info.smtp_port if mailport: mailhost = (mailhost, mailport) smtp_secure = info.smtp_secure secure_args = _adapt_smtp_secure(smtp_secure) mail_handler = logging.handlers.SMTPHandler( mailhost=mailhost, fromaddr=info.from_addr, toaddrs=[info.to_addr], subject='Docker registry exception', credentials=(info.smtp_login, info.smtp_password), secure=secure_args) mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler) # Configure bugsnag info = cfg.bugsnag if info: if not bugsnag: raise _bugsnag_import_error root_path = os.path.abspath( os.path.join(os.path.dirname(__file__), '..')) bugsnag.configure(api_key=info, project_root=root_path, release_stage=cfg.flavor, notify_release_stages=[cfg.flavor], app_version=__version__) bugsnag.flask.handle_exceptions(app)
def prepare(self): middleware = bugsnag.configure().internal_middleware bugsnag.configure().runtime_versions['tornado'] = tornado.version middleware.before_notify(self.add_tornado_request_to_notification) if bugsnag.configuration.auto_capture_sessions: bugsnag.start_session()
def test_notify_proxy(self): bugsnag.configure(proxy_host=self.server.url) bugsnag.notify(ScaryException('unexpected failover')) self.assertEqual(len(self.server.received), 1) self.assertEqual(self.server.received[0]['method'], 'POST') self.assertEqual(self.server.received[0]['path'].strip('/'), self.server.url)
def finish(o): if not o.DEBUG: import bugsnag bugsnag.configure( api_key = o.BUGSNAG_KEY , project_root = _env.PREFIX ) o.TITLE = o.HOST
def test_notify_metadata_filter(self): bugsnag.configure(params_filters=['apple', 'grape']) bugsnag.notify(ScaryException('unexpected failover'), apple='four', cantaloupe='green') json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertEqual('[FILTERED]', event['metaData']['custom']['apple']) self.assertEqual('green', event['metaData']['custom']['cantaloupe'])
def setUp(self): super(TestWSGI, self).setUp() bugsnag.configure(use_ssl=False, endpoint=self.server.address, api_key='3874876376238728937', notify_release_stages=['dev'], release_stage='dev', asynchronous=False)
def test_auto_notify_ignored_exc_info(self): bugsnag.configure(auto_notify=False) try: raise ScaryException('Testing Notify EXC Info') except Exception: bugsnag.auto_notify_exc_info() self.assertEqual(len(self.server.received), 0)
def configure_bugsnag_error_monitoring(bugsnag_key, bugsnag_release_stage): bugsnag.configure(api_key=bugsnag_key, project_root="./", notify_release_stages=["production", "staging"], release_stage=bugsnag_release_stage) bugsnag_handler = BugsnagHandler() bugsnag_handler.setLevel(logging.ERROR) add_handler_to_logger(bugsnag_handler)
def test_disable_environment(self): bugsnag.configure(send_environment=False) response = self.fetch('/notify', method="POST", body="test=post") self.assertEqual(response.code, 200) self.assertEqual(len(self.server.received), 1) payload = self.server.received[0]['json_body'] assert 'environment' not in payload['events'][0]['metaData']
def setUp(self): super(TornadoTests, self).setUp() bugsnag.configure(use_ssl=False, endpoint=self.server.address, api_key='3874876376238728937', notify_release_stages=['dev'], release_stage='dev', asynchronous=False)
def test_disable_environment(bugsnag_server, django_client): bugsnag.configure(send_environment=False) response = django_client.get('/notes/handled-exception/?foo=strawberry') assert response.status_code == 200 bugsnag_server.wait_for_request() payload = bugsnag_server.received[0]['json_body'] assert 'environment' not in payload['events'][0]['metaData']
def setUp(self): super(TestBugsnag, self).setUp() bugsnag.configure(use_ssl=False, endpoint=self.server.address, api_key='tomatoes', notify_release_stages=['dev'], release_stage='dev', asynchronous=False)
def setUp(self): super(HandlersTest, self).setUp() bugsnag.configure(use_ssl=False, endpoint=self.server.address, api_key='tomatoes', notify_release_stages=['dev'], release_stage='dev', asynchronous=False) bugsnag.logger.setLevel(logging.INFO)
def init_bugsnag(app_version: str): bugsnag.configure(api_key=BUGSNAG_ID, project_root=Path(__file__).parent / 'pingou', notify_release_stages=['production'], release_stage=RELEASE_STAGE, app_version=app_version) bugsnag_handler = BugsnagHandler() bugsnag_handler.setLevel(logging.ERROR) return bugsnag_handler
def test_enable_environment(bugsnag_server, django_client): bugsnag.configure(send_environment=True) response = django_client.get('/notes/handled-exception/?foo=strawberry') assert response.status_code == 200 bugsnag_server.wait_for_request() payload = bugsnag_server.received[0]['json_body'] event = payload['events'][0] assert event['metaData']['environment']['REQUEST_METHOD'] == 'GET'
def test_notify_payload_matching_filter(self): bugsnag.configure(params_filters=['number']) bugsnag.notify(ScaryException('unexpected failover'), apple='four', number=76) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] exception = event['exceptions'][0] self.assertEqual('four', event['metaData']['custom']['apple']) self.assertEqual('[FILTERED]', event['metaData']['custom']['number']) self.assertEqual(173, exception['stacktrace'][0]['lineNumber'])
def test_notify_callback_app_type(self): def callback(report): report.app_type = 'whopper' bugsnag.configure(app_type='rq') bugsnag.before_notify(callback) bugsnag.notify(ScaryException('unexpected failover')) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertEqual('whopper', event['app']['type'])
def configure_bugsnag(): if is_debug_mode(): return # 增加bugsnag上报一些不在预期内的错误 bugsnag.configure( api_key="723026d09a7442c9e02ebc5d4a08e8d0", app_version=now_version, auto_capture_sessions=True, )
def configure(): # default to development if in DEBUG mode if getattr(settings, 'DEBUG'): bugsnag.configure(release_stage='development') # Import Bugsnag settings from settings.py django_bugsnag_settings = getattr(settings, 'BUGSNAG', {}) bugsnag.configure(**django_bugsnag_settings) bugsnag.before_notify(add_django_request_to_notification)
def bugsnag_filter(global_conf, **local_conf): if 'notify_release_stages' in local_conf: local_conf['notify_release_stages'] = local_conf[ 'notify_release_stages'].split(',') bugsnag.configure(**local_conf) def bugsnag_filter(app): return BugsnagMiddleware(app) return bugsnag_filter
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if not self._debug: bugsnag.configure( ).runtime_versions['Starlette'] = starlette.__version__ middleware = bugsnag.configure().internal_middleware middleware.before_notify(self.additional_info) await self.bugsnag_app(scope, receive, send) return await self.app(scope, receive, send)
def test_notify_payload_matching_filter(self): bugsnag.configure(params_filters=['number']) bugsnag.notify(ScaryException('unexpected failover'), apple='four', number=76) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] exception = event['exceptions'][0] self.assertEqual('four', event['metaData']['custom']['apple']) self.assertEqual('[FILTERED]', event['metaData']['custom']['number']) self.assertEqual(148, exception['stacktrace'][0]['lineNumber'])
def bugsnag_server(): server = FakeBugsnagServer() bugsnag.configure(endpoint=server.url, api_key='3874876376238728937') yield server # Reset shared client config global_setup.configuration = bugsnag.Configuration() global_setup.default_client.configuration = global_setup.configuration server.shutdown()
def test_asynchronous_notify(self): bugsnag.configure(asynchronous=True) self.server.paused = True bugsnag.notify(ScaryException('unexpected failover')) self.server.paused = False start = time.time() while len(self.server.received) == 0: if time.time() > (start + 0.5): raise Exception( 'Timed out while waiting for asynchronous request.') time.sleep(0.001) self.assertEqual(len(self.server.received), 1)
def test_notify_user_filter(self): bugsnag.configure(params_filters=['address', 'phonenumber']) bugsnag.notify(ScaryException('unexpected failover'), user={ "id": "test-man", "address": "123 street\n cooltown\n ABC 123", "phonenumber": "12345678900", "firstname": "Test", "lastname": "Man" }) json_body = self.server.received[0]['json_body'] event = json_body['events'][0] self.assertEqual('[FILTERED]', event['user']['address']) self.assertEqual('[FILTERED]', event['user']['phonenumber']) self.assertEqual('test-man', event['user']['id']) self.assertEqual('Test', event['user']['firstname']) self.assertEqual('Man', event['user']['lastname'])
def boot(application, api_key, flavor, version): # Configure bugsnag if api_key: try: import bugsnag import bugsnag.flask root_path = os.path.abspath( os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) bugsnag.configure(api_key=api_key, project_root=root_path, release_stage=flavor, notify_release_stages=[flavor], app_version=version ) bugsnag.flask.handle_exceptions(application) except Exception as e: raise Exception('Failed to init bugsnag agent %s' % e)
def setup_app(app): #setup syslog app.logger.addFilter(app_logging.ContextFilter()) syslog_handler = app_logging.get_syslog_handler() if syslog_handler: app.logger.addHandler(syslog_handler) #send application error exception message to log def log_exception(sender, exception, **extra): sender.logger.error(exception) got_request_exception.connect(log_exception, app) #setup bugsnag bugsnag_api_key = os.getenv('SS_SORAYA_RECO_BUGSNAG_API_KEY') if bugsnag_api_key: bugsnag.configure( api_key= bugsnag_api_key, project_root=os.path.abspath(os.path.dirname(__file__)), release_stage=os.getenv('SS_SORAYA_RECO_RELEASE_STAGE')) handle_exceptions(app)
def init_before_first_request(): import datetime init_tag = "[Initiation of Service Process]\n" # Configure Bugsnag bugsnag.configure( api_key=BUGSNAG_TOKEN, project_root=os.path.dirname(os.path.realpath(__file__)), ) # Attach Bugsnag to Flask's exception handler handle_exceptions(app) log_init_time = "Initiation START at: \t%s\n" % datetime.datetime.now() log_app_env = "Environment Variable: \t%s\n" % APP_ENV log_bugsnag_token = "Bugsnag Service TOKEN: \t%s\n" % BUGSNAG_TOKEN log_logentries_token = "Logentries Service TOKEN: \t%s\n" % LOGENTRIES_TOKEN logger.info(init_tag + log_init_time) logger.info(init_tag + log_app_env) logger.info(init_tag + log_bugsnag_token) logger.info(init_tag + log_logentries_token)
def process_request(self, request): if is_development_server(request): bugsnag.configure(release_stage="development") try: bugsnag.configure_request( context=request.path, user_id=get_user_id(request), session_data=dict(request.session), request_data={ 'path': request.path, 'encoding': request.encoding, 'params': dict(request.REQUEST), 'url': request.build_absolute_uri(), }, environment_data=dict(request.META), ) except Exception as exc: bugsnag.log("Error in request middleware: %s" % exc) return None
def init(): # Configure the secret key if not cfg.secret_key: raise RuntimeError("Config error: `secret_key' is not set") flask.Flask.secret_key = cfg.secret_key # Configure the email exceptions info = cfg.email_exceptions if info: mailhost = info["smtp_host"] mailport = info.get("smtp_port") if mailport: mailhost = (mailhost, mailport) smtp_secure = info.get("smtp_secure", None) secure_args = _adapt_smtp_secure(smtp_secure) mail_handler = logging.handlers.SMTPHandler( mailhost=mailhost, fromaddr=info["from_addr"], toaddrs=[info["to_addr"]], subject="Docker registry exception", credentials=(info["smtp_login"], info["smtp_password"]), secure=secure_args, ) mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler) # Configure bugsnag info = cfg.bugsnag if info: if not bugsnag: raise _bugsnag_import_error root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) bugsnag.configure( api_key=info, project_root=root_path, release_stage=cfg.flavor, notify_release_stages=[cfg.flavor], app_version=VERSION, ) bugsnag.flask.handle_exceptions(app)
def init(): # Configure the email exceptions info = cfg.email_exceptions if info and info.smtp_host: mailhost = info.smtp_host mailport = info.smtp_port if mailport: mailhost = (mailhost, mailport) smtp_secure = info.smtp_secure secure_args = _adapt_smtp_secure(smtp_secure) mail_handler = logging.handlers.SMTPHandler( mailhost=mailhost, fromaddr=info.from_addr, toaddrs=[info.to_addr], subject='Docker registry exception', credentials=(info.smtp_login, info.smtp_password), secure=secure_args) mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler) # Configure bugsnag info = cfg.bugsnag if info: if not bugsnag: raise _bugsnag_import_error root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) bugsnag.configure(api_key=info, project_root=root_path, release_stage=cfg.flavor, notify_release_stages=[cfg.flavor], app_version=__version__ ) bugsnag.flask.handle_exceptions(app) # Configure flask_cors for i in cfg.cors.keys(): app.config['CORS_%s' % i.upper()] = cfg.cors[i] CORS(app)
def make_application(): from flask import Flask from cronq.config import Config import cronq.web import os flask_app = Flask(__name__, static_url_path='/static') if Config.BUGSNAG_API_KEY: import bugsnag from bugsnag.flask import handle_exceptions bugsnag.configure(api_key=Config.BUGSNAG_API_KEY) handle_exceptions(flask_app) elif Config.SENTRY_DSN: from raven.contrib.flask import Sentry sentry = Sentry() sentry.init_app(flask_app, dsn=Config.SENTRY_DSN) flask_app.config.from_object('cronq.config.Config') flask_app.register_blueprint(cronq.web.blueprint_http) cronq.web.blueprint_http.config = flask_app.config return flask_app
# Find the correct template folder when running from a different location tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') app = Flask(__name__, template_folder=tmpl_dir) # Set up Bugsnag exception tracking, if desired. To use Bugsnag, install the # Bugsnag Python client with the command "pip install bugsnag", and set the # environment variable BUGSNAG_API_KEY. You can also optionally set # BUGSNAG_RELEASE_STAGE. if os.environ.get("BUGSNAG_API_KEY") is not None: try: import bugsnag import bugsnag.flask release_stage = os.environ.get("BUGSNAG_RELEASE_STAGE") or "production" bugsnag.configure(api_key=os.environ.get("BUGSNAG_API_KEY"), project_root=os.path.dirname(os.path.abspath(__file__)), use_ssl=True, release_stage=release_stage, ignore_classes=['werkzeug.exceptions.NotFound']) bugsnag.flask.handle_exceptions(app) except: app.logger.warning("Unable to initialize Bugsnag exception handling.") # ----------- # Middlewares # ----------- @app.after_request def set_cors_headers(response): response.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*') response.headers['Access-Control-Allow-Credentials'] = 'true' if request.method == 'OPTIONS': # Both of these headers are only used for the "preflight request"
# Configure logging logging.config.dictConfig(app.config['LOGGING']) log = logging.getLogger(__name__) if app.config['DEBUG']: log.info('Pillar starting, debug=%s', app.config['DEBUG']) # Configure Bugsnag if not app.config.get('TESTING') and app.config.get('BUGSNAG_API_KEY'): import bugsnag import bugsnag.flask import bugsnag.handlers bugsnag.configure( api_key=app.config['BUGSNAG_API_KEY'], project_root="/data/git/pillar/pillar", ) bugsnag.flask.handle_exceptions(app) bs_handler = bugsnag.handlers.BugsnagHandler() bs_handler.setLevel(logging.ERROR) log.addHandler(bs_handler) else: log.info('Bugsnag NOT configured.') # Google Cloud project try: os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = \ app.config['GCLOUD_APP_CREDENTIALS'] except KeyError: raise SystemExit('GCLOUD_APP_CREDENTIALS configuration is missing')
def setup_logging(): """ Configure our logger """ logger = logging.getLogger() logger.handlers = [] if cmd_line.debug: log_level = logging.DEBUG else: log_level = logging.INFO logger.setLevel(log_level) context_filter = ContextFilter() logger.addFilter(context_filter.filter) # Log format formatter = logging.Formatter( fmt="%(asctime)s [%(levelname)s] %(filename)s(%(lineno)d) %(funcName)s(): %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) # File logger try: file_handler = logging.FileHandler("/tmp/DSGos_Installer.log", mode="w") file_handler.setLevel(log_level) file_handler.setFormatter(formatter) logger.addHandler(file_handler) except PermissionError as permission_error: print("Can't open /tmp/DSGos_Installer.log : ", permission_error) # Stdout logger if cmd_line.verbose: # Show log messages to stdout stream_handler = logging.StreamHandler() stream_handler.setLevel(log_level) stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) if cmd_line.log_server: log_server = cmd_line.log_server if log_server == "bugsnag": if not BUGSNAG_ERROR: # Bugsnag logger bugsnag_api = context_filter.api_key if bugsnag_api is not None: bugsnag.configure( api_key=bugsnag_api, app_version=info.DSGos_Installer_VERSION, project_root="/usr/share/DSGos-Installer", release_stage=info.DSGos_Installer_RELEASE_STAGE, ) bugsnag_handler = BugsnagHandler(api_key=bugsnag_api) bugsnag_handler.setLevel(logging.WARNING) bugsnag_handler.setFormatter(formatter) bugsnag_handler.addFilter(context_filter.filter) bugsnag.before_notify(context_filter.bugsnag_before_notify_callback) logger.addHandler(bugsnag_handler) logging.info("Sending DSGos_Installer log messages to bugsnag server (using python-bugsnag).") else: logging.warning("Cannot read the bugsnag api key, logging to bugsnag is not possible.") else: logging.warning(BUGSNAG_ERROR) else: # Socket logger socket_handler = logging.handlers.SocketHandler(log_server, logging.handlers.DEFAULT_TCP_LOGGING_PORT) socket_formatter = logging.Formatter(formatter) socket_handler.setFormatter(socket_formatter) logger.addHandler(socket_handler) # Also add uuid filter to requests logs logger_requests = logging.getLogger("requests.packages.urllib3.connectionpool") logger_requests.addFilter(context_filter.filter) uid = str(uuid.uuid1()).split("-") myuid = uid[3] + "-" + uid[1] + "-" + uid[2] + "-" + uid[4] logging.info("Sending DSGos_Installer logs to {0} with id '{1}'".format(log_server, myuid))
app = Flask(__name__, template_folder=config.FLASK_TEMPLATE_FOLDER, static_folder=config.FLASK_STATIC_FOLDER) app.config.from_object(config) Compress(app) CsrfProtect(app) app_db = SQLAlchemy(app) login_manager = LoginManager() login_manager.login_view = "login" login_manager.login_message = None login_manager.init_app(app) if config.ENVIRONMENT == 'production': # Configure Bugsnag bugsnag.configure( api_key=config.BUGSNAG_KEY, project_root=config.FLASK_PROJECT_PATH, ) handle_exceptions(app) # Configure Logging stream_handler = logging.StreamHandler() app.logger.addHandler(stream_handler) app.logger.setLevel(logging.INFO) else: # Configure local logging from logging.handlers import RotatingFileHandler file_handler = RotatingFileHandler('logs/kateheddleston.log', 'a', 1 * 1024 * 1024, 10) file_handler.setLevel(logging.INFO) file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')) app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO)
from rq import Worker, Queue, Connection import urlparse import getopt import sys from multiprocessing import Process import bugsnag # Preload heavy libraries from bs4 import BeautifulSoup import pandas as pd import requests import bugsnag from bugsnag.flask import handle_exceptions bugsnag.configure( api_key = "2556d33391f9accc8ea79325cd78ab62", ) concurrency = 1 try: opts, args = getopt.getopt(sys.argv[1:],"c:",[]) except getopt.GetoptError: print 'worker.py -c <concurrency>' sys.exit(2) for opt, arg in opts: if opt == '-c': concurrency = int(arg) listen = ['high', 'default', 'low']
import bugsnag from bugsnag.wsgi.middleware import BugsnagMiddleware bugsnag.configure( api_key="some-api-key" ) def application(env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) raise Exception('Ack') return [b"Hello World"] application = BugsnagMiddleware(application)
# Configure Logentries logger = logging.getLogger('logentries') if APP_ENV == 'prod': logger.setLevel(logging.INFO) else: logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s : %(levelname)s, %(message)s')) logger.addHandler(ch) logentries_handler = LogentriesHandler(LOGENTRIES_TOKEN) logger.addHandler(logentries_handler) # Configure Bugsnag bugsnag.configure( api_key=BUGSNAG_TOKEN, project_root=os.path.dirname(os.path.realpath(__file__)) ) app = Flask(__name__) # Attach Bugsnag to Flask's exception handler handle_exceptions(app) @app.before_first_request def init_before_first_request(): init_tag = "[Initiation of Service Process]\n" logger.info('[init] enter init_before_first_request') log_init_time = "Initiation START at: \t%s\n" % (datetime.datetime.now()) log_app_env = "Environment Variable: \t%s\n" % (APP_ENV)
consumer_key=conf('MEETUP_OAUTH_CONSUMER_KEY'), consumer_secret=conf('MEETUP_OAUTH_CONSUMER_SECRET'), ) meetup = Meetup(meetup_oauth) app.config['MONGO_URI'] = conf('MONGOHQ_URL', 'mongodb://localhost/meetups') mongo = PyMongo(app) sendgrid_api = sendgrid.Sendgrid( username=conf('SENDGRID_USERNAME'), password=conf('SENDGRID_PASSWORD'), secure=True, ) if conf('BUGSNAG_API_KEY'): bugsnag.configure( api_key=conf('BUGSNAG_API_KEY'), release_stage=conf('BUGSNAG_RELEASE_STAGE', 'development'), notify_release_stages=['production'], auto_notify=False, use_ssl=True, project_root=os.path.dirname(os.path.dirname(__file__)), # project_version= ) from .models import login_manager login_manager.setup_app(app) from . import views from . import filters
#!/usr/bin/env python import os import bugsnag from bugsnag.handlers import BugsnagHandler from bugsnag.flask import handle_exceptions from flask import Flask # Configure bugsnag bugsnag.configure( api_key="066f5ad3590596f9aa8d601ea89af845", project_root=os.path.dirname(os.path.realpath(__file__)), ) # Create flask app app = Flask(__name__) handle_exceptions(app) # app.logger.addHandler(BugsnagHandler()); # raise Exception("Oh dear fatal exception") @app.route("/") def hello(): raise Exception("Oh dear fatal exception") return "Hello World!" if __name__ == "__main__": app.run()