config

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const DefaultTimeFormat = time.RFC3339

DefaultTimeFormat is a time format used in formatting timestamps by default.

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 ByteSize added in v1.1.0

type ByteSize int

ByteSize supports decoding from byte count or number with unit.

Example: 1k, 1.5m, 1g, 1t, 1p.

func (*ByteSize) UnmarshalJSON added in v1.1.0

func (s *ByteSize) UnmarshalJSON(text []byte) error

UnmarshalJSON implements json.Unmarshaler.

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"`

	// MaxFileSizeBytes is the maximum size of the file to load.
	MaxFileSizeBytes ByteSize `json:"maxFileSizeBytes" 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("", "  ")

	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,
      "time_format": "2006-01-02T15:04:05Z07:00"
    },
    {
      "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"
  },
  "maxFileSizeBytes": 2000000000
}

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"`
	TimeFormat *string   `json:"time_format,omitempty"`
}

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