Skip to content

Commit c484182

Browse files
authored
feat(#61): support multiple configurations (#67)
1 parent 30d0a96 commit c484182

File tree

3 files changed

+82
-73
lines changed

3 files changed

+82
-73
lines changed

README.md

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The application is designed to help in visualization, navigation, and analyzing
4949

5050
```sh
5151
jlv file.json
52+
jlv -config .jlv.jsonc file.json
5253
```
5354

5455
| Key | Action |
@@ -104,77 +105,11 @@ The application will look for the config `.jlv.jsonc` in the working directory o
104105
- `$PWD/.jlv.jsonc`;
105106
- `$HOME/.jlv.jsonc`.
106107

108+
It's also possible to define the path to the configuration using "-config" flag.
109+
107110
The Json path supports the described in [yalp/jsonpath](https://github.com/yalp/jsonpath#jsonpath-quick-intro) syntax.
108111

109-
Example configuration:
110-
```jsonc
111-
{
112-
// Comments are allowed.
113-
"fields": [
114-
{
115-
"title": "Time", // Max length is 32.
116-
// Kind affects rendering. There are:
117-
// * time;
118-
// * numerictime;
119-
// * secondtime;
120-
// * millitime;
121-
// * microtime;
122-
// * level;
123-
// * message;
124-
// * any.
125-
"kind": "numerictime",
126-
"ref": [
127-
// The application will display the first matched value.
128-
"$.timestamp",
129-
"$.time",
130-
"$.t",
131-
"$.ts"
132-
],
133-
"width": 30
134-
},
135-
{
136-
"title": "Level",
137-
"kind": "level",
138-
"ref": [
139-
"$.level",
140-
"$.lvl",
141-
"$.l"
142-
],
143-
"width": 10
144-
},
145-
{
146-
"title": "Message",
147-
"kind": "message",
148-
"ref": [
149-
"$.message",
150-
"$.msg",
151-
"$.error",
152-
"$.err"
153-
],
154-
"width": 0 // The width will be calculated automatically.
155-
},
156-
{
157-
"title": "Custom",
158-
"kind": "any",
159-
"ref": [
160-
"$.custom"
161-
],
162-
"width": 0
163-
},
164-
],
165-
// Mapping of log level.
166-
// Possible values: none, trace, debug, info, warn, error, panic, fatal.
167-
"customLevelMapping": {
168-
// Replace "10" to "trace" in log level.
169-
"10": "trace",
170-
"20": "debug",
171-
"30": "info",
172-
"40": "warn",
173-
"50": "error",
174-
"60": "fatal"
175-
}
176-
}
177-
```
112+
Example configuration: [example.jlv.jsonc](example.jlv.jsonc).
178113

179114
### Time Formats
180115
JSON Log Viewer can handle a variety of datetime formats when parsing your logs.

cmd/jlv/main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67
"path"
@@ -14,16 +15,19 @@ import (
1415
const configFileName = ".jlv.jsonc"
1516

1617
func main() {
17-
if len(os.Args) != 2 {
18+
configPath := flag.String("config", "", "Path to the config")
19+
flag.Parse()
20+
21+
if flag.NArg() != 1 {
1822
fatalf("Invalid arguments, usage: %s file.log\n", os.Args[0])
1923
}
2024

21-
cfg, err := readConfig()
25+
cfg, err := readConfig(*configPath)
2226
if err != nil {
2327
fatalf("Error reading config: %s\n", err)
2428
}
2529

26-
appModel := app.NewModel(os.Args[1], cfg)
30+
appModel := app.NewModel(flag.Args()[0], cfg)
2731
program := tea.NewProgram(appModel, tea.WithAltScreen())
2832

2933
if _, err := program.Run(); err != nil {
@@ -38,9 +42,13 @@ func fatalf(message string, args ...any) {
3842

3943
// readConfig tries to read config from working directory or home directory.
4044
// If configs are not found, then it returns a default configuration.
41-
func readConfig() (*config.Config, error) {
45+
func readConfig(configPath string) (*config.Config, error) {
4246
paths := []string{}
4347

48+
if configPath != "" {
49+
paths = append(paths, configPath)
50+
}
51+
4452
workDir, err := os.Getwd()
4553
if err == nil {
4654
paths = append(paths, path.Join(workDir, configFileName))

example.jlv.jsonc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
// Comments are allowed.
3+
"fields": [
4+
{
5+
"title": "Time", // Max length is 32.
6+
// Kind affects rendering. There are:
7+
// * time;
8+
// * numerictime;
9+
// * secondtime;
10+
// * millitime;
11+
// * microtime;
12+
// * level;
13+
// * message;
14+
// * any.
15+
"kind": "numerictime",
16+
"ref": [
17+
// The application will display the first matched value.
18+
"$.timestamp",
19+
"$.time",
20+
"$.t",
21+
"$.ts"
22+
],
23+
"width": 30
24+
},
25+
{
26+
"title": "Level",
27+
"kind": "level",
28+
"ref": [
29+
"$.level",
30+
"$.lvl",
31+
"$.l"
32+
],
33+
"width": 10
34+
},
35+
{
36+
"title": "Message",
37+
"kind": "message",
38+
"ref": [
39+
"$.message",
40+
"$.msg",
41+
"$.error",
42+
"$.err"
43+
],
44+
"width": 0 // The width will be calculated automatically.
45+
},
46+
{
47+
"title": "Custom",
48+
"kind": "any",
49+
"ref": [
50+
"$.custom"
51+
],
52+
"width": 0
53+
}
54+
],
55+
// Mapping of log level.
56+
// Possible values: none, trace, debug, info, warn, error, panic, fatal.
57+
"customLevelMapping": {
58+
// Replace "10" to "trace" in log level.
59+
"10": "trace",
60+
"20": "debug",
61+
"30": "info",
62+
"40": "warn",
63+
"50": "error",
64+
"60": "fatal"
65+
}
66+
}

0 commit comments

Comments
 (0)