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

Add a new function parseJSON to --template flag #160

Merged
merged 1 commit into from
Sep 20, 2021

Conversation

superbrothers
Copy link
Member

parseJSON function parses string as JSON.

For example, if an application outputs the following log message:

{"level": "INFO", "message": "This is log message"}

We can use parseJSON function to format logs as follows:

$ stern . --template='{{.PodName}}/{{.ContainerName}} {{with $d := .Message | parseJSON}}[{{$d.level}}] {{$d.message}}{{end}}{{"\n"}}'
app-544bbcd5dc-fwgdj/application [INFO] This is log message

You can try this new function using the following steps:

cat <<EOL | kubectl apply -f-
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
spec:
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - image: busybox
        name: application
        command: ["/bin/sh"]
        args:
        - -c
        - |-
          while true; do
            echo '{"level": "INFO", "message": "This is log message"}'
            sleep 1
          done
EOL

stern . --template='{{.PodName}}/{{.ContainerName}} {{with $d := .Message | parseJSON}}[{{$d.level}}] {{$d.message}}{{end}}{{"\n"}}'

Closes #22

`parseJSON` function parses string as JSON.

For example, if an application outputs the following log message:

```
{"level": "INFO", "message": "This is log message"}
```

We can use `parseJSON` function to format logs as follows:

```
$ stern . --template='{{.PodName}}/{{.ContainerName}} {{with $d := .Message | parseJSON}}[{{$d.level}}] {{$d.message}}{{end}}{{"\n"}}'
app-544bbcd5dc-fwgdj/application [INFO] This is log message
```
Copy link
Member

@rkmathi rkmathi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!
this is very convenient😺

@superbrothers superbrothers merged commit 60e2a77 into master Sep 20, 2021
@superbrothers superbrothers deleted the template-parseJSON branch September 20, 2021 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tailing logs with JSON output.
2 participants