lightlog

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: MIT Imports: 10 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

This section is empty.

Types

type ConsoleLogger

type ConsoleLogger struct {
	*LoggerCore
}

ConsoleLogger is a struct that contains a pointer to LoggerCore

func NewConsoleLogger

func NewConsoleLogger(name string, level Level) *ConsoleLogger

NewConsoleLogger creates a new ConsoleLogger with the given name and level

type ConsoleTransport

type ConsoleTransport struct {
	*Transport
}

ConsoleTransport is a struct that represents a console transport

func NewConsoleTransport

func NewConsoleTransport(name string, level Level) *ConsoleTransport

NewConsoleTransport creates a new ConsoleTransport instance

func (*ConsoleTransport) Log

func (c *ConsoleTransport) Log(data *LogData)

Log logs the formatted data to the console

type FileLogger

type FileLogger struct {
	*LoggerCore
}

FileLogger is a struct that contains a pointer to LoggerCore

func NewFileLogger

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

NewFileLogger creates a new FileLogger with the given name, level, and filepath

type FileTransport

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

FileTransport is a struct that represents 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(data *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(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
}

Logger is a struct that contains a pointer to LoggerCore

func NewLogger

func NewLogger(options *LoggerOptions) *Logger

NewLogger creates a new Logger with the given options

type LoggerCore

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

func NewLoggerCore

func NewLoggerCore(name string, level Level) *LoggerCore

NewLoggerCore creates a new LoggerCore instance

func (*LoggerCore) AddTransport

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

AddTransport adds new transport to the logger

func (*LoggerCore) Close

func (l *LoggerCore) Close()

Close closes all transports

func (*LoggerCore) CloseAllTransport

func (l *LoggerCore) CloseAllTransport()

CloseAllTransport closes all transports

func (*LoggerCore) CloseTransport

func (l *LoggerCore) CloseTransport(name string)

CloseTransport closes the transport with the given name

func (*LoggerCore) Debug

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

Debug logs a message with the DEBUG level

func (*LoggerCore) DisableTransport

func (l *LoggerCore) DisableTransport(name string)

DisableTransport disables the transport with the given name

func (*LoggerCore) EnableTransport

func (l *LoggerCore) EnableTransport(name string)

EnableTransport enables the transport with the given name

func (*LoggerCore) Error

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

Error logs a message with the ERROR level

func (*LoggerCore) Fatal

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

Fatal logs a message with the FATAL level

func (*LoggerCore) Flush

func (l *LoggerCore) Flush()

Flush flushes all transports

func (*LoggerCore) FlushSync

func (l *LoggerCore) FlushSync()

FlushSync flushes all transports synchronously

func (*LoggerCore) GetTransport

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

GetTransport returns the transport with the given name

func (*LoggerCore) Info

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

Info logs a message with the INFO level

func (*LoggerCore) Log

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

Log logs a message with the given level and format

func (*LoggerCore) ReloadAllTransports

func (l *LoggerCore) ReloadAllTransports()

ReloadAllTransports reloads all transports

func (*LoggerCore) ReloadTransport

func (l *LoggerCore) ReloadTransport(name string)

ReloadTransport reloads the transport with the given name

func (*LoggerCore) RemoveTransport

func (l *LoggerCore) RemoveTransport(name string)

RemoveTransport removes the transport with the given name

func (*LoggerCore) Trace

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

Trace logs a message with the TRACE level

func (*LoggerCore) Warn

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

Warn logs a message with the WARN level

type LoggerOptions

type LoggerOptions struct {
	Name     string // Name of the logger
	Level    Level  // Level of the logger
	Filepath string // Filepath for the logger
}

LoggerOptions is a struct that contains options for creating a new Logger

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