logt

package module
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 9 Imported by: 1

README

Logt

Color logger for golang

Usage

func main() {
	// Create a new logger
	// Token: Telegram Bot token
    	// UserID: Telegram User ID
	// Logo: Set to true if you want to show logo
    	// NameSpace: Namespace of your project
    	// If you want use Telegrambot set your token and user id, else set to empty
	l := NewLog(&Log{
		Token:     "1234567890:ABCDEFGHIowX1FpcRML5frzcAylXEeEMB9I",
		UserID:    265943548,
		Logo:      false,
		NameSpace: "test",
	})

	// Initialize writer logger
    	// functionName: Function name
    	// saveFile: If you set to true, the file will be saved, 
	w := l.NewWriter("item.Create()", true)
	defer w.Close()

	// Write a message
	w.Data("Hello world")
	w.Debug("Hello world")
	w.Error(errors.New("hello world"))
	w.Info("Hello world")
	w.Msg("Hello world")
	w.Succes("Hello world")
	w.Warning("Hello world")

	user := User{
		Firstname: "Jahongir",
		Lastname:  "Temirov",
		Email:     "realjakhongir@gmail.com",
		Password:  "12345678",
		Address: Address{
			Country: "Uzbekistan",
			City:    "Tashkent",
		},
	}

    	// Use w.Send method to send message to telegram
	w.Send([]any{user, "Hello world!", 123})

}

type User struct {
	Firstname string  `json:"first_name"`
	Lastname  string  `json:"last_name"`
	Email     string  `json:"email"`
	Password  string  `json:"password"`
	Address   Address `json:"address"`
}

type Address struct {
	Country string `json:"country"`
	City    string `json:"city"`
}

Output

console

WithContext

func main() {
    newLogger := logt.NewLog(&Log{NameSpace: "test"})
    
	ctx := context.Background()
    ctx = newLogger.SetContext(ctx,"data", 12345)
    
	s := NewServiceA(newLogger)
}

type myService struct {
	logger logt.ILog
}

func NewMyService(logger logt.ILog) *myService {
    return &myService{
        logger: logger,
    }
}

func (s *myService) myFunction(ctx context.Context) {
    w := s.logger.NewWriter("myService.myFunction", false) 

	w.Succes("log from myService") // not output to console from context
    w.FromContext(ctx).Succes("log from myService") // it will output to console from console
}

WithContext

Telegram Bot

[
  {
    "first_name": "Jahongir",
    "last_name": "Temirov",
    "email": "realjakhongir@gmail.com",
    "password": "12345678",
    "address": {
      "country": "Uzbekistan",
      "city": "Tashkent"
    }
  },
  "Hello world!",
  123
]

File

Filename:

2023-09-10 14:42:42-item.Create().txt

Each function log can be saved as a separate file when the function is finished.

When outputting the file to the console, it will output with the previous colors.

file

TODO

  • Add writers with format
  • ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address added in v1.1.7

type Address struct {
	Country string `json:"country"`
	City    string `json:"city"`
}

type ILog added in v1.1.3

type ILog interface {
	NewWriter(functionName string, saveFile bool) IWriter
	SetContext(ctx context.Context, fields ...any) context.Context
}

func NewLog added in v1.1.3

func NewLog(log *Log) ILog

NewLog create new log data namespace - name of namespace intro - show intro

Example:

logt.NewLog(log *Log)

type Log struct {
 // Telegram bot token
	Token     string = "123456789:ABCDEFGHi-ijklmnop-QRSTUVXYZ"
 // Telegram user ID
	UserID     int64 = 9876543210
	Logo      bool
	NameSpace string
}

logt.NewLog("namespace", false)

type IWriter added in v1.1.3

type IWriter interface {
	Close()
	Data(str ...any)
	Debug(str ...any)
	Error(str ...any)
	Info(str ...any)
	Succes(str ...any)
	Warning(str ...any)
	Msg(str ...any)
	Write(str ...any)
	Send(str ...any)
	FromContext(ctx context.Context) IWriter
}

type Log added in v1.1.3

type Log struct {
	Token     string
	UserID    int64
	NameSpace string
}

Log is a struct for creating a new log implementation

type User added in v1.1.7

type User struct {
	Firstname string  `json:"first_name"`
	Lastname  string  `json:"last_name"`
	Email     string  `json:"email"`
	Password  string  `json:"password"`
	Address   Address `json:"address"`
}

Jump to

Keyboard shortcuts

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