Exemplo n.º 1
0
 def test_log_file_path(self):
     """Test the log_file_path returns correct path."""
     log_temp_dir = temp_dir('logs')
     actual_path = os.path.join(log_temp_dir, 'inasafe.log')
     message = 'Actual log path: %s, I got %s' % (
         actual_path, log_file_path())
     self.assertEqual(actual_path, log_file_path(), message)
Exemplo n.º 2
0
 def test_log_file_path(self):
     """Test the log_file_path returns correct path."""
     log_temp_dir = temp_dir('logs')
     actual_path = os.path.join(log_temp_dir, 'inasafe.log')
     message = 'Actual log path: %s, I got %s' % (
         actual_path, log_file_path())
     self.assertEqual(actual_path, log_file_path(), message)
Exemplo n.º 3
0
 def test_logging(self):
     inasafe_log_path = log_file_path()
     current_date = datetime.datetime.now()
     date_string = current_date.strftime('%d-%m-%Y-%H:%M:%S')
     message = 'Testing logger %s' % date_string
     LOGGER.info(message)
     log_file = open(inasafe_log_path)
     log_lines = str(log_file.readlines())
     self.assertIn(message, log_lines,
                   'Error, expected log message not shown in logs')
     log_file.close()
Exemplo n.º 4
0
 def test_logging(self):
     inasafe_log_path = log_file_path()
     current_date = datetime.datetime.now()
     date_string = current_date.strftime('%d-%m-%Y-%H:%M:%S')
     message = 'Testing logger %s' % date_string
     LOGGER.info(message)
     log_file = open(inasafe_log_path)
     log_lines = str(log_file.readlines())
     self.assertIn(
         message,
         log_lines,
         'Error, expected log message not shown in logs')
     log_file.close()
Exemplo n.º 5
0
def setup_logger(logger_name, log_file=None, sentry_url=None):
    """Run once when the module is loaded and enable logging.

    :param logger_name: The logger name that we want to set up.
    :type logger_name: str

    :param log_file: Optional full path to a file to write logs to.
    :type log_file: str

    :param sentry_url: Optional url to sentry api for remote
        logging. Defaults to http://c64a83978732474ea751d432ab943a6b:
        [email protected]/5 which is the
        sentry project for InaSAFE desktop.
    :type sentry_url: str

    Borrowed heavily from this:
    http://docs.python.org/howto/logging-cookbook.html

    Now to log a message do::

       LOGGER.debug('Some debug message')

    .. note:: The file logs are written to the inasafe user tmp dir e.g.:
       /tmp/inasafe/23-08-2012/timlinux/logs/inasafe.log

    """
    logger = logging.getLogger(logger_name)
    logger.setLevel(logging.DEBUG)
    default_handler_level = logging.DEBUG

    # create formatter that will be added to the handlers
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # create syslog handler which logs even debug messages
    # (ariel): Make this log to /var/log/safe.log instead of
    #               /var/log/syslog
    # (Tim) Ole and I discussed this - we prefer to log into the
    # user's temporary working directory.
    inasafe_log_path = log_file_path()
    if log_file is None:
        file_handler = logging.FileHandler(inasafe_log_path)
    else:
        file_handler = logging.FileHandler(log_file)
    file_handler.setLevel(default_handler_level)

    # create console handler with a higher log level
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)

    # create a QGIS handler
    qgis_handler = QgsLogHandler()

    # Set formatters
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)
    qgis_handler.setFormatter(formatter)

    # add the handlers to the logger
    add_logging_handler_once(logger, file_handler)
    add_logging_handler_once(logger, console_handler)
    add_logging_handler_once(logger, qgis_handler)

    # Sentry handler - this is optional hence the localised import
    # It will only log if pip install raven. If raven is available
    # logging messages will be sent to http://sentry.linfiniti.com
    # We will log exceptions only there. You need to either:
    #  * Set env var 'INASAFE_SENTRY=1' present (value can be anything)
    # before this will be enabled or sentry is enabled in QSettings
    settings = QSettings()
    flag = settings.value('inasafe/useSentry', False, type=bool)
    env_inasafe_sentry = 'INASAFE_SENTRY' in os.environ

    if env_inasafe_sentry or flag:
        if sentry_url is None:
            client = Client(
                'http://11b7c9cb73874f97807ebc1934575e92'
                ':[email protected]/5')
        else:
            client = Client(sentry_url)
        sentry_handler = SentryHandler(client)
        sentry_handler.setFormatter(formatter)
        sentry_handler.setLevel(logging.ERROR)
        if add_logging_handler_once(logger, sentry_handler):
            logger.debug('Sentry logging enabled in safe')
    else:
        logger.debug('Sentry logging disabled in safe')
Exemplo n.º 6
0
 def test_log_file_path(self):
     """Test the log_file_path returns correct path."""
     log_temp_dir = temp_dir('logs')
     actual_path = os.path.join(log_temp_dir, 'inasafe.log')
     self.assertEqual(actual_path, log_file_path())
Exemplo n.º 7
0
 def test_log_file_path(self):
     """Test the log_file_path returns correct path."""
     log_temp_dir = temp_dir('logs')
     actual_path = os.path.join(log_temp_dir, 'inasafe.log')
     self.assertEqual(actual_path, log_file_path())
Exemplo n.º 8
0
def setup_logger(logger_name, log_file=None, sentry_url=None):
    """Run once when the module is loaded and enable logging.

    :param logger_name: The logger name that we want to set up.
    :type logger_name: str

    :param log_file: Optional full path to a file to write logs to.
    :type log_file: str

    :param sentry_url: Optional url to sentry api for remote
        logging. Defaults to http://c64a83978732474ea751d432ab943a6b:
        [email protected]/5 which is the
        sentry project for InaSAFE desktop.
    :type sentry_url: str

    Borrowed heavily from this:
    http://docs.python.org/howto/logging-cookbook.html

    Now to log a message do::

       LOGGER.debug('Some debug message')

    .. note:: The file logs are written to the inasafe user tmp dir e.g.:
       /tmp/inasafe/23-08-2012/timlinux/logs/inasafe.log

    """
    logger = logging.getLogger(logger_name)
    logger.setLevel(logging.DEBUG)
    default_handler_level = logging.DEBUG

    # create formatter that will be added to the handlers
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # create syslog handler which logs even debug messages
    # (ariel): Make this log to /var/log/safe.log instead of
    #               /var/log/syslog
    # (Tim) Ole and I discussed this - we prefer to log into the
    # user's temporary working directory.
    inasafe_log_path = log_file_path()
    if log_file is None:
        file_handler = logging.FileHandler(inasafe_log_path)
    else:
        file_handler = logging.FileHandler(log_file)
    file_handler.setLevel(default_handler_level)

    # create console handler with a higher log level
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)

    # create a QGIS handler
    qgis_handler = QgsLogHandler()

    # Set formatters
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)
    qgis_handler.setFormatter(formatter)

    # add the handlers to the logger
    add_logging_handler_once(logger, file_handler)
    add_logging_handler_once(logger, console_handler)
    add_logging_handler_once(logger, qgis_handler)

    # Sentry handler - this is optional hence the localised import
    # It will only log if pip install raven. If raven is available
    # logging messages will be sent to http://sentry.linfiniti.com
    # We will log exceptions only there. You need to either:
    #  * Set env var 'INASAFE_SENTRY=1' present (value can be anything)
    # before this will be enabled or sentry is enabled in QSettings
    settings = QSettings()
    flag = settings.value('inasafe/useSentry', False, type=bool)
    env_inasafe_sentry = 'INASAFE_SENTRY' in os.environ

    if env_inasafe_sentry or flag:
        if sentry_url is None:
            client = Client(
                'http://11b7c9cb73874f97807ebc1934575e92'
                ':[email protected]/5')
        else:
            client = Client(sentry_url)
        sentry_handler = SentryHandler(client)
        sentry_handler.setFormatter(formatter)
        sentry_handler.setLevel(logging.ERROR)
        if add_logging_handler_once(logger, sentry_handler):
            logger.debug('Sentry logging enabled in safe')
    else:
        logger.debug('Sentry logging disabled in safe')
Exemplo n.º 9
0
def setup_logger(logger_name, log_file=None, sentry_url=None):
    """Run once when the module is loaded and enable logging.

    :param logger_name: The logger name that we want to set up.
    :type logger_name: str

    :param log_file: Optional full path to a file to write logs to.
    :type log_file: str

    :param sentry_url: Optional url to sentry api for remote
        logging. Defaults to URL defined in safe.definitions.sentry.py
        which is the sentry project for InaSAFE desktop.
    :type sentry_url: str

    Borrowed heavily from this:
    http://docs.python.org/howto/logging-cookbook.html

    Now to log a message do::

       LOGGER.debug('Some debug message')

    .. note:: The file logs are written to the inasafe user tmp dir e.g.:
       /tmp/inasafe/23-08-2012/timlinux/logs/inasafe.log
    """
    logger = logging.getLogger(logger_name)
    logging_level = int(os.environ.get('INASAFE_LOGGING_LEVEL', logging.DEBUG))
    logger.setLevel(logging_level)
    default_handler_level = logging_level

    # create formatter that will be added to the handlers
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    # create syslog handler which logs even debug messages
    # (ariel): Make this log to /var/log/safe.log instead of
    #               /var/log/syslog
    # (Tim) Ole and I discussed this - we prefer to log into the
    # user's temporary working directory.
    inasafe_log_path = log_file_path()
    if log_file is None:
        file_handler = logging.FileHandler(inasafe_log_path)
    else:
        file_handler = logging.FileHandler(log_file)
    file_handler.setLevel(default_handler_level)
    file_handler.setFormatter(formatter)
    add_logging_handler_once(logger, file_handler)

    if 'MUTE_LOGS' not in os.environ:
        # create console handler with a higher log level
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.INFO)
        console_handler.setFormatter(formatter)
        add_logging_handler_once(logger, console_handler)

    # create a QGIS handler
    qgis_handler = QgsLogHandler()
    qgis_handler.setFormatter(formatter)
    add_logging_handler_once(logger, qgis_handler)

    # Sentry handler - this is optional hence the localised import
    # If raven is available logging messages will be sent to
    # http://sentry.kartoza.com
    # We will log exceptions only there. You need to either:
    #  * Set env var 'INASAFE_SENTRY=1' present (value can be anything)
    # before this will be enabled or sentry is enabled in QSettings
    qsettings_flag = QSettings().value('inasafe/useSentry', False, type=bool)
    environment_flag = 'INASAFE_SENTRY' in os.environ

    if environment_flag or qsettings_flag:
        if sentry_url is None:
            sentry_url = PRODUCTION_SERVER

        tags = dict()
        tags[provenance_gdal_version['provenance_key']] = gdal.__version__
        tags[provenance_os['provenance_key']] = readable_os_version()
        qgis_short_version = provenance_qgis_version['provenance_key']
        qgis_full_version = qgis_short_version + '_full'
        versions = [str(v) for v in qgis_version_detailed()]
        tags[qgis_short_version] = '.'.join(versions[0:2])
        tags[qgis_full_version] = '.'.join(versions[0:3])
        tags[provenance_qt_version['provenance_key']] = QT_VERSION_STR

        hostname = os.environ.get('HOSTNAME_SENTRY', socket.gethostname())

        sentry_handler = SentryHandler(
            dsn=sentry_url,
            name=hostname,
            release=get_version(),
            tags=tags,
        )
        sentry_handler.setFormatter(formatter)
        sentry_handler.setLevel(logging.ERROR)
        if add_logging_handler_once(logger, sentry_handler):
            logger.debug('Sentry logging enabled in safe')
    else:
        logger.debug('Sentry logging disabled in safe')