Skip to content

Commit 2f3aea7

Browse files
committed
Added support for rawjson format
1 parent e1fcd00 commit 2f3aea7

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

README.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Usage
2929
usage::
3030

3131
beaver [-h] [-c CONFIG] [-d] [-D] [-f FILES [FILES ...]]
32-
[-F {json,msgpack,string,raw}] [-H HOSTNAME] [-m {bind,connect}]
32+
[-F {json,msgpack,raw,rawjson,string}] [-H HOSTNAME] [-m {bind,connect}]
3333
[-l OUTPUT] [-p PATH] [-P PID]
3434
[-t {rabbitmq,redis,sqs,stdout,udp,zmq}] [-v] [--fqdn]
3535

@@ -43,7 +43,7 @@ optional arguments::
4343
-f FILES [FILES ...], --files FILES [FILES ...]
4444
space-separated filelist to watch, can include globs
4545
(*.log). Overrides --path argument
46-
-F {json,msgpack,string,raw}, --format {json,msgpack,string,raw}
46+
-F {json,msgpack,raw,rawjson,string}, --format {json,msgpack,raw,rawjson,string}
4747
format to use when sending to transport
4848
-H HOSTNAME, --hostname HOSTNAME
4949
manual hostname override for source_host
@@ -303,14 +303,18 @@ Example 12: SQS Transport::
303303
# From the commandline
304304
beaver -c /etc/beaver.conf -t sqs
305305

306+
Example 13: [Raw Json Support](http://blog.pkhamre.com/2012/08/23/logging-to-logstash-json-format-in-nginx/::
307+
308+
beaver --format rawjson
309+
306310
Todo
307311
====
308312

309313
* More documentation
310-
* Use python threading + subprocess in order to support usage of ``yield`` across all operating systems
311-
* ~~Fix usage on non-linux platforms - file.readline() does not work as expected on OS X. See above for potential solution~~
314+
* <del>Use python threading + subprocess in order to support usage of ``yield`` across all operating systems</del>
315+
* <del>Fix usage on non-linux platforms - file.readline() does not work as expected on OS X. See above for potential solution</del>
312316
* More transports
313-
* ~~Ability to specify files, tags, and other metadata within a configuration file~~
317+
* <del>Ability to specify files, tags, and other metadata within a configuration file</del>
314318

315319
Caveats
316320
=======

beaver/transport.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ def __init__(self, beaver_config, file_config, logger=None):
5050
pass
5151
else:
5252
break
53+
elif beaver_config.get('format') == 'rawjson':
54+
# priority: ujson > simplejson > jsonlib2 > json
55+
priority = ['ujson', 'simplejson', 'jsonlib2', 'json']
56+
for mod in priority:
57+
try:
58+
json = __import__(mod)
59+
def rawjson_formatter(data):
60+
json_data = json.loads(data['@message'])
61+
for field in {'@source', '@type', '@tags', '@source_host', '@source_path'}:
62+
json_data[field] = data[field]
63+
return json.dumps(json_data)
64+
self._formatter = rawjson_formatter
65+
except ImportError:
66+
pass
67+
else:
68+
break
5369
elif beaver_config.get('format') == 'string':
5470
def string_formatter(data):
5571
return "[{0}] [{1}] {2}".format(data['@source_host'], data['@timestamp'], data['@message'])

beaver/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def parse_args():
3131
parser.add_argument('-d', '--debug', help='enable debug mode', dest='debug', default=False, action='store_true')
3232
parser.add_argument('-D', '--daemonize', help='daemonize in the background', dest='daemonize', default=False, action='store_true')
3333
parser.add_argument('-f', '--files', help='space-separated filelist to watch, can include globs (*.log). Overrides --path argument', dest='files', default=None, nargs='+')
34-
parser.add_argument('-F', '--format', help='format to use when sending to transport', default=None, dest='format', choices=['json', 'msgpack', 'string', 'raw'])
34+
parser.add_argument('-F', '--format', help='format to use when sending to transport', default=None, dest='format', choices=['json', 'msgpack', 'raw', 'rawjson', 'string'])
3535
parser.add_argument('-H', '--hostname', help='manual hostname override for source_host', default=None, dest='hostname')
3636
parser.add_argument('-m', '--mode', help='bind or connect mode', dest='mode', default=None, choices=['bind', 'connect'])
3737
parser.add_argument('-l', '--logfile', '-o', '--output', help='file to pipe output to (in addition to stdout)', default=None, dest='output')

0 commit comments

Comments
 (0)