8000 add test for new feature · jbadiapa/fluent-logger-python@268c6a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 268c6a7

Browse files
committed
add test for new feature
1 parent 3dd0408 commit 268c6a7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/test_handler.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,48 @@ def test_custom_fmt(self):
6262
self.assertTrue('lineno' in data[0][2])
6363
self.assertTrue('emitted_at' in data[0][2])
6464

65+
def test_custom_field_raise_exception(self):
66+
handler = fluent.handler.FluentHandler('app.follow', port=self._port)
67+
68+
logging.basicConfig(level=logging.INFO)
69+
log = logging.getLogger('fluent.test')
70+
handler.setFormatter(
71+
fluent.handler.FluentRecordFormatter(fmt={
72+
'name': '%(name)s',
73+
'custom_field': '%(custom_field)s'
74+
})
75+
)
76+
log.addHandler(handler)
77+
with self.assertRaises(KeyError):
78+
log.info({'sample': 'value'})
79+
log.removeHandler(handler)
80+
handler.close()
81+
82+
def test_custom_field_fill_missing_fmt_key_is_true(self):
83+
handler = fluent.handler.FluentHandler('app.follow', port=self._port)
84+
85+
logging.basicConfig(level=logging.INFO)
86+
log = logging.getLogger('fluent.test')
87+
handler.setFormatter(
88+
fluent.handler.FluentRecordFormatter(fmt={
89+
'name': '%(name)s',
90+
'custom_field': '%(custom_field)s'
91+
},
92+
fill_missing_fmt_key=True
93+
)
94+
)
95+
log.addHandler(handler)
96+
log.info({'sample': 'value'})
97+
log.removeHandler(handler)
98+
handler.close()
99+
100+
data = self.get_data()
101+
self.assertTrue('name' in data[0][2])
102+
self.assertEqual('fluent.test', data[0][2]['name'])
103+
self.assertTrue('custom_field' in data[0][2])
104+
# field defaults to none if not in log record
105+
self.assertIsNone(data[0][2]['custom_field'])
106+
65107
def test_json_encoded_message(self):
66108
handler = fluent.handler.FluentHandler('app.follow', port=self._port)
67109

0 commit comments

Comments
 (0)
0