Exemple #1
0
def transform_and_roll_out():
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    sys.excepthook = uncaught_ex_handler

    parser = create_argparser()
    args = parser.parse_args()

    configuration = get_bot_defaults()

    if args.conf:
        with open(args.conf) as cfile:
            configuration.update(parse_config(cfile))

    # Environment variables override configuration file settings
    env_config = get_environment_configuration()
    configuration.update(env_config)

    if ROLLBAR_ACCESS_TOKEN in configuration:
        rollbar.init(configuration[ROLLBAR_ACCESS_TOKEN],
                     configuration[ROLLBAR_ENVIRONMENT])
        rollbar_handler = RollbarHandler()
        rollbar_handler.setLevel(logging.ERROR)
        logging.getLogger('').addHandler(rollbar_handler)

    # This is hack-city, but since we're constructing the redis data
    # after the fact, we'll now bolt the database back into the baseclass
    walrus = Walrus(host=configuration[REDIS_URL],
                    port=configuration[REDIS_PORT],
                    password=configuration[REDIS_PASSWORD])
    AutoBotBaseModel.set_database(walrus)

    bot = AutoBot(configuration)
    bot.run(args.forever, args.interval)
    def _create_logger(self):
        logger = logging.getLogger(__name__)
        logger.setLevel(logging.DEBUG)

        rollbar_handler = RollbarHandler(_test_access_token, _test_environment)
        rollbar_handler.setLevel(logging.WARNING)

        logger.addHandler(rollbar_handler)

        return logger
Exemple #3
0
    def _create_logger(self):
        logger = logging.getLogger(__name__)
        logger.setLevel(logging.DEBUG)

        rollbar_handler = RollbarHandler(_test_access_token, _test_environment)
        rollbar_handler.setLevel(logging.WARNING)

        logger.addHandler(rollbar_handler)

        return logger
Exemple #4
0
def setup_rollbar_log_handler(rollbar_settings: Dict[str, str]) -> None:
    """
    All log messages with level ERROR or higher will be sent to Rollbar
    """
    rollbar.init(**rollbar_settings)

    rollbar_handler = RollbarHandler()
    rollbar_handler.setLevel(logging.ERROR)

    root_logger = logging.getLogger()
    root_logger.addHandler(rollbar_handler)
    def get_logger(self, name):
        logger = logging.getLogger(name)
        logger.setLevel(self.log_level)

        has_stream_handler = any(
            isinstance(handler, logging.StreamHandler)
            for handler in logger.handlers
        )
        if not has_stream_handler:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(self.log_level)
            stream_handler.setFormatter(logging.Formatter(
                name + ': %(levelname)s: %(asctime)s %(message)s'
            ))
            logger.addHandler(stream_handler)

        has_file_handler = any(
            isinstance(handler, handlers.RotatingFileHandler)
            for handler in logger.handlers
        )
        if self.logfile_root is not None and not has_file_handler:
            ensure_path_exists(self.logfile_root)
            logfile_path = os.path.join(self.logfile_root, "{0}.log".format(name))
            file_handler = handlers.RotatingFileHandler(logfile_path,
                                                        backupCount=20,
                                                        maxBytes=10000000)
            file_handler.setLevel(logging.DEBUG)
            file_handler.setFormatter(logging.Formatter(
                '%(levelname)s: %(asctime)s %(message)s'
            ))
            logger.addHandler(file_handler)

        if is_rollbar_available():
            import rollbar
            from rollbar.logger import RollbarHandler
            has_rollbar_handler = any(
                isinstance(handler, RollbarHandler)
                for handler in logger.handlers
            )
            if not has_rollbar_handler:
                rb_secret = os.environ['ROLLBAR_SECRET']
                rb_environment = os.environ['ROLLBAR_ENVIRONMENT']
                if not rollbar._initialized:
                    rollbar.init(rb_secret, rb_environment)

                rb_handler = RollbarHandler()
                rb_handler.setLevel(logging.WARNING)
                logger.addHandler(rb_handler)
        return logger
Exemple #6
0
    def init_app(cls, app):
        Config.init_app(app)
        assert os.environ.get('SECRET_KEY'), 'SECRET_KEY IS NOT SET!'

        rollbar.init('ROLLBAR_ACCESS_TOKEN')

        logger = logging.getLogger(__name__)
        logger.setLevel(logging.DEBUG)

        # report WARNING and above to Rollbar
        rollbar_handler = RollbarHandler(history_size=3)
        rollbar_handler.setLevel(logging.WARNING)

        # gather history for DEBUG+ log messages
        rollbar_handler.setHistoryLevel(logging.DEBUG)

        # attach the history handler to the root logger
        logger.addHandler(rollbar_handler)
Exemple #7
0
class LogHandlerTest(BaseTest):
    def setUp(self):
        rollbar._initialized = False
        rollbar.SETTINGS = copy.deepcopy(_default_settings)

    def _create_logger(self):
        logger = logging.getLogger(__name__)
        logger.setLevel(logging.DEBUG)

        self.rollbar_handler = RollbarHandler(_test_access_token, _test_environment)
        self.rollbar_handler.setLevel(logging.WARNING)

        logger.addHandler(self.rollbar_handler)

        return logger

    @mock.patch('rollbar.send_payload')
    def test_message_stays_unformatted(self, send_payload):
        logger = self._create_logger()
        logger.warning("Hello %d %s", 1, 'world')

        payload = send_payload.call_args[0][0]

        self.assertEqual(payload['data']['body']['message']['body'], "Hello %d %s")
        self.assertEqual(payload['data']['body']['message']['args'], (1, 'world'))
        self.assertEqual(payload['data']['body']['message']['record']['name'], __name__)

    @mock.patch('rollbar.send_payload')
    def test_string_or_int_level(self, send_payload):
        logger = self._create_logger()
        logger.setLevel(logging.ERROR)
        self.rollbar_handler.setLevel('WARNING')
        logger.error("I am an error")

        payload = send_payload.call_args[0][0]

        self.assertEqual(payload['data']['level'], 'error')

        self.rollbar_handler.setLevel(logging.WARNING)
        logger.error("I am an error")

        self.assertEqual(payload['data']['level'], 'error')

    def test_request_is_get_from_log_record_if_present(self):
        logger = self._create_logger()
        # Request objects vary depending on python frameworks or packages.
        # Using a dictionary for this test is enough.
        request = {"fake": "request", "for":  "testing purporse"}

        # No need to test request parsing and payload sent,
        # just need to be sure that proper rollbar function is called
        # with passed request as argument.
        with mock.patch("rollbar.report_message") as report_message_mock:
            logger.warning("Warning message", extra={"request": request})
            self.assertEqual(report_message_mock.call_args[1]["request"], request)

        # Python 2.6 doesnt support extra param in logger.exception.
        if not sys.version_info[:2] == (2, 6):
            # if you call logger.exception outside of an exception
            # handler, it shouldn't try to report exc_info, since it
            # won't have any
            with mock.patch("rollbar.report_exc_info") as report_exc_info:
                with mock.patch("rollbar.report_message") as report_message_mock:
                    logger.exception("Exception message", extra={"request": request})
                    report_exc_info.assert_not_called()
                    self.assertEqual(report_message_mock.call_args[1]["request"], request)

            with mock.patch("rollbar.report_exc_info") as report_exc_info:
                with mock.patch("rollbar.report_message") as report_message_mock:
                    try:
                        raise Exception()
                    except:
                        logger.exception("Exception message", extra={"request": request})
                        self.assertEqual(report_exc_info.call_args[1]["request"], request)
                        report_message_mock.assert_not_called()

    @mock.patch('rollbar.send_payload')
    def test_nested_exception_trace_chain(self, send_payload):
        logger = self._create_logger()

        def _raise_context():
            bar_local = 'bar'
            raise CauseException('bar')

        def _raise_ex():
            try:
                _raise_context()
            except CauseException as context:
                # python2 won't automatically assign this traceback...
                exc_info = sys.exc_info()
                setattr(context, '__traceback__', exc_info[2])
                try:
                    foo_local = 'foo'
                    # in python3 __context__ is automatically set when an exception is raised in an except block
                    e = Exception('foo')
                    setattr(e, '__context__', context)  # PEP-3134
                    raise e
                except:
                    logger.exception("Bad time")

        _raise_ex()

        self.assertEqual(send_payload.called, True)
        payload = send_payload.call_args[0][0]
        body = payload['data']['body']
        trace = body['trace'] if 'trace' in body else None
        trace_chain = body['trace_chain'] if 'trace_chain' in body else None
        has_only_trace_chain = trace is None and trace_chain is not None
        has_only_trace = trace is not None and trace_chain is None
        self.assertTrue(has_only_trace or has_only_trace_chain)
        if trace_chain is not None:
            self.assertEqual('Bad time', payload['data']['custom']['exception']['description'])
        if trace is not None:
            self.assertEqual('Bad time', trace['exception']['description'])

    @mock.patch('rollbar.send_payload')
    def test_not_nested_exception_trace_chain(self, send_payload):
        logger = self._create_logger()

        def _raise_context():
            bar_local = 'bar'
            raise CauseException('bar')

        def _raise_ex():
            try:
                _raise_context()
            except:
                logger.exception("Bad time")

        _raise_ex()

        self.assertEqual(send_payload.called, True)
        payload = send_payload.call_args[0][0]
        body = payload['data']['body']
        trace = body['trace'] if 'trace' in body else None
        trace_chain = body['trace_chain'] if 'trace_chain' in body else None
        has_only_trace_chain = trace is None and trace_chain is not None
        has_only_trace = trace is not None and trace_chain is None
        self.assertTrue(has_only_trace or has_only_trace_chain)
        if trace_chain is not None:
            self.assertEqual('Bad time', payload['data']['custom']['exception']['description'])
        if trace is not None:
            self.assertEqual('Bad time', trace['exception']['description'])
Exemple #8
0
def add_rollbar_handler(logger, level=logging.WARNING):
    if settings.ROLLBAR_ACCESS_TOKEN is not None:
        rollbar_handler = RollbarHandler()
        rollbar_handler.setLevel(level)
        logger.addHandler(rollbar_handler)
Exemple #9
0
from flask import Flask

import rollbar
from rollbar.logger import RollbarHandler

ACCESS_TOKEN = '...'
ENVIRONMENT = 'development'

rollbar.init(ACCESS_TOKEN, ENVIRONMENT)

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# report WARNING and above to Rollbar
rollbar_handler = RollbarHandler(history_size=3)
rollbar_handler.setLevel(logging.WARNING)

# gather history for DEBUG+ log messages
rollbar_handler.setHistoryLevel(logging.DEBUG)

# attach the history handler to the root logger
logger.addHandler(rollbar_handler)

app = Flask(__name__)


@app.route('/')
def root():
    logger.info('about to call foo()')
    try:
Exemple #10
0
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse

# Initialize Rollbar SDK with your server-side ACCESS_TOKEN
rollbar.init(
    'ACCESS_TOKEN',
    environment='staging',
    handler='async',  # For asynchronous reporting use: default, async or httpx
)

# Set root logger to log DEBUG and above
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Report ERROR and above to Rollbar
rollbar_handler = RollbarHandler()
rollbar_handler.setLevel(logging.ERROR)

# Attach Rollbar handler to the root logger
logger.addHandler(rollbar_handler)

# Integrate Rollbar with Starlette application
app = Starlette()
app.add_middleware(LoggerMiddleware)  # should be added as the last middleware


# GET query params will be sent to Rollbar and available in the UI
# $ curl http://localhost:8888?param1=hello&param2=world
@app.route('/')
async def root(request):
    # Report log entries
class LogHandlerTest(BaseTest):
    def setUp(self):
        rollbar._initialized = False
        rollbar.SETTINGS = copy.deepcopy(_default_settings)

    def _create_logger(self):
        logger = logging.getLogger(__name__)
        logger.setLevel(logging.DEBUG)

        self.rollbar_handler = RollbarHandler(_test_access_token,
                                              _test_environment)
        self.rollbar_handler.setLevel(logging.WARNING)

        logger.addHandler(self.rollbar_handler)

        return logger

    @mock.patch('rollbar.send_payload')
    def test_message_stays_unformatted(self, send_payload):
        logger = self._create_logger()
        logger.warning("Hello %d %s", 1, 'world')

        payload = send_payload.call_args[0][0]

        self.assertEqual(payload['data']['body']['message']['body'],
                         "Hello %d %s")
        self.assertEqual(payload['data']['body']['message']['args'],
                         (1, 'world'))
        self.assertEqual(payload['data']['body']['message']['record']['name'],
                         __name__)

    @mock.patch('rollbar.send_payload')
    def test_string_or_int_level(self, send_payload):
        logger = self._create_logger()
        logger.setLevel(logging.ERROR)
        self.rollbar_handler.setLevel('WARNING')
        logger.error("I am an error")

        payload = send_payload.call_args[0][0]

        self.assertEqual(payload['data']['level'], 'error')

        self.rollbar_handler.setLevel(logging.WARNING)
        logger.error("I am an error")

        self.assertEqual(payload['data']['level'], 'error')

    def test_request_is_get_from_log_record_if_present(self):
        logger = self._create_logger()
        # Request objects vary depending on python frameworks or packages.
        # Using a dictionary for this test is enough.
        request = {"fake": "request", "for": "testing purporse"}

        # No need to test request parsing and payload sent,
        # just need to be sure that proper rollbar function is called
        # with passed request as argument.
        with mock.patch("rollbar.report_message") as report_message_mock:
            logger.warning("Warning message", extra={"request": request})
            self.assertEqual(report_message_mock.call_args[1]["request"],
                             request)

        # Python 2.6 doesnt support extra param in logger.exception.
        if not sys.version_info[:2] == (2, 6):
            # if you call logger.exception outside of an exception
            # handler, it shouldn't try to report exc_info, since it
            # won't have any
            with mock.patch("rollbar.report_exc_info") as report_exc_info:
                with mock.patch(
                        "rollbar.report_message") as report_message_mock:
                    logger.exception("Exception message",
                                     extra={"request": request})
                    report_exc_info.assert_not_called()
                    self.assertEqual(
                        report_message_mock.call_args[1]["request"], request)

            with mock.patch("rollbar.report_exc_info") as report_exc_info:
                with mock.patch(
                        "rollbar.report_message") as report_message_mock:
                    try:
                        raise Exception()
                    except:
                        logger.exception("Exception message",
                                         extra={"request": request})
                        self.assertEqual(
                            report_exc_info.call_args[1]["request"], request)
                        report_message_mock.assert_not_called()

    @mock.patch('rollbar.send_payload')
    def test_nested_exception_trace_chain(self, send_payload):
        logger = self._create_logger()

        def _raise_context():
            bar_local = 'bar'
            raise CauseException('bar')

        def _raise_ex():
            try:
                _raise_context()
            except CauseException as context:
                # python2 won't automatically assign this traceback...
                exc_info = sys.exc_info()
                setattr(context, '__traceback__', exc_info[2])
                try:
                    foo_local = 'foo'
                    # in python3 __context__ is automatically set when an exception is raised in an except block
                    e = Exception('foo')
                    setattr(e, '__context__', context)  # PEP-3134
                    raise e
                except:
                    logger.exception("Bad time")

        _raise_ex()

        self.assertEqual(send_payload.called, True)
        payload = send_payload.call_args[0][0]
        body = payload['data']['body']
        trace = body['trace'] if 'trace' in body else None
        trace_chain = body['trace_chain'] if 'trace_chain' in body else None
        has_only_trace_chain = trace is None and trace_chain is not None
        has_only_trace = trace is not None and trace_chain is None
        self.assertTrue(has_only_trace or has_only_trace_chain)
        if trace_chain is not None:
            self.assertEqual(
                'Bad time',
                payload['data']['custom']['exception']['description'])
        if trace is not None:
            self.assertEqual('Bad time', trace['exception']['description'])

    @mock.patch('rollbar.send_payload')
    def test_not_nested_exception_trace_chain(self, send_payload):
        logger = self._create_logger()

        def _raise_context():
            bar_local = 'bar'
            raise CauseException('bar')

        def _raise_ex():
            try:
                _raise_context()
            except:
                logger.exception("Bad time")

        _raise_ex()

        self.assertEqual(send_payload.called, True)
        payload = send_payload.call_args[0][0]
        body = payload['data']['body']
        trace = body['trace'] if 'trace' in body else None
        trace_chain = body['trace_chain'] if 'trace_chain' in body else None
        has_only_trace_chain = trace is None and trace_chain is not None
        has_only_trace = trace is not None and trace_chain is None
        self.assertTrue(has_only_trace or has_only_trace_chain)
        if trace_chain is not None:
            self.assertEqual(
                'Bad time',
                payload['data']['custom']['exception']['description'])
        if trace is not None:
            self.assertEqual('Bad time', trace['exception']['description'])
Exemple #12
0
    else:
		print "Unknown command"
		sys.exit(2)



def assure_path_exists(path="~/dredd_logs"):
    path = os.path.expanduser(path)
    if not os.path.exists(path):
        os.makedirs(path)
    return path

if __name__ == "__main__":
    # Log to rotating file
    logger = logging.getLogger('dredd')
    logger.setLevel(logging.INFO)
    log_file = assure_path_exists("~/dredd_logs") + "/current.log"
    file_handeler = TimedRotatingFileHandler(log_file, when="midnight", utc=True)
    log_format = logging.Formatter('%(levelname)s %(asctime)s: %(message)s')
    file_handeler.setFormatter(log_format)
    logger.addHandler(file_handeler)

    # Log only critical errors to Rollbar
    configuration = Configuration()
    rollbar.init(configuration.rollbar_api_token, 'production', locals=configuration.rollbar_sizes_settings())
    rollbar_handler = RollbarHandler()
    rollbar_handler.setLevel(logging.ERROR)
    logger.addHandler(rollbar_handler)

    main(logger)
Exemple #13
0
def add_rollbar_handler(logger, level=logging.WARNING):
    if settings.ROLLBAR_ACCESS_TOKEN is not None:
        rollbar_handler = RollbarHandler()
        rollbar_handler.setLevel(level)
        logger.addHandler(rollbar_handler)
Exemple #14
0
from flask import Flask

import rollbar
from rollbar.logger import RollbarHandler

ACCESS_TOKEN = '...'
ENVIRONMENT = 'development'

rollbar.init(ACCESS_TOKEN, ENVIRONMENT)

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# report WARNING and above to Rollbar
rollbar_handler = RollbarHandler(history_size=3)
rollbar_handler.setLevel(logging.WARNING)

# gather history for DEBUG+ log messages
rollbar_handler.setHistoryLevel(logging.DEBUG)

# attach the history handler to the root logger
logger.addHandler(rollbar_handler)


app = Flask(__name__)

@app.route('/')
def root():
    logger.info('about to call foo()')
    try:
Exemple #15
0
    (r"/api/" + version +
     "/support/(?P<run_numero_url>\d+)/recovery/(?P<method>\d+)$",
     support.SupportHandler),
    (r"/api/" + version + "/support/(?P<run_numero_url>\d+)/email$",
     support.SupportHandler),
    (r"/api/" + version + "/support/agents/users$",
     user_agent.SupportAgentsHandler),
    (r"/api/" + version + "/support/agents/users/(?P<run_numero_url>\d+)$",
     user_agent.SupportAgentsHandler)
]

# Error application
logger = logging.getLogger('tornado.application')
# report ERROR and above to Rollbar
rollbar_handler = RollbarHandler(
    access_token='3d825d37bb6a47df87ddd8444dd69fb2',
    environment='production',
    level=logging.ERROR)
# attach the handlers to the root logger
logger.addHandler(rollbar_handler)
# app
tornado_app = tornado.web.Application(handlers,
                                      default_handler_class=base.BaseHandler,
                                      **settings)
# Main app
application = tornado.wsgi.WSGIAdapter(tornado_app)

if __name__ == "__main__":
    import gevent.wsgi
    server = gevent.wsgi.WSGIServer(("", 8888), application)
    server.serve_forever()