Skip to content

Commit 6cfd06a

Browse files
committed
Fix support for asctime attribute
This does not work in python2.6 as it did not use usesTime to detect the presence of asctime in fmt.
1 parent d08a557 commit 6cfd06a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: python
22
python:
3-
- "2.6"
43
- "2.7"
54
- "3.2"
65
- "3.3"

fluent/handler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import socket
5+
import sys
56

67
try:
78
import simplejson as json
@@ -38,6 +39,10 @@ def __init__(self, fmt=None, datefmt=None):
3839
self.hostname = socket.gethostname()
3940

4041
def format(self, record):
42+
# Only needed for python2.6
43+
if sys.version_info[0:2] <= (2, 6) and self.usesTime():
44+
record.asctime = self.formatTime(record, self.datefmt)
45+
4146
# Compute attributes handled by parent class.
4247
super(FluentRecordFormatter, self).format(record)
4348
# Add ours
@@ -49,6 +54,10 @@ def format(self, record):
4954
self._structuring(data, record.msg)
5055
return data
5156

57+
def usesTime(self):
58+
return any([value.find('%(asctime)') >= 0
59+
for value in self._fmt_dict.values()])
60+
5261
def _structuring(self, data, msg):
5362
""" Melds `msg` into `data`.
5463

tests/test_handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def test_custom_fmt(self):
5454
fluent.handler.FluentRecordFormatter(fmt={
5555
'name': '%(name)s',
5656
'lineno': '%(lineno)d',
57+
'emitted_at': '%(asctime)s',
5758
})
5859
)
5960
log.addHandler(handler)
@@ -64,6 +65,7 @@ def test_custom_fmt(self):
6465
self.assertTrue('name' in data[0][2])
6566
self.assertEqual('fluent.test', data[0][2]['name'])
6667
self.assertTrue('lineno' in data[0][2])
68+
self.assertTrue('emitted_at' in data[0][2])
6769

6870
def test_unstructured_message(self):
6971
handler = fluent.handler.FluentHandler('app.follow', port=self._port)

0 commit comments

Comments
 (0)