mlogger

package module
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 11 Imported by: 0

README

MLogger

Logger with additional contact points and message templates based on zerolog for golang 1.20.*

Contact points implementations:

  1. Matrix
  2. Telegram

Parse Modes

  1. Markdown

Usage

Install

go get -u github.com/Alp4ka/mlogger

Example

package main

import (
  "context"
  "fmt"
  "github.com/Alp4ka/mlogger/contactpoints/matrix"
  "github.com/Alp4ka/mlogger/field"
  "github.com/Alp4ka/mlogger/misc"
  "github.com/Alp4ka/mlogger/templates"
  "github.com/Alp4ka/mlogger"
  "testing"
  "time"
)

const t = `
My test template! 

**Time:**
*{{ .LogTime.Format "Jan 02, 2006 15:04:05 UTC" }}*

**Level:**
*{{ .LogLevel }}*

**Origin:**
*{{ .LogSource }}*
{{ if len .LogContextFields }}
**Context Fields:**
{{- end }}
{{ range .LogContextFields }}
{{ .Key }}: {{ .Value }}
{{ end }}
{{ if len .LogFields }}
**Fields:**
{{- end }}
{{ range .LogFields }}
{{ .Key }}: {{ .Value }}
{{ end }}
**Message:**
*{{ .LogMessage }}*`

func main() {
	cp := matrix.NewContactPoint(matrix.Config{
		Level:         misc.LevelInfo,
		HomeserverURL: "HOMESERVER_URL",
		UserID:        "USER_ID",
		RoomID:        "ROOM_ID",
		AccessToken:   "ACCESS_TOKEN",
	})

	cfg := Config{Level: misc.LevelInfo,
		Template: templates.Config{Pattern: t, Use: true}}

	f1 := field.Bool("test_bool", true)
	ctx := field.WithContextFields(context.Background(), f1)
	logger, err := mlogger.NewProduction(ctx, cfg, cp)
	if err != nil {
		panic(err.Error())
	}

	mlogger.ReplaceGlobals(logger)
	mlogger.L(ctx).Info(
		"test message",
		field.Int("test_int", 123),
		field.String("test_string", "hello world!"),
		field.Error(fmt.Errorf("test_error")))

	time.Sleep(time.Second)
}

Output

alt console output

alt matrix output

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReplaceGlobals

func ReplaceGlobals(logger *MainLogger)

Types

type Config

type Config struct {
	Level        misc.Level
	Template     templates.Config
	JSONSecurity jsonsecurity.Config
}

type MainLogger

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

func L

func L(optionalCtx ...context.Context) *MainLogger

func NewProduction

func NewProduction(ctx context.Context, cfg Config, contacts ...contactpoints.ContactPoint) (*MainLogger, error)

func (*MainLogger) Debug

func (l *MainLogger) Debug(msg string, fields ...field.Field)

func (*MainLogger) Error

func (l *MainLogger) Error(msg string, fields ...field.Field)

func (*MainLogger) Fatal

func (l *MainLogger) Fatal(msg string, fields ...field.Field)

func (*MainLogger) Info

func (l *MainLogger) Info(msg string, fields ...field.Field)

func (*MainLogger) Panic

func (l *MainLogger) Panic(msg string, fields ...field.Field)

func (*MainLogger) Warn

func (l *MainLogger) Warn(msg string, fields ...field.Field)

Directories

Path Synopsis
tg

Jump to

Keyboard shortcuts

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