README
¶
Hermes
Hermes was the messenger of the gods in Greek mythology, known for his cunning, speed, and versatility. He was the son of Zeus and the nymph Maia, and he was born in a cave on Mount Cyllene in Arcadia.
This repository provides a logger service that implements the ZeroLog repository and supports three types of logger writers:
- DB writer: logs messages to a database (there is an influxDB implementation integrated too)
- CLI writer: logs messages to the console (stdout)
- File writer: logs messages to a file
Installation
To use the logger service, you'll need to install the package using Go modules:
go get github.com/mateenbagheri/hermes
Usage
To use the logger service, you'll first need to create a logger instance using one of the available writer types:
import "github.com/mateenbagheri/hermes"
logger := hermes.New(hermes.ZeroLoggerType).
WithLevel(hermes.DebugLevel).
WithServiceName("test").
WithWriters(
// Create a new logger instance with a CLI writer
hermes.ConsoleWriterType,
// Create a new logger instance with a file writer
hermes.FileWriterType,
).
Build().
WithScope("main")
logger.Debug("this is a test")
Once you have a logger instance, you can use it to log messages at different levels:
logger.Trace("This is a trace message")
logger.Debug("This is a debug message")
logger.Info("This is an info message")
logger.Warn("This is a warning message")
logger.Error("This is an error message")
logger.Fatal("This is a fatal message")
logger.Panic("This is a panic message")
You can also log messages with additional key-value pairs:
logger.Tracev("This is a trace message with additional data", "key1", "value1", "key2", "value2")
Configuration
In case you want to use the InfluxDB as the database writer, you will need to add .WithInfluxConfig()
to the .New()
method. for configuring influx, you will need the following configs:
import "github.com/mateenbagheri/hermes"
logger := hermes.New(hermes.ZeroLoggerType).
WithInfluxConfig(
os.Getenv("INFLUX_ADDRESS"),
os.Getenv("INFLUX_TOKEN"),
os.Getenv("INFLUX_ORGANIZATION"),
os.Getenv("INFLUX_BUCKET"),
).
WithLevel(hermes.DebugLevel).
WithServiceName("test").
WithWriters(
// Create a new logger instance with a CLI writer
hermes.ConsoleWriterType,
// Create a new logger instance with a file writer
hermes.FileWriterType,
// Create a new logger instance with an InfluxDB writer
hermes.DatabaseWriterType,
).
Build().
WithScope("main")
logger.Debug("this is a test")
Contributing
Thank you for considering contributing to Hermes! Please follow these guidelines to ensure your contribution is properly considered:
- Fork the repository and create your branch from main.
- Make your changes, and add new tests as appropriate.
- Run go test to make sure all tests pass.
- Format your code with gofmt -s.
- Ensure your code passes golint.
- Commit your changes and push your branch to your forked repository. Create a pull request to the main Hermes repository.
If you would like to report a bug or suggest a new feature, please feel free to open a GitHub issue in the Hermes repository. We welcome all feedback and suggestions!
Documentation
¶
Index ¶
- type DebugLogger
- type ErrorLogger
- type Factory
- func (f *Factory) Build() Logger
- func (f *Factory) WithInfluxConfig(address, authToken, organization, bucket string) *Factory
- func (f *Factory) WithLevel(level Level) *Factory
- func (f *Factory) WithServiceName(name string) *Factory
- func (f *Factory) WithStackError() *Factory
- func (f *Factory) WithWriters(writers ...WriterType) *Factory
- type FatalLogger
- type InfoLogger
- type Level
- type Logger
- type PanicLogger
- type TraceLogger
- type Type
- type WarnLogger
- type WriterType
- type ZeroLogger
- func (z *ZeroLogger) Debug(message string)
- func (z *ZeroLogger) Debugf(format string, args ...any)
- func (z *ZeroLogger) Debugv(message string, keyValue ...any)
- func (z *ZeroLogger) Err(err error)
- func (z *ZeroLogger) Error(message string)
- func (z *ZeroLogger) Errorf(format string, args ...any)
- func (z *ZeroLogger) Errorv(message string, keyValue ...any)
- func (z *ZeroLogger) Fatal(message string)
- func (z *ZeroLogger) Fatalf(format string, args ...any)
- func (z *ZeroLogger) Fatalv(message string, keyValue ...any)
- func (z *ZeroLogger) Info(message string)
- func (z *ZeroLogger) Infof(format string, args ...any)
- func (z *ZeroLogger) Infov(message string, keyValue ...any)
- func (z *ZeroLogger) Panic(message string)
- func (z *ZeroLogger) Panicf(format string, args ...any)
- func (z *ZeroLogger) Panicv(message string, keyValue ...any)
- func (z *ZeroLogger) Trace(message string)
- func (z *ZeroLogger) Tracef(format string, args ...any)
- func (z *ZeroLogger) Tracev(message string, keyValue ...any)
- func (z *ZeroLogger) Warn(message string)
- func (z *ZeroLogger) Warnf(format string, args ...any)
- func (z *ZeroLogger) Warnv(message string, keyValue ...any)
- func (z *ZeroLogger) WithScope(scope string) Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DebugLogger ¶
type DebugLogger interface { Debug(message string) Debugf(format string, args ...any) // Debugv logs debug level logs with additional context data in the form of key-value pairs Debugv(message string, KeyValue ...any) }
DebugLogger defines logging methods for debug level logs
type ErrorLogger ¶
type ErrorLogger interface { Err(err error) Error(message string) Errorf(format string, args ...any) // Errorv logs error level logs with additional context data in the form of key-value pairs Errorv(message string, keyValue ...any) }
ErrorLogger defines logging methods for error level logs
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
Factory is a struct that holds the type, service name, and zerolog.Logger.
func (*Factory) WithInfluxConfig ¶
func (*Factory) WithLevel ¶
WithLevel is a function that sets the logging level for the Factory. Currently since we only have ZeroLog implemented, other values are considered to be invalid.
func (*Factory) WithServiceName ¶
WithServiceName is a function that sets the service name for the Factory.
func (*Factory) WithStackError ¶
WithStackError is a function that sets the stack error for the Factory.
func (*Factory) WithWriters ¶
func (f *Factory) WithWriters(writers ...WriterType) *Factory
WithWriters is a function that sets the writers for the Factory.
type FatalLogger ¶
type FatalLogger interface { Fatal(message string) Fatalf(format string, args ...any) // Fatalv logs fatal level logs with additional context data in the form of key-value pairs Fatalv(message string, keyValue ...any) }
FatalLogger defines logging methods for fatal level logs
type InfoLogger ¶
type InfoLogger interface { Info(message string) Infof(format string, args ...any) // Infov logs info level logs with additional context data in the form of key-value pairs Infov(message string, keyValue ...any) }
InfoLogger defines logging methods for info level logs
type Level ¶
type Level int8
Level represents different logging levels that can be used for filtering logs
const ( // DebugLevel is the logging level for debug logs DebugLevel Level = iota // InfoLevel is the logging level for informational logs InfoLevel // WarnLevel is the logging level for warning logs WarnLevel // ErrorLevel is the logging level for error logs ErrorLevel // FatalLevel is the logging level for fatal logs FatalLevel // PanicLevel is the logging level for panic logs PanicLevel // TraceLevel is a special logging level to log trace level logs TraceLevel Level = -1 )
type Logger ¶
type Logger interface { TraceLogger DebugLogger InfoLogger WarnLogger ErrorLogger FatalLogger PanicLogger // WithScope returns a new Logger instance with a specified scope added to the context WithScope(scope string) Logger }
Logger interface defines the logging methods that are common across different loggers
type PanicLogger ¶
type PanicLogger interface { Panic(message string) Panicf(format string, args ...any) // Panicv logs panic level logs with additional context data in the form of key-value pairs Panicv(message string, keyValue ...any) }
PanicLogger defines logging methods for panic level logs
type TraceLogger ¶
type TraceLogger interface { Trace(message string) Tracef(format string, args ...any) // Tracev logs trace level logs with additional context data in the form of key-value pairs Tracev(message string, keyValue ...any) }
TraceLogger defines logging methods for trace level logs
type WarnLogger ¶
type WarnLogger interface { Warn(message string) Warnf(format string, args ...any) // Warnv logs warn level logs with additional context data in the form of key-value pairs Warnv(message string, keyValue ...any) }
WarnLogger defines logging methods for warn level logs
type WriterType ¶
type WriterType uint8
WriterType represents the type of writer to use for the logger.
const ( // ConsoleWriterType represents a console writer. ConsoleWriterType WriterType = iota // FileWriterType represents a file writer. FileWriterType // DatabaseWriterType represents a database writer. DatabaseWriterType )
type ZeroLogger ¶
ZeroLogger is a wrapper around a zerolog Logger instance with additional service and scope fields
func (*ZeroLogger) Debug ¶
func (z *ZeroLogger) Debug(message string)
Debug logs a debug-level message with the provided message string
func (*ZeroLogger) Debugf ¶
func (z *ZeroLogger) Debugf(format string, args ...any)
Debugf logs a debug-level message with the provided format string and arguments
func (*ZeroLogger) Debugv ¶
func (z *ZeroLogger) Debugv(message string, keyValue ...any)
Debugv logs a debug-level message with the provided message string and key-value pairs
func (*ZeroLogger) Err ¶
func (z *ZeroLogger) Err(err error)
Err logs an error and sends the log message.
func (*ZeroLogger) Errorf ¶
func (z *ZeroLogger) Errorf(format string, args ...any)
Errorf logs a formatted error message.
func (*ZeroLogger) Errorv ¶
func (z *ZeroLogger) Errorv(message string, keyValue ...any)
Errorv logs an error message with additional key-value pairs.
func (*ZeroLogger) Fatal ¶
func (z *ZeroLogger) Fatal(message string)
Fatal logs a fatal error message.
func (*ZeroLogger) Fatalf ¶
func (z *ZeroLogger) Fatalf(format string, args ...any)
Fatalf logs a formatted fatal error message.
func (*ZeroLogger) Fatalv ¶
func (z *ZeroLogger) Fatalv(message string, keyValue ...any)
Fatalv logs a fatal error message with additional key-value pairs.
func (*ZeroLogger) Info ¶
func (z *ZeroLogger) Info(message string)
Info logs an info-level message with the provided message string
func (*ZeroLogger) Infof ¶
func (z *ZeroLogger) Infof(format string, args ...any)
Infof logs an info-level message with the provided format string and arguments
func (*ZeroLogger) Infov ¶
func (z *ZeroLogger) Infov(message string, keyValue ...any)
Infov logs an info-level message with the provided message string and key-value pairs
func (*ZeroLogger) Panicf ¶
func (z *ZeroLogger) Panicf(format string, args ...any)
Panicf logs a formatted panic message.
func (*ZeroLogger) Panicv ¶
func (z *ZeroLogger) Panicv(message string, keyValue ...any)
Panicv logs a panic message with additional key-value pairs.
func (*ZeroLogger) Trace ¶
func (z *ZeroLogger) Trace(message string)
Trace logs a trace-level message with the provided message string
func (*ZeroLogger) Tracef ¶
func (z *ZeroLogger) Tracef(format string, args ...any)
Tracef logs a trace-level message with the provided format string and arguments
func (*ZeroLogger) Tracev ¶
func (z *ZeroLogger) Tracev(message string, keyValue ...any)
Tracev logs a trace-level message with the provided message string and key-value pairs
func (*ZeroLogger) Warn ¶
func (z *ZeroLogger) Warn(message string)
Warn logs a warning-level message with the provided message string
func (*ZeroLogger) Warnf ¶
func (z *ZeroLogger) Warnf(format string, args ...any)
Warnf logs a warning-level message with the provided format string and arguments
func (*ZeroLogger) Warnv ¶
func (z *ZeroLogger) Warnv(message string, keyValue ...any)
Warnv logs a warning-level message with the provided message string and key-value pairs
func (*ZeroLogger) WithScope ¶
func (z *ZeroLogger) WithScope(scope string) Logger
WithScope returns a new logger instance with the provided scope added