lightlog

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 8 Imported by: 1

README

🚀🚀🚀 lightlog

a lightweight and high-performance logging library for Go.

Install

go get github.com/go-labx/lightlog

Usage

To use lightlog, you need to create a logger object. Here's an example of how to create a logger with default options:

options := &LoggerOptions{
    name:     "mylogger",
    level:    INFO,
    filepath: "/var/log/myapp.log",
}
logger := NewLogger(options)

Once you have a logger object, you can use it to log messages:

logger.Trace("This is a trace message: %s", message)
logger.Debug("This is a debug message: %s", message)
logger.Info("This is an info message: %s", message)
logger.Warn("This is a warning message: %s", message)
logger.Error("This is an error message: %s", message)
logger.Fatal("This is an fatal message: %s", message)

Transport

lightlog provides several transport options for logging messages. By default, lightlog creates a console and file transport for each logger. You can also add your own custom transports.

ConsoleTransport

The ConsoleTransport logs messages to the console. Here's an example of how to create a ConsoleTransport:

transport := NewConsoleTransport("myconsole", INFO)
FileTransport

The FileTransport logs messages to a file. Here's an example of how to create a FileTransport:

transport := NewFileTransport("myfile", INFO, "/var/log/myapp.log")
Custom Transports

You can also create your own custom transports by implementing the ITransport interface. Here's an example of a custom transport:

type MyTransport struct {
    *Transport
    // Add any custom fields here
}

func NewMyTransport(name string, level Level) *MyTransport {
    return &MyTransport{
        Transport: NewTransport(name, level),
        // Initialize any custom fields here
    }
}

func (t *MyTransport) Log(formattedData string, data *LogData) {
    // Implement your logging logic here
}

Log Levels

lightlog provides several log levels that you can use to filter messages. The available log levels are:

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

To set the log level of a logger, you can use the level field of the LoggerOptions struct.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetIPAddresses

func GetIPAddresses() (ipv4 string, ipv6 string)

GetIPAddresses This function returns the IP addresses of the current machine.

func GetLocation

func GetLocation() string

GetLocation returns the file name and line number of the caller of the method that called

Types

type ConsoleLogger

type ConsoleLogger struct {
	*LoggerCore
}

func NewConsoleLogger

func NewConsoleLogger(name string, level Level) *ConsoleLogger

type ConsoleTransport

type ConsoleTransport struct {
	*Transport
}

func NewConsoleTransport

func NewConsoleTransport(name string, level Level) *ConsoleTransport

func (*ConsoleTransport) Log

func (c *ConsoleTransport) Log(formattedData string, _ *LogData)

type FileLogger

type FileLogger struct {
	*LoggerCore
}

func NewFileLogger

func NewFileLogger(name string, level Level, filepath string) *FileLogger

type FileTransport

type FileTransport struct {
	*Transport
	// contains filtered or unexported fields
}

FileTransport is a struct that represents a transport for logging to a file.

func NewFileTransport

func NewFileTransport(name string, level Level, filepath string) *FileTransport

NewFileTransport is a constructor function that creates a new instance of FileTransport.

func (*FileTransport) Close

func (f *FileTransport) Close()

Close is a method that flushes the writer's buffer to the file and closes the file.

func (*FileTransport) Flush

func (f *FileTransport) Flush()

Flush is a method that flushes the writer's buffer to the file.

func (*FileTransport) FlushSync

func (f *FileTransport) FlushSync()

FlushSync is a method that synchronously flushes the writer's buffer to the file.

func (*FileTransport) Log

func (f *FileTransport) Log(formattedData string, _ *LogData)

Log is a method that writes formattedData to the file.

func (*FileTransport) Reload

func (f *FileTransport) Reload()

Reload is a method that flushes the writer's buffer to the file, reloads the file, and closes the old file.

type ITransport

type ITransport interface {
	Enable()
	Disable()
	ShouldLog(level Level) bool
	Log(formattedData string, data *LogData)
	Flush()
	FlushSync()
	Reload()
	Close()
}

ITransport Define an interface called ITransport with several methods

type Level

type Level int
const (
	TRACE Level = iota
	DEBUG
	INFO
	WARN
	ERROR
	FATAL
)

type LogData

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

type Logger

type Logger struct {
	*LoggerCore
}

func NewLogger

func NewLogger(options *LoggerOptions) *Logger

type LoggerCore

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

func NewLoggerCore

func NewLoggerCore(name string, level Level) *LoggerCore

func (*LoggerCore) AddTransport

func (l *LoggerCore) AddTransport(name string, transport ITransport)

func (*LoggerCore) Close

func (l *LoggerCore) Close()

func (*LoggerCore) CloseAllTransport

func (l *LoggerCore) CloseAllTransport()

func (*LoggerCore) CloseTransport

func (l *LoggerCore) CloseTransport(name string)

func (*LoggerCore) Debug

func (l *LoggerCore) Debug(format string, v ...interface{})

func (*LoggerCore) DisableTransport

func (l *LoggerCore) DisableTransport(name string)

func (*LoggerCore) EnableTransport

func (l *LoggerCore) EnableTransport(name string)

func (*LoggerCore) Error

func (l *LoggerCore) Error(format string, v ...interface{})

func (*LoggerCore) Fatal

func (l *LoggerCore) Fatal(format string, v ...interface{})

func (*LoggerCore) Flush

func (l *LoggerCore) Flush()

func (*LoggerCore) FlushSync

func (l *LoggerCore) FlushSync()

func (*LoggerCore) GetTransport

func (l *LoggerCore) GetTransport(name string) ITransport

func (*LoggerCore) Info

func (l *LoggerCore) Info(format string, v ...interface{})

func (*LoggerCore) Log

func (l *LoggerCore) Log(level Level, format string, v ...interface{})

func (*LoggerCore) ReloadAllTransports

func (l *LoggerCore) ReloadAllTransports()

func (*LoggerCore) ReloadTransport

func (l *LoggerCore) ReloadTransport(name string)

func (*LoggerCore) RemoveTransport

func (l *LoggerCore) RemoveTransport(name string)

func (*LoggerCore) Trace

func (l *LoggerCore) Trace(format string, v ...interface{})

func (*LoggerCore) Warn

func (l *LoggerCore) Warn(format string, v ...interface{})

type LoggerOptions

type LoggerOptions struct {
	Name     string
	Level    Level
	Filepath string
}

type Transport

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

func NewTransport

func NewTransport(name string, level Level) *Transport

NewTransport Create a new Transport object

func (*Transport) Close

func (t *Transport) Close()

Close the transport

func (*Transport) Disable

func (t *Transport) Disable()

Disable the transport

func (*Transport) Enable

func (t *Transport) Enable()

Enable the transport

func (*Transport) Flush

func (t *Transport) Flush()

Flush log data

func (*Transport) FlushSync

func (t *Transport) FlushSync()

FlushSync flush log data synchronously

func (*Transport) Reload

func (t *Transport) Reload()

Reload the transport

func (*Transport) ShouldLog

func (t *Transport) ShouldLog(level Level) bool

ShouldLog Check if the transport should log a message with the given level

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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