Skip to content

Commit

Permalink
support for jsonpath in column names
Browse files Browse the repository at this point in the history
  • Loading branch information
gsantoro committed Nov 23, 2022
1 parent 1caea9e commit ea30521
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/main.py",
"args": ["-i", "data/sample.elastic-agent.log"],
"args": ["-i", "data/sample.log", "-c", "@timestamp,log.logger,$.'log.origin'.'file.name',message"],
"console": "integratedTerminal",
"justMyCode": true
}
Expand Down
3 changes: 3 additions & 0 deletions index.todo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Todo:
- [x] add support for jsonpath when filtering columns
- [ ] add support for adding more highlighting via cmd line option
5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

@click.command()
@click.option("-i", "--input-path", help="Input path")
def main(input_path):
@click.option("-c", "--columns", default="@timestamp, log.level, message", help="Columns to filter")
def main(input_path, columns):
highlighter = LogHighlighter()
theme = LogTheme

Expand All @@ -15,7 +16,7 @@ def main(input_path):
else:
input_file = open(input_path, 'r')

log = ColoredLogs("Logs", "@timestamp, log.level, message", input_file, highlighter, theme)
log = ColoredLogs("Logs", columns, input_file, highlighter, theme)
log.process()

if __name__ == '__main__':
Expand Down
15 changes: 11 additions & 4 deletions src/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from rich.table import Table, Column
from rich.markup import escape

from jsonpath_ng import jsonpath, parse

class ColoredLogs:
def __init__(self, title, columns, input, highlighter, theme):
self.title = title
Expand All @@ -17,8 +19,8 @@ def get_cols(self, columns):
parts = columns.split(",")
ans = []
for col in parts:
col = col.strip()
c = Column(col, justify="left", style="grey62", overflow="fold")
col_name = col.strip()
c = Column(col_name, justify="left", style="grey62", overflow="fold")
ans.append(c)
return ans

Expand All @@ -32,12 +34,17 @@ def process(self):

values = []
for col in self.columns:
v = escape(d[col.header])
if str(col.header).startswith("$"):
jsonpath_expression = parse(col.header)
match = jsonpath_expression.find(d)
v = escape(match[0].value)
else:
v = escape(d[col.header])

values.append(v)

table.add_row(*values)
except:
except Exception as e:
print(f"Ignored line: {line}", end="")

line = self.input.readline()
Expand Down

0 comments on commit ea30521

Please sign in to comment.