config

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const PathDefault = "default"

PathDefault is a fake path to the default config.

Variables

This section is empty.

Functions

func GetDefaultCustomLevelMapping added in v0.3.0

func GetDefaultCustomLevelMapping() map[string]string

GetDefaultCustomLevelMapping returns the custom mapping of levels.

Types

type Config

type Config struct {
	// Path to the config.
	Path string `json:"-"`

	Fields []Field `json:"fields" validate:"min=1"`

	CustomLevelMapping map[string]string `json:"customLevelMapping"`

	// The number of rows to prerender.
	PrerenderRows int `json:"prerenderRows"`
	// ReloadThreshold is the minimum duration between reloading rows.
	ReloadThreshold time.Duration `json:"reloadThreshold" validate:"min=100ms"`
	// MaxFileSizeBytes is the maximum size of the file to load.
	MaxFileSizeBytes int64 `json:"maxFileSizeBytes" validate:"min=1"`

	// StdinReadTimeout is the timeout of reading from the standart input.
	StdinReadTimeout time.Duration `json:"stdinReadTimeout" validate:"min=100ms"`
}

Config contains application customization settings.

func GetDefaultConfig

func GetDefaultConfig() *Config

GetDefaultConfig returns the configuration with default values.

Example
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"

	"github.com/hedhyw/json-log-viewer/internal/pkg/config"
)

func main() {
	cfg := config.GetDefaultConfig()

	var buf bytes.Buffer

	jsonEncoder := json.NewEncoder(&buf)
	jsonEncoder.SetIndent("", "\t")

	if err := jsonEncoder.Encode(&cfg); err != nil {
		log.Fatal(err)
	}

	fmt.Println(buf.String())
}
Output:

{
	"fields": [
		{
			"title": "Time",
			"kind": "numerictime",
			"ref": [
				"$.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
		}
	],
	"customLevelMapping": {
		"10": "trace",
		"20": "debug",
		"30": "info",
		"40": "warn",
		"50": "error",
		"60": "fatal"
	},
	"prerenderRows": 100,
	"reloadThreshold": 1000000000,
	"maxFileSizeBytes": 1073741824,
	"stdinReadTimeout": 1000000000
}

func Read

func Read(paths ...string) (*Config, error)

Read config from the given paths. From higher priority to lower priority.

type Field

type Field struct {
	Title      string    `json:"title" validate:"required,min=1,max=32"`
	Kind       FieldKind `json:"kind" validate:"required,oneof=time message numerictime secondtime millitime microtime level any"`
	References []string  `json:"ref" validate:"min=1,dive,required"`
	Width      int       `json:"width" validate:"min=0"`
}

Field customization.

type FieldKind

type FieldKind string

FieldKind describes the type of the log field.

const (
	FieldKindTime        FieldKind = "time"
	FieldKindNumericTime FieldKind = "numerictime"
	FieldKindSecondTime  FieldKind = "secondtime"
	FieldKindMilliTime   FieldKind = "millitime"
	FieldKindMicroTime   FieldKind = "microtime"
	FieldKindMessage     FieldKind = "message"
	FieldKindLevel       FieldKind = "level"
	FieldKindAny         FieldKind = "any"
)

Possible kinds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL