Help us shape the future of Go! Take the 2026 Go Developer Survey

zenlogger

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2023 License: MIT Imports: 15 Imported by: 2

README ¶

Zenlogger 🤖

GoDoc

This library is built based on internal & personal needs. So, this lib can be not suit for your need.

This library support such as

  • Logging to file (including customize naming file)
  • Structured log to satisfy Gcloud logs structure
  • Customize structure & keys

Sample Output

###Sample structured output log

This is the sample of output output when beautifyJson set to true.

{
  "pid": "97732c84970d49a883193352f7da24f3",
  "severity": "Info",
  "timestamp": "2022-11-29T20:01:48+0700",
  "caller": "map_test.go:47",
  "message": {
    "title": "test map",
    "values": {
      "halo": {
        "2": "two",
        "developer": {
          "Age": 27,
          "Gender": "male",
          "IsMarried": false,
          "Name": "Zeni",
          "Relationship": "",
          "Supporter": [
            {
              "Age": 22,
              "Gender": "bunga",
              "IsMarried": true,
              "Name": "Mawar",
              "Relationship": "",
              "Supporter": []
            },
            {
              "Age": 20,
              "Gender": "warna",
              "IsMarried": false,
              "Name": "Hitam",
              "Relationship": "",
              "Supporter": []
            }
          ]
        },
        "four": 4,
        "one": 1,
        "three": "tilu"
      }
    }
  }
}

Installation 🚀

How to use: This is the sample code of zenlogger usage

package main

import (
	"github.com/zenmuharom/zenlogger"
)

type Person struct {
	Name         string
	Gender       string
	Age          int
	IsMarried    bool
	Relationship string
	Supporter    []Person
}

func Test_map(t *testing.T) {

	logger := NewZenlogger()
	logger.SetConfig(Config{BeautifyJson: true})

	person := Person{
		Name:      "Zeni",
		Gender:    "male",
		Age:       27,
		IsMarried: false,
		Supporter: []Person{
			{
				Name:      "Mawar",
				Age:       22,
				Gender:    "bunga",
				IsMarried: true,
			},
			{
				Name:      "Hitam",
				Age:       20,
				Gender:    "warna",
				IsMarried: false,
			},
		},
	}

	testMap := map[string]interface{}{
		"one":       "1",
		"2":         "two",
		"three":     "tilu",
		"four":      4,
		"developer": person,
	}
	logger.Info("test map", ZenField{Key: "halo", Value: testMap})
}


Config

This is the sample of config you can customize

	logger := zenlogger.NewZenlogger()
	config := zenlogger.Config{
		Pid: zenlogger.ZenConf{
			Label: "insertId",
		},
		Severity: zenlogger.Severity{
			Label:   "Level",
			Access:  "API",
			Info:    "THIS IS INFO",
			Debug:   "DEBUG",
			Warning: "Please Attention To This",
			Error:   "Fault",
			Query:   "DB",
		},
		Caller: zenlogger.Caller{
			Label: "trace_file",
			Level: 0,
		},
		Message: zenlogger.Message{
			Label: "pesan",
			Title: zenlogger.ZenConf{
				Label: "judul",
			},
			Values: zenlogger.ZenConf{
				Label: "isi",
			},
		},
		BeautifyJson: true,
	}
	logger.SetConfig(config)
Set log to file

You can set the log to make zenlogger write into file by adding:

	config := zenlogger.Config{}
	config.Output.Path = "logs"
	config.Output.Format = "20060102"
	config.BeautifyJson = true
	logger.SetConfig(config)

Zenlogger will automatically make directory logs (if not exists), and write into file with golang timed format.


Config Property 🤖
Property sub description
Pid process id is label of the key that differentiate with another process in your program
Severity Label The label of severity key
Severity Access The value of severity when you call zenlogger.Access()
Severity Info The value of severity when you call zenlogger.Info()
Severity Debug The value of severity when you call zenlogger.Debug()
Severity Warning The value of severity when you call zenlogger.Warning()
Severity Error The value of severity when you call zenlogger.Error()
Severity Query The value of severity when you call zenlogger.Query()
DateTime Label The label of datetime key when log written
DateTime Format The output datetime format, using standard GO datetime format
Caller Label The label of caller key
Caller Level The level of caller key. the default is 0
Message Label The label of message key
Message Title.Label The label of title key
Message Values.Label The label of values key
BeautifyJson The beautify config, true to make it beautify formatted, otherwise, set it to false
Output Path The output path of log files
Output Format The output file name using datetime GO format

You can set or try it in the link below here: 👉 GO Play. 👈

This library is released under: MIT License.

Documentation ¶

Index ¶

Constants ¶

View Source
const (
	DEFAULT_ACCESS  = "Access"
	DEFAULT_INFO    = "Info"
	DFEAULT_WARNING = "Warning"
	DEFAULT_ERROR   = "Error"
	DEFAULT_DEBUG   = "Debug"
	DEFAULT_QUERY   = "Query"
)

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Caller ¶

type Caller struct {
	Label string
	Level int
}

type Config ¶

type Config struct {
	Pid          ZenConf
	Severity     Severity
	DateTime     DateTime
	Caller       Caller
	Message      Message
	BeautifyJson bool
	Production   bool
	Output       Output
}

type DateTime ¶

type DateTime struct {
	Label  string
	Format string
}

type DefaultZenlogger ¶

type DefaultZenlogger struct {
	// contains filtered or unexported fields
}

func (*DefaultZenlogger) Access ¶

func (zenlog *DefaultZenlogger) Access(message string, fields ...ZenField)

func (*DefaultZenlogger) ConfCaller ¶

func (zenlog *DefaultZenlogger) ConfCaller(CallerConf Caller)

func (*DefaultZenlogger) ConfDateTime ¶

func (zenlog *DefaultZenlogger) ConfDateTime(DateTimeConf DateTime)

func (*DefaultZenlogger) ConfMessage ¶

func (zenlog *DefaultZenlogger) ConfMessage(MessageConf Message)

func (*DefaultZenlogger) ConfPid ¶

func (zenlog *DefaultZenlogger) ConfPid(pidConf ZenConf)

func (*DefaultZenlogger) ConfSeverity ¶

func (zenlog *DefaultZenlogger) ConfSeverity(ServerityConf Severity)

func (*DefaultZenlogger) Debug ¶

func (zenlog *DefaultZenlogger) Debug(message string, fields ...ZenField)

func (*DefaultZenlogger) Error ¶

func (zenlog *DefaultZenlogger) Error(message string, fields ...ZenField)

func (*DefaultZenlogger) GetConfig ¶

func (zenlog *DefaultZenlogger) GetConfig() Config

func (*DefaultZenlogger) GetPid ¶

func (zenlog *DefaultZenlogger) GetPid() string

func (*DefaultZenlogger) Info ¶

func (zenlog *DefaultZenlogger) Info(message string, fields ...ZenField)

func (*DefaultZenlogger) Query ¶

func (zenlog *DefaultZenlogger) Query(message string, fields ...ZenField)

func (*DefaultZenlogger) SetConfig ¶

func (zenlog *DefaultZenlogger) SetConfig(newConfig Config)

func (*DefaultZenlogger) Warning ¶

func (zenlog *DefaultZenlogger) Warning(message string, fields ...ZenField)

func (*DefaultZenlogger) WithPid ¶

func (zenlog *DefaultZenlogger) WithPid(pid string) Zenlogger

func (*DefaultZenlogger) Write ¶

func (zenlog *DefaultZenlogger) Write(Type string, msgStr string, fields ...ZenField)

type Message ¶

type Message struct {
	Label  string
	Title  ZenConf
	Values ZenConf
}

type Output ¶

type Output struct {
	Path       string
	Format     string
	BufferSize int64
}

type Severity ¶

type Severity struct {
	Label   string
	Access  string
	Info    string
	Debug   string
	Query   string
	Warning string
	Error   string
}

type ZenConf ¶

type ZenConf struct {
	Label string
}

type ZenField ¶

type ZenField struct {
	Key   string
	Value interface{}
}

type ZenLog ¶

type ZenLog []ZenField

func (ZenLog) MarshalJSON ¶

func (zmap ZenLog) MarshalJSON() ([]byte, error)

type ZenMessage ¶

type ZenMessage struct {
	Title  string                 `json:"title,omitempty"`
	Values map[string]interface{} `json:"values,omitempty"`
}

type Zenlogger ¶

type Zenlogger interface {
	SetConfig(config Config)
	GetConfig() Config
	WithPid(pid string) Zenlogger
	GetPid() string
	Access(message string, fields ...ZenField)
	Info(message string, fields ...ZenField)
	Query(message string, fields ...ZenField)
	Debug(message string, fields ...ZenField)
	Warning(message string, fields ...ZenField)
	Error(message string, fields ...ZenField)
}

func NewZenlogger ¶

func NewZenlogger(pid ...string) Zenlogger

Jump to

Keyboard shortcuts

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