Skip to content

Commit

Permalink
feat(#61): support multiple configurations (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedhyw authored May 23, 2024
1 parent 30d0a96 commit c484182
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 73 deletions.
73 changes: 4 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The application is designed to help in visualization, navigation, and analyzing

```sh
jlv file.json
jlv -config .jlv.jsonc file.json
```

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

It's also possible to define the path to the configuration using "-config" flag.

The Json path supports the described in [yalp/jsonpath](https://github.com/yalp/jsonpath#jsonpath-quick-intro) syntax.

Example configuration:
```jsonc
{
// Comments are allowed.
"fields": [
{
"title": "Time", // Max length is 32.
// Kind affects rendering. There are:
// * time;
// * numerictime;
// * secondtime;
// * millitime;
// * microtime;
// * level;
// * message;
// * any.
"kind": "numerictime",
"ref": [
// The application will display the first matched value.
"$.timestamp",
"$.time",
"$.t",
"$.ts"
],
"width": 30
},
{
"title": "Level",
"kind": "level",
"ref": [
"$.level",
"$.lvl",
"$.l"
],
"width": 10
},
{
"title": "Message",
"kind": "message",
"ref": [
"$.message",
"$.msg",
"$.error",
"$.err"
],
"width": 0 // The width will be calculated automatically.
},
{
"title": "Custom",
"kind": "any",
"ref": [
"$.custom"
],
"width": 0
},
],
// Mapping of log level.
// Possible values: none, trace, debug, info, warn, error, panic, fatal.
"customLevelMapping": {
// Replace "10" to "trace" in log level.
"10": "trace",
"20": "debug",
"30": "info",
"40": "warn",
"50": "error",
"60": "fatal"
}
}
```
Example configuration: [example.jlv.jsonc](example.jlv.jsonc).

### Time Formats
JSON Log Viewer can handle a variety of datetime formats when parsing your logs.
Expand Down
16 changes: 12 additions & 4 deletions cmd/jlv/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"os"
"path"
Expand All @@ -14,16 +15,19 @@ import (
const configFileName = ".jlv.jsonc"

func main() {
if len(os.Args) != 2 {
configPath := flag.String("config", "", "Path to the config")
flag.Parse()

if flag.NArg() != 1 {
fatalf("Invalid arguments, usage: %s file.log\n", os.Args[0])
}

cfg, err := readConfig()
cfg, err := readConfig(*configPath)
if err != nil {
fatalf("Error reading config: %s\n", err)
}

appModel := app.NewModel(os.Args[1], cfg)
appModel := app.NewModel(flag.Args()[0], cfg)
program := tea.NewProgram(appModel, tea.WithAltScreen())

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

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

if configPath != "" {
paths = append(paths, configPath)
}

workDir, err := os.Getwd()
if err == nil {
paths = append(paths, path.Join(workDir, configFileName))
Expand Down
66 changes: 66 additions & 0 deletions example.jlv.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
// Comments are allowed.
"fields": [
{
"title": "Time", // Max length is 32.
// Kind affects rendering. There are:
// * time;
// * numerictime;
// * secondtime;
// * millitime;
// * microtime;
// * level;
// * message;
// * any.
"kind": "numerictime",
"ref": [
// The application will display the first matched value.
"$.timestamp",
"$.time",
"$.t",
"$.ts"
],
"width": 30
},
{
"title": "Level",
"kind": "level",
"ref": [
"$.level",
"$.lvl",
"$.l"
],
"width": 10
},
{
"title": "Message",
"kind": "message",
"ref": [
"$.message",
"$.msg",
"$.error",
"$.err"
],
"width": 0 // The width will be calculated automatically.
},
{
"title": "Custom",
"kind": "any",
"ref": [
"$.custom"
],
"width": 0
}
],
// Mapping of log level.
// Possible values: none, trace, debug, info, warn, error, panic, fatal.
"customLevelMapping": {
// Replace "10" to "trace" in log level.
"10": "trace",
"20": "debug",
"30": "info",
"40": "warn",
"50": "error",
"60": "fatal"
}
}

0 comments on commit c484182

Please sign in to comment.