config

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2023 License: MIT Imports: 6 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

This section is empty.

Types

type Config

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

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

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
		}
	]
}

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