Пример #1
0
 def test_nojournal_from_generic_logger(self):
     offset = len(_journal_file.getvalue())
     logger = logging.getLogger('test_nojournal_from_generic_logger')
     logger.addHandler(JournalLogHandler(path=None))
     logger.error('Hello, World!',
                  extra={'citest_journal': {'nojournal':True}})
     self.assertEqual(offset, len(_journal_file.getvalue()))
Пример #2
0
  def test_context_logging(self):
      offset = len(_journal_file.getvalue())
      logger = JournalLogger('test_journal_logger')
      logger.addHandler(JournalLogHandler(path=None))
      citest_extra = {'foo':'bar'}
      start_time = _journal_clock.last_time

      JournalLogger.execute_in_context(
        'The Test Context',
        lambda: {logger.debug('Test Log Message')},
        **citest_extra)

      expect_sequence = [
        {
          '_title': 'The Test Context',
          '_type': 'JournalContextControl',
          '_timestamp': start_time + 1,
          '_thread': current_thread().ident,
          'control': 'BEGIN',
          'foo': 'bar',
        },
        {
          '_value': 'Test Log Message',
          '_type': 'JournalMessage',
          '_level': logging.DEBUG,
          '_timestamp': start_time + 2,
          '_thread': current_thread().ident,
          'format': 'pre'
        },
        {
          '_type': 'JournalContextControl',
          '_timestamp': start_time + 3,
          '_thread': current_thread().ident,
          'control': 'END'
        }
      ]

      entry_str = _journal_file.getvalue()[offset:]
      input_stream = RecordInputStream(BytesIO(entry_str))
      for expect in expect_sequence:
        json_str = next(input_stream)
        json_dict = json_module.JSONDecoder().decode(json_str)
        self.assertEqual(expect, json_dict)
Пример #3
0
  def test_journal_logger_with_custom_message(self):
      offset = len(_journal_file.getvalue())
      logger = JournalLogger(__name__)
      logger.addHandler(JournalLogHandler(path=None))
      citest_extra = {'foo':'bar', '_journal_message':'HELLO, JOURNAL'}
      logger.debug('Hello, World!', extra={'citest_journal': citest_extra})

      expect = {
          '_value': 'HELLO, JOURNAL',
          '_type': 'JournalMessage',
          '_level': logging.DEBUG,
          '_timestamp': _journal_clock.last_time,
          '_thread': current_thread().ident,
          'foo': 'bar',
          'format': 'pre'
      }

      entry_str = _journal_file.getvalue()[offset:]
      json_str = next(RecordInputStream(BytesIO(entry_str)))
      json_dict = json_module.JSONDecoder().decode(json_str)
      self.assertEqual(expect, json_dict)
Пример #4
0
  def test_journal_logger(self):
      offset = len(_journal_file.getvalue())
      logger = JournalLogger('test_journal_logger')
      logger.addHandler(JournalLogHandler(path=None))
      citest_extra = {'foo':'bar', 'format':'FMT'}
      logger.info('Hello, World!', extra={'citest_journal': citest_extra})

      expect = {
          '_value': 'Hello, World!',
          '_type': 'JournalMessage',
          '_level': logging.INFO,
          '_timestamp': _journal_clock.last_time,
          '_thread': current_thread().ident,
          'foo': 'bar',
          'format': 'FMT',
      }

      entry_str = _journal_file.getvalue()[offset:]
      json_str = next(RecordInputStream(BytesIO(entry_str)))
      json_dict = json_module.JSONDecoder().decode(json_str)
      self.assertEqual(expect, json_dict)
Пример #5
0
  def test_journal_log_handler_from_generic_logger(self):
      offset = len(_journal_file.getvalue())
      logger = logging.getLogger('test_journal_log_handler')
      logger.addHandler(JournalLogHandler(path=None))
      citest_extra = {'foo':'bar', '_journal_message':'HELLO, JOURNAL'}
      logger.error('Hello, World!', extra={'citest_journal': citest_extra})

      # Note the extra args arent visible because they arent in the normal
      # LogRecord.
      expect = {
          '_value': 'HELLO, JOURNAL',
          '_type': 'JournalMessage',
          '_level': logging.ERROR,
          '_timestamp': _journal_clock.last_time,
          '_thread': current_thread().ident,
          'foo': 'bar',
          'format': 'pre',
      }

      entry_str = _journal_file.getvalue()[offset:]
      json_str = next(RecordInputStream(BytesIO(entry_str)))
      json_dict = json_module.JSONDecoder().decode(json_str)
      self.assertEqual(expect, json_dict)