loginjector

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 17 Imported by: 0

README

LogInjector

Description:

LogInjector is a lightweight logging utility written in Go designed to provide comprehensive and efficient log management applications.

Features:

  • Flexible Log Levels: Easily categorize logs with predefined levels such as DEBUG, INFO, WARN, ERROR, and FATAL.
  • Structured Logging: Support for structured logging, allowing you to include context-rich information in your logs.
  • Output Options: Write logs to various outputs including console, files, and remote servers.
  • Log Rotation: Automatically manage log file sizes and rotation, ensuring your log storage remains efficient.
  • Custom Formats: Define custom log formats to suit your application's needs.
  • Performance Optimization: Built with Go's concurrency model for high performance and minimal overhead.
  • Extensible: Plugin architecture for adding custom log processors and outputs.
  • JSON Support: Native support for logging in JSON format, perfect for integrating with log aggregation and analysis tools.
  • HTTP Middleware: Easily integrate LogInjector with your Go web applications using the provided HTTP middleware.
  • Error Handling: Built-in error handling and recovery mechanisms to ensure your application remains stable.

Installation:

go get github.com/prorochestvo/loginjector

Usage:

package main

import (
	"bytes"
	"github.com/prorochestvo/loginjector"
)

const (
	LogLevelDebug loginjector.LogLevel = 1
	LogLevelInfo  loginjector.LogLevel = 2
	LogLevelWarn  loginjector.LogLevel = 3
	LogLevelError loginjector.LogLevel = 4
)

func main() {
	b := bytes.NewBufferString("")

	l, err := loginjector.NewLogger(LogLevelWarn, b)
	if err != nil {
		panic(err)
	}

	_, err = l.WriteLog(LogLevelInfo, []byte("Hello, World!"))
	if err != nil {
		panic(err)
	}

	_, err = l.WriteLog(LogLevelWarn, []byte("log message: warning"))
	if err != nil {
		panic(err)
	}

	println(b.String())
}

Contributing:

We welcome contributions from the community! Please read our contributing guidelines and submit your pull requests or report issues on our GitHub repository.

License:

LogInjector is released under the MIT License. See the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseOrIgnore

func CloseOrIgnore(closer io.Closer)

CloseOrIgnore closes the closer and ignores the error if there is one.

func CloseOrLog

func CloseOrLog(closer io.Closer)

CloseOrLog closes the closer and logs the error if there is one.

func CloseOrPanic

func CloseOrPanic(closer io.Closer)

CloseOrPanic closes the closer and panics if there is an error.

func CloseOrPrintLn

func CloseOrPrintLn(closer io.Closer)

CloseOrPrintLn closes the closer and prints the error if there is one.

func CyclicOverwritingFilesHandler

func CyclicOverwritingFilesHandler(folder, fileNamePrefix string, maxFileCapacity uint32, maxFilesInFolder int) io.Writer

CyclicOverwritingFilesHandler save messages to files by number. The file name is generated by fileNamePrefix and index. The folder is the directory where the files are saved. The maxFileCapacity is the maximum size of the file in bytes. The maxFilesInFolder is the maximum number of files in the folder.

func FileByFormatHandler

func FileByFormatHandler(folder string, maxFilesInFolder int, fileNameGenerator func() string) io.Writer

FileByFormatHandler save messages to files by format. The file name is generated by fileNameGenerator. The folder is the directory where the files are saved. The maxFilesInFolder is the maximum number of files in the folder.

func JsonDecode

func JsonDecode(r io.Reader, v any) error

func JsonDecodeAndClose

func JsonDecodeAndClose(r io.ReadCloser, v any) (err error)

func JsonDecodeAndCloseEx

func JsonDecodeAndCloseEx(r io.ReadCloser, v any) (raw []byte, err error)

func JsonDecodeEx

func JsonDecodeEx(r io.Reader, v any) ([]byte, error)

func JsonEncode

func JsonEncode(w io.Writer, v any, indent ...int) error

func JsonEncodeAndClose

func JsonEncodeAndClose(w io.WriteCloser, v any, indent ...int) (err error)

func JsonEncodeAndCloseEx

func JsonEncodeAndCloseEx(w io.WriteCloser, v any, indent ...int) (raw []byte, err error)

func JsonEncodeEx

func JsonEncodeEx(w io.Writer, v any, indent ...int) ([]byte, error)

func NewHttpPayloadHandler

func NewHttpPayloadHandler(logger *Logger, level LogLevel, nextFunc http.HandlerFunc) (http.HandlerFunc, error)

NewHttpPayloadHandler - create new http middleware handler. This middleware logs the request and response payloads. The log is written to the logger.

func PrintHandler

func PrintHandler() io.Writer

PrintHandler prints the message to the console

func SilenceHandler

func SilenceHandler() io.Writer

SilenceHandler does nothing that is ignores any messages and returns the length of the message without any errors

func TelegramHandler

func TelegramHandler(botToken, chatID, fileName string, labels ...string) io.Writer

Types

type HookID

type HookID string

HookID is a unique identifier for a hook

type HttpError

type HttpError interface {
	StatusCode() int
	error
}

HttpError is an error that provides an HTTP status code.

func NewHttpError

func NewHttpError(code int) HttpError

NewHttpError creates a new HttpError with the given status code.

type LogLevel

type LogLevel int

LogLevel is a log level

type Logger

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

Logger is describing the logger structure and its methods

func NewLogger

func NewLogger(min LogLevel, handlers ...io.Writer) (*Logger, error)

NewLogger creates a new logger with the given minimum log level and handlers If no handlers are provided, a default print handler is added

func (*Logger) Fatal

func (l *Logger) Fatal(level LogLevel, args ...any)

Fatal writes a log message and exits the program

func (*Logger) Fatalf

func (l *Logger) Fatalf(level LogLevel, format string, args ...any)

Fatalf writes a formatted log message and exits the program

func (*Logger) Hook

func (l *Logger) Hook(writer io.Writer, level LogLevel, additional ...LogLevel) HookID

Hook adds a hook to the logger

func (*Logger) JoinAs

func (l *Logger) JoinAs(logLevel LogLevel, outputs ...func(w io.Writer))

JoinAs joins the logger as a writer to the given outputs

func (*Logger) Print

func (l *Logger) Print(level LogLevel, args ...any)

Print writes a log message

func (*Logger) Printf

func (l *Logger) Printf(level LogLevel, format string, args ...any)

Printf writes a formatted log message

func (*Logger) SetMinLevel

func (l *Logger) SetMinLevel(level LogLevel)

SetMinLevel sets the minimum log level

func (*Logger) Unhook

func (l *Logger) Unhook(id HookID)

Unhook removes a hook from the logger

func (*Logger) Write

func (l *Logger) Write(message []byte) (int, error)

Write writes a log message with the minimum log level

func (*Logger) WriteLog

func (l *Logger) WriteLog(level LogLevel, message []byte) (int, error)

WriteLog writes a log message

func (*Logger) WriterAs

func (l *Logger) WriterAs(level LogLevel) io.Writer

WriterAs returns a writer that writes to the logger as the given log level

type TraceError

type TraceError interface {
	Line() string
	error
}

TraceError is an error that provides a stack trace.

func NewTraceError

func NewTraceError() TraceError

NewTraceError creates a new TraceError with the current stack trace.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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