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

zenlogger

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 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

	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:  "OH MY GOD THIS IS ERROR",
			Query:  "QUERY",
		},
		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)

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.


You can set or try it in the link below here: GO Playground.

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