https://github.com/gitkashish/golog
Simple and efficient log parsing and formatting tool. It allows you to define templates to extract and present log data in a structured and readable way
https://github.com/gitkashish/golog
cli-app formatter golang logging
Last synced: about 1 month ago
JSON representation
Simple and efficient log parsing and formatting tool. It allows you to define templates to extract and present log data in a structured and readable way
- Host: GitHub
- URL: https://github.com/gitkashish/golog
- Owner: gitKashish
- License: gpl-3.0
- Created: 2025-01-17T20:58:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-08T10:05:29.000Z (over 1 year ago)
- Last Synced: 2025-03-08T10:27:36.618Z (over 1 year ago)
- Topics: cli-app, formatter, golang, logging
- Language: Go
- Homepage:
- Size: 1.9 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# golog
`golog` is a simple and efficient log parsing and formatting tool. It allows you to define templates to extract and present log data in a structured and readable way.
[](https://goreportcard.com/report/github.com/gitKashish/golog) [](https://www.gnu.org/licenses/gpl-3.0)
## 🚀 Getting Started
### Installation
#### Binary Installation (Recommended)
```bash
# Install the latest version
go install github.com/gitKashish/golog@latest
```
This will install the `golog` binary to your `$GOPATH/bin` directory (or `$GOBIN` if set). Make sure this directory is in your `PATH`.
#### Building from Source
```bash
# Clone repository
git clone https://github.com/gitKashish/golog.git
# Build binary
cd golog
go build
```
This will create a `golog` executable in your current directory.
### Creating `template.yaml`
`golog` relies on a `template.yaml` file to define how logs should be parsed and formatted. Create this file in the same directory as the `golog` binary.
## 📄 Template Format
The `template.yaml` file defines two key templates: `sourceTemplate` and `targetTemplate`.
### 1. `sourceTemplate`
The `sourceTemplate` describes the structure of your *incoming* log lines. It uses a specific format for defining fields:
```
@fieldName-fieldType@
```
* **`fieldName`:** The name of the field (must be unique, alphanumeric characters and underscores only).
* **`fieldType`:** The data type of the field.
Supported Field Types:
| Type | Symbol | Description |
| --------- | :----: | ------------------------------------------------------------------------ |
| Raw | `raw` | Value is returned as is (no formatting). |
| Number | `number` | Value is treated as a number. |
| String | `string` | Value is treated as a string. |
| JSON | `json` | Value is parsed as a JSON string and pretty-printed. |
| Timestamp | `timestamp` | Value is parsed as a timestamp and formatted into RFC822Z format. |
| Default | N/A | Used internally when the log doesn't match the `sourceTemplate`. |
### 2. `targetTemplate`
The `targetTemplate` defines how the *output* should be formatted. It uses the field names defined in the `sourceTemplate`:
```
@fieldName@
```
A field name can be used multiple times in the `targetTemplate`.
## ✨ Example
**Log Input:**
```
5|3022 | -->2025-01-24 07:29:52.954 :----: users :=: updateJobStatus :=: {"EVENT":"deleteNotificationTaskFromScheduler","ERROR":{"errno":-110,"code":"ETIMEDOUT","syscall":"connect","address":"52.41.75.101","port":3013}}
```
**`template.yaml`:**
```yaml
sourceTemplate: "@server-number@|@instance-number@ | -->@time-timestamp@ :----: @module-string@ :=: @api-string@ :=: @details-json@"
targetTemplate: |
---------------------------
Server: @server@
Instance: @instance@
Timestamp: @time@
API: @api@
Module : @module@
Details: @details@
---------------------------
```
## 🔎 Usage
### `golog write`
Formats log and writes to a specified file.
```bash
golog write -i -o [-s]
```
* `-i, --input string`: Path to the input log file (required).
* `-o, --output string`: Path to the output file (required).
* `-s, --show`: Also print the output to the console.
### `golog show`
Formats log and prints the output to the console.
```bash
golog show -i
```
* `-i, --input string`: Path to the input log file (required).
## ⚙️ Development
Go v1.20 or later is recommended.
## 🤝 Contributing
Contributions are welcome! Please open an issue or submit a pull request.
## 📄 License
This project is licensed under the GNU Lesser General Public License v3.0. See the [LICENSE](LICENSE) file for details.