Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: JSON output #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bonfire/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .graylog_api import SearchRange, SearchQuery
from .utils import cli_error, api_from_config, api_from_host
from .output import run_logprint
from .formats import tail_format, dump_format
from .formats import tail_format, dump_format, json_format


@click.command()
Expand All @@ -37,6 +37,7 @@
@click.option("-#", "--search-to", default=None, help="Query range to (default: now)")
@click.option('-t', '--tail', 'mode', flag_value='tail', default=True, help="Show the last n lines for the query (default)")
@click.option('-d', '--dump', 'mode', flag_value='dump', help="Print the query result as a csv")
@click.option('-j', '--json', 'mode', flag_value='json', help="Print the raw JSON log objects, one per line")
@click.option("-f", "--follow", default=False, is_flag=True, help="Poll the logging server for new logs matching the query (sets search from to now, limit to None)")
@click.option("-l", "--interval", default=1000, help="Polling interval in ms (default: 1000)")
@click.option("-n", "--limit", default=10, help="Limit the number of results (default: 10)")
Expand Down Expand Up @@ -200,6 +201,8 @@ def run(host,
formatter = tail_format(fields, color)
elif mode == "dump":
formatter = dump_format(fields, color)
elif mode == "json":
formatter = json_format

run_logprint(gl_api, q, formatter, follow, interval, latency)

Expand Down
4 changes: 4 additions & 0 deletions bonfire/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from termcolor import colored
import syslog
import json

def get_log_color_and_background(level):
log_color = 'green'
Expand Down Expand Up @@ -43,3 +44,6 @@ def format(entry):
return colored(msg, log_color, log_background)
return msg
return format

def json_format(entry):
return json.dumps(entry.message_dict)